TUIChatService.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Created by Tencent on 2023/06/09.
  2. // Copyright © 2023 Tencent. All rights reserved.
  3. #import "TUIChatService.h"
  4. #import <TUICore/NSDictionary+TUISafe.h>
  5. #import <TUICore/TUILogin.h>
  6. #import <TUICore/TUIThemeManager.h>
  7. #import "TUIChatConfig.h"
  8. #import "TUIChatDefine.h"
  9. #import "TUIMessageDataProvider.h"
  10. #import "TUIMessageCellConfig.h"
  11. @interface TUIChatService () <TUINotificationProtocol, TUIExtensionProtocol>
  12. @end
  13. @implementation TUIChatService
  14. + (void)load {
  15. [TUICore registerService:TUICore_TUIChatService object:[TUIChatService shareInstance]];
  16. TUIRegisterThemeResourcePath(TUIChatThemePath, TUIThemeModuleChat);
  17. }
  18. + (TUIChatService *)shareInstance {
  19. static dispatch_once_t onceToken;
  20. static TUIChatService *g_sharedInstance = nil;
  21. dispatch_once(&onceToken, ^{
  22. g_sharedInstance = [[TUIChatService alloc] init];
  23. });
  24. return g_sharedInstance;
  25. }
  26. - (instancetype)init {
  27. if (self = [super init]) {
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccessNotification) name:TUILoginSuccessNotification object:nil];
  29. }
  30. return self;
  31. }
  32. - (void)loginSuccessNotification {
  33. [TUICore callService:TUICore_TUICallingService
  34. method:TUICore_TUICallingService_EnableFloatWindowMethod
  35. param:@{TUICore_TUICallingService_EnableFloatWindowMethod_EnableFloatWindow : @(TUIChatConfig.defaultConfig.enableFloatWindowForCall)}];
  36. [TUICore
  37. callService:TUICore_TUICallingService
  38. method:TUICore_TUICallingService_EnableMultiDeviceAbilityMethod
  39. param:@{
  40. TUICore_TUICallingService_EnableMultiDeviceAbilityMethod_EnableMultiDeviceAbility : @(TUIChatConfig.defaultConfig.enableMultiDeviceForCall)
  41. }];
  42. }
  43. - (NSString *)getDisplayString:(V2TIMMessage *)message {
  44. return [TUIMessageDataProvider getDisplayString:message];
  45. }
  46. #pragma mark - TUIServiceProtocol
  47. - (id)onCall:(NSString *)method param:(nullable NSDictionary *)param {
  48. if ([method isEqualToString:TUICore_TUIChatService_GetDisplayStringMethod]) {
  49. return [self getDisplayString:param[TUICore_TUIChatService_GetDisplayStringMethod_MsgKey]];
  50. } else if ([method isEqualToString:TUICore_TUIChatService_SendMessageMethod]) {
  51. V2TIMMessage *message = [param tui_objectForKey:TUICore_TUIChatService_SendMessageMethod_MsgKey asClass:V2TIMMessage.class];
  52. if (message == nil) {
  53. return nil;
  54. }
  55. NSDictionary *userInfo = @{TUICore_TUIChatService_SendMessageMethod_MsgKey : message};
  56. [[NSNotificationCenter defaultCenter] postNotificationName:TUIChatSendMessageNotification object:nil userInfo:userInfo];
  57. } else if ([method isEqualToString:TUICore_TUIChatService_SetChatExtensionMethod]) {
  58. [param enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *obj, BOOL *_Nonnull stop) {
  59. if (![key isKindOfClass:NSString.class] || ![obj isKindOfClass:NSNumber.class]) {
  60. return;
  61. }
  62. if ([key isEqualToString:TUICore_TUIChatService_SetChatExtensionMethod_EnableVideoCallKey]) {
  63. TUIChatConfig.defaultConfig.enableVideoCall = obj.boolValue;
  64. } else if ([key isEqualToString:TUICore_TUIChatService_SetChatExtensionMethod_EnableAudioCallKey]) {
  65. TUIChatConfig.defaultConfig.enableAudioCall = obj.boolValue;
  66. } else if ([key isEqualToString:TUICore_TUIChatService_SetChatExtensionMethod_EnableLinkKey]) {
  67. TUIChatConfig.defaultConfig.enableWelcomeCustomMessage = obj.boolValue;
  68. }
  69. }];
  70. } else if ([method isEqualToString:TUICore_TUIChatService_AppendCustomMessageMethod]) {
  71. if ([param isKindOfClass:NSDictionary.class]) {
  72. NSString *businessID = param[BussinessID];
  73. NSString *cellName = param[TMessageCell_Name];
  74. NSString *cellDataName = param[TMessageCell_Data_Name];
  75. [TUIMessageCellConfig registerCustomMessageCell:cellName messageCellData:cellDataName forBusinessID:businessID isPlugin:YES];
  76. }
  77. }
  78. return nil;
  79. }
  80. @end