// // TUIBaseMessageController+TUIPatch.m // Youth // // Created by mambaxie on 2022/1/15. // #import "TUIBaseMessageController+TUIPatch.h" #import #import "NSObject+BKAssociatedObjects.h" #import "V2TIMMessage+TUIPatch.h" #import "TUIChatPopMenu.h" #import "TUITextMessageCellData.h" #import "TUIReplyMessageCellData.h" #import "TUITextMessageCell.h" static NSString * const IsInActivityRoomAssociateKey = @"IsInActivityRoomAssociateKey"; @implementation TUIBaseMessageController (TUIPatch) + (void)load { NSError *error; [self jr_swizzleMethod:@selector(patch_sendMessage:) withMethod:@selector(sendMessage:) error:&error]; [self jr_swizzleMethod:@selector(patch_sendUIMessage:) withMethod:@selector(sendUIMessage:) error:&error]; [self jr_swizzleMethod:@selector(patch_supportCheckBox:) withMethod:@selector(supportCheckBox:) error:&error]; [self jr_swizzleMethod:@selector(patch_onLongPressMessage:) withMethod:@selector(onLongPressMessage:) error:&error]; if (error) { NSLog(@"patch TUI error: %@", error); } } - (void)patch_setIsInActivityRoom:(BOOL)isInActivityRoom { [self bk_associateValue:@(isInActivityRoom) withKey:(__bridge const void * _Nonnull)(IsInActivityRoomAssociateKey)]; } - (BOOL)patch_isInActivityRoom { return [[self bk_associatedValueForKey:(__bridge const void * _Nonnull)(IsInActivityRoomAssociateKey)] boolValue]; } - (void)patch_sendMessage:(V2TIMMessage *)msg { if ([self patch_isInActivityRoom]) { [msg patch_setIsInActivityRoom:YES]; } [self patch_sendMessage:msg]; } - (void)patch_sendUIMessage:(TUIMessageCellData *)cellData { if ([self patch_isInActivityRoom]) { cellData.showName = YES; cellData.name = [UserService currentUser].nickname; } [self patch_sendUIMessage:cellData]; } - (BOOL)patch_supportCheckBox:(TUIMessageCellData *)data { if ([self patch_isInActivityRoom]) { data.showName = YES; } return [self patch_supportCheckBox:data]; } - (void)patch_onLongPressMessage:(TUIMessageCell *)cell { if ([self patch_isInActivityRoom]) { return; } if (![cell isKindOfClass:TUITextMessageCell.class]) { // 指引纯文本才支持复制 return; } /// 长按直接复制文本 if ([self respondsToSelector:@selector(onCopyMsg:)]) { ((TUITextMessageCell *)cell).selectContent = ((TUITextMessageCell *)cell).textView.text; [self performSelector:@selector(onCopyMsg:) withObject:cell]; } // TUIMessageCellData *data = cell.messageData; //// [self patch_onLongPressMessage:cell]; // [self setValue:data forKeyPath:@"menuUIMsg"]; //// self.menuUIMsg = ; // __weak typeof(self) weakSelf = self; // TUIChatPopMenu *menu = [[TUIChatPopMenu alloc] init]; // if ([data isKindOfClass:[TUITextMessageCellData class]] || [data isKindOfClass:TUIReplyMessageCellData.class]) { // [menu addAction:[[TUIChatPopMenuAction alloc] initWithTitle:TUIKitLocalizableString(Copy) image:[UIImage d_imageNamed:@"icon_copy" bundle:TUIChatBundle] rank:1 callback:^{ // ((TUITextMessageCell *)cell).selectContent = ((TUITextMessageCell *)cell).textView.text; // [weakSelf performSelector:@selector(onCopyMsg:) withObject:cell]; // }]]; // } // BOOL isFirstResponder = NO; // if(isFirstResponder){ // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuDidHide:) name:UIMenuControllerDidHideMenuNotification object:nil]; // } // else{ // [self becomeFirstResponder]; // } // // CGRect frame = [UIApplication.sharedApplication.keyWindow convertRect:cell.container.frame fromView:cell]; // [menu setArrawPosition:CGPointMake(frame.origin.x + frame.size.width * 0.5, frame.origin.y - 5) // adjustHeight:frame.size.height + 5]; // [menu showInView:self.tableView]; } @end