NIMKitNotificationFirer.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // NIMKitNotificationFirer.m
  3. // NIMKit
  4. //
  5. // Created by chris on 16/6/13.
  6. // Copyright © 2016年 NetEase. All rights reserved.
  7. //
  8. #import "NIMKitNotificationFirer.h"
  9. NSString *const NIMKitUserInfoHasUpdatedNotification = @"NIMKitUserInfoHasUpdatedNotification";
  10. NSString *const NIMKitTeamInfoHasUpdatedNotification = @"NIMKitTeamInfoHasUpdatedNotification";
  11. NSString *const NIMKitUserBlackListHasUpdatedNotification = @"NIMKitUserBlackListHasUpdatedNotification";
  12. NSString *const NIMKitUserMuteListHasUpdatedNotification = @"NIMKitUserMuteListHasUpdatedNotification";
  13. NSString *const NIMKitTeamMembersHasUpdatedNotification = @"NIMKitTeamMembersHasUpdatedNotification";
  14. NSString *const NIMKitInfoKey = @"InfoId";
  15. @implementation NIMKitNotificationFirer
  16. - (instancetype)init{
  17. self = [super init];
  18. if (self) {
  19. _timer = [[NIMKitTimerHolder alloc] init];
  20. _timeInterval = 1.0f;
  21. _cachedInfo = [[NSMutableDictionary alloc] init];
  22. }
  23. return self;
  24. }
  25. - (void)addFireInfo:(NIMKitFirerInfo *)info{
  26. NSAssert([NSThread currentThread].isMainThread, @"info must be fired in main thread");
  27. if (!self.cachedInfo.count) {
  28. [self.timer startTimer:self.timeInterval delegate:self repeats:NO];
  29. }
  30. [self.cachedInfo setObject:info forKey:info.saveIdentity];
  31. }
  32. #pragma mark - NIMKitTimerHolderDelegate
  33. - (void)onNIMKitTimerFired:(NIMKitTimerHolder *)holder{
  34. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  35. for (NIMKitFirerInfo *info in self.cachedInfo.allValues) {
  36. NSMutableArray *fireInfos = dict[info.notificationName];
  37. if (!fireInfos) {
  38. fireInfos = [[NSMutableArray alloc] init];
  39. dict[info.notificationName] = fireInfos;
  40. }
  41. if (info.fireObject) {
  42. [fireInfos addObject:info.fireObject];
  43. }
  44. }
  45. for (NSString *notificationName in dict) {
  46. NSDictionary *userInfo = dict[notificationName]? @{ NIMKitInfoKey:dict[notificationName] } : nil;
  47. [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:userInfo];
  48. }
  49. [self.cachedInfo removeAllObjects];
  50. }
  51. @end
  52. @implementation NIMKitFirerInfo
  53. - (NSObject *)fireObject
  54. {
  55. if (self.session) {
  56. return self.session.sessionId;
  57. }
  58. return [NSNull null];
  59. }
  60. - (NSString *)saveIdentity
  61. {
  62. if (self.session) {
  63. return [NSString stringWithFormat:@"%@-%zd",self.session.sessionId,self.session.sessionType];;
  64. }
  65. return self.notificationName;
  66. }
  67. @end