RQAppEventModuleManager.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // RQAppEventModuleManager.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2020/4/8.
  6. // Copyright © 2020 JCZ. All rights reserved.
  7. //
  8. #import "RQAppEventModuleManager.h"
  9. #import "RQAppEventAnnotation.h"
  10. #import "RQAppEventModuleProtocol.h"
  11. #define kModuleInfoNameKey @"moduleClass"
  12. #define kModuleInfoLevelKey @"moduleLevel"
  13. @implementation RQAppEventModuleManager
  14. + (instancetype)sharedInstance {
  15. static id sharedInstance = nil;
  16. static dispatch_once_t onceToken = 0;
  17. dispatch_once(&onceToken, ^{
  18. sharedInstance = [[self alloc] init];
  19. });
  20. return sharedInstance;
  21. }
  22. #pragma mark - public methods
  23. - (void)registedAllModules {
  24. NSArray<NSString *>*mods = [RQAppEventAnnotation AnnotationModules];
  25. for (NSString *modName in mods)
  26. {
  27. if (modName)
  28. {
  29. Class moduleCls = NSClassFromString(modName);
  30. if (moduleCls && [moduleCls conformsToProtocol:@protocol(RQAppEventModuleProtocol)])
  31. {
  32. NSMutableDictionary *moduleInfo = [NSMutableDictionary dictionary];
  33. id<RQAppEventModuleProtocol> moduleInstance = [[moduleCls alloc] init];
  34. NSInteger levelInt = (NSInteger)[moduleInstance performSelector:@selector(moduleLevel)];
  35. [moduleInfo setObject:@(levelInt) forKey:kModuleInfoLevelKey];
  36. [moduleInfo setObject:moduleInstance forKey:kModuleInfoNameKey];
  37. [self.appEventModules addObject:moduleInfo];
  38. }
  39. }
  40. }
  41. [self.appEventModules sortUsingComparator:^NSComparisonResult(NSDictionary *module1, NSDictionary *module2) {
  42. NSNumber *module1Level = (NSNumber *)[module1 objectForKey:kModuleInfoLevelKey];
  43. NSNumber *module2Level = (NSNumber *)[module2 objectForKey:kModuleInfoLevelKey];
  44. return [module1Level intValue] > [module2Level intValue];
  45. }];
  46. NSMutableArray *tmpArray = [NSMutableArray array];
  47. [self.appEventModules enumerateObjectsUsingBlock:^(NSDictionary *module, NSUInteger idx, BOOL * _Nonnull stop) {
  48. id<RQAppEventModuleProtocol> moduleInstance = [module objectForKey:kModuleInfoNameKey];
  49. [tmpArray addObject:moduleInstance];
  50. }];
  51. [self.appEventModules removeAllObjects];
  52. [self.appEventModules addObjectsFromArray:tmpArray];
  53. }
  54. - (void)handleApplicationEvent:(SEL)eventSel
  55. Complete:(void(^)(id module,SEL sel))complete {
  56. // appEventModules遍历Event过程中,module执行完可以将自己移除
  57. // 这里赋值为临时array,防止array遍历过程中移除自己导致出错
  58. NSMutableArray *tmpAppEventModules = [[NSMutableArray alloc] initWithArray:self.appEventModules];
  59. for (id<RQAppEventModuleProtocol>module in tmpAppEventModules)
  60. {
  61. if ([module conformsToProtocol:@protocol(RQAppEventModuleProtocol)])
  62. {
  63. if ([module respondsToSelector:eventSel]) {
  64. if (complete) {
  65. complete(module,eventSel);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. - (void)removeModule:(NSString *)moduleID {
  72. NSInteger index = NSNotFound;
  73. NSInteger resIndex = 0;
  74. for (id<RQAppEventModuleProtocol>module in self.appEventModules)
  75. {
  76. if ([[module moduleID] isEqualToString:moduleID])
  77. {
  78. index = resIndex;
  79. break;
  80. }
  81. resIndex++;
  82. }
  83. if (index != NSNotFound) {
  84. [self.appEventModules removeObjectAtIndex:index];
  85. }
  86. }
  87. #pragma mark - getter
  88. - (NSMutableArray *)appEventModules {
  89. if (!_appEventModules) {
  90. _appEventModules = [NSMutableArray array];
  91. }
  92. return _appEventModules;
  93. }
  94. @end