NIMKit.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // NIMKit.m
  3. // NIMKit
  4. //
  5. // Created by amao on 8/14/15.
  6. // Copyright (c) 2015 NetEase. All rights reserved.
  7. //
  8. #import "NIMKit.h"
  9. #import "NIMKitTimerHolder.h"
  10. #import "NIMKitNotificationFirer.h"
  11. #import "NIMKitDataProviderImpl.h"
  12. #import "NIMCellLayoutConfig.h"
  13. #import "NIMKitInfoFetchOption.h"
  14. extern NSString *const NIMKitUserInfoHasUpdatedNotification;
  15. extern NSString *const NIMKitTeamInfoHasUpdatedNotification;
  16. @interface NIMKit(){
  17. NSRegularExpression *_urlRegex;
  18. }
  19. @property (nonatomic,strong) NIMKitNotificationFirer *firer;
  20. @property (nonatomic,strong) id<NIMCellLayoutConfig> layoutConfig;
  21. @end
  22. @implementation NIMKit
  23. - (instancetype)init
  24. {
  25. if (self = [super init]) {
  26. _resourceBundleName = @"NIMKitResource.bundle";
  27. _emoticonBundleName = @"NIMKitEmoticon.bundle";
  28. _firer = [[NIMKitNotificationFirer alloc] init];
  29. _provider = [[NIMKitDataProviderImpl alloc] init]; //默认使用 NIMKit 的实现
  30. _layoutConfig = [[NIMCellLayoutConfig alloc] init];
  31. _robotTemplateParser = [[NIMKitRobotDefaultTemplateParser alloc] init];
  32. }
  33. return self;
  34. }
  35. + (instancetype)sharedKit
  36. {
  37. static NIMKit *instance = nil;
  38. static dispatch_once_t onceToken;
  39. dispatch_once(&onceToken, ^{
  40. instance = [[NIMKit alloc] init];
  41. });
  42. return instance;
  43. }
  44. - (void)registerLayoutConfig:(NIMCellLayoutConfig *)layoutConfig
  45. {
  46. if ([layoutConfig isKindOfClass:[NIMCellLayoutConfig class]])
  47. {
  48. self.layoutConfig = layoutConfig;
  49. }
  50. else
  51. {
  52. NSAssert(0, @"class should be subclass of NIMLayoutConfig");
  53. }
  54. }
  55. - (id<NIMCellLayoutConfig>)layoutConfig
  56. {
  57. return _layoutConfig;
  58. }
  59. - (NIMKitConfig *)config
  60. {
  61. //不要放在 NIMKit 初始化里面,因为 UIConfig 初始化会使用 NIMKit, 防止死循环
  62. if (!_config)
  63. {
  64. _config = [[NIMKitConfig alloc] init];
  65. }
  66. return _config;
  67. }
  68. - (void)notfiyUserInfoChanged:(NSArray *)userIds{
  69. if (!userIds.count) {
  70. return;
  71. }
  72. for (NSString *userId in userIds) {
  73. NIMSession *session = [NIMSession session:userId type:NIMSessionTypeP2P];
  74. NIMKitFirerInfo *info = [[NIMKitFirerInfo alloc] init];
  75. info.session = session;
  76. info.notificationName = NIMKitUserInfoHasUpdatedNotification;
  77. [self.firer addFireInfo:info];
  78. }
  79. }
  80. - (void)notifyTeamInfoChanged:(NSArray *)teamIds{
  81. if (teamIds.count)
  82. {
  83. for (NSString *teamId in teamIds)
  84. {
  85. [self notifyTeam:teamId];
  86. }
  87. }
  88. else
  89. {
  90. [self notifyTeam:nil];
  91. }
  92. }
  93. - (void)notifyTeamMemebersChanged:(NSArray *)teamIds
  94. {
  95. if (teamIds.count)
  96. {
  97. for (NSString *teamId in teamIds)
  98. {
  99. [self notifyTeamMemebers:teamId];
  100. }
  101. }
  102. else
  103. {
  104. [self notifyTeamMemebers:nil];
  105. }
  106. }
  107. - (void)notifyTeam:(NSString *)teamId
  108. {
  109. NIMKitFirerInfo *info = [[NIMKitFirerInfo alloc] init];
  110. if (teamId.length) {
  111. NIMSession *session = [NIMSession session:teamId type:NIMSessionTypeTeam];
  112. info.session = session;
  113. }
  114. info.notificationName = NIMKitTeamInfoHasUpdatedNotification;
  115. [self.firer addFireInfo:info];
  116. }
  117. - (void)notifyTeamMemebers:(NSString *)teamId
  118. {
  119. NIMKitFirerInfo *info = [[NIMKitFirerInfo alloc] init];
  120. if (teamId.length) {
  121. NIMSession *session = [NIMSession session:teamId type:NIMSessionTypeTeam];
  122. info.session = session;
  123. }
  124. extern NSString *NIMKitTeamMembersHasUpdatedNotification;
  125. info.notificationName = NIMKitTeamMembersHasUpdatedNotification;
  126. [self.firer addFireInfo:info];
  127. }
  128. - (NIMKitInfo *)infoByUser:(NSString *)userId option:(NIMKitInfoFetchOption *)option
  129. {
  130. NIMKitInfo *info = nil;
  131. if (self.provider && [self.provider respondsToSelector:@selector(infoByUser:option:)]) {
  132. info = [self.provider infoByUser:userId option:option];
  133. }
  134. return info;
  135. }
  136. - (NIMKitInfo *)infoByTeam:(NSString *)teamId option:(NIMKitInfoFetchOption *)option
  137. {
  138. NIMKitInfo *info = nil;
  139. if (self.provider && [self.provider respondsToSelector:@selector(infoByTeam:option:)]) {
  140. info = [self.provider infoByTeam:teamId option:option];
  141. }
  142. return info;
  143. }
  144. @end