react-native-navigation的迁移库

RNNStore.m 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #import "RNNStore.h"
  2. @interface RNNStore ()
  3. @end
  4. @implementation RNNStore {
  5. NSMapTable* _componentStore;
  6. NSMutableDictionary* _externalComponentCreators;
  7. BOOL _isReadyToReceiveCommands;
  8. }
  9. -(instancetype)init {
  10. self = [super init];
  11. _isReadyToReceiveCommands = false;
  12. _componentStore = [NSMapTable strongToWeakObjectsMapTable];
  13. _externalComponentCreators = [NSMutableDictionary new];
  14. return self;
  15. }
  16. -(UIViewController *)findComponentForId:(NSString *)componentId {
  17. return [_componentStore objectForKey:componentId];
  18. }
  19. - (void)setComponent:(UIViewController*)viewController componentId:(NSString*)componentId {
  20. UIViewController *existingVewController = [self findComponentForId:componentId];
  21. if (existingVewController) {
  22. @throw [NSException exceptionWithName:@"MultipleComponentId" reason:[@"Component id already exists " stringByAppendingString:componentId] userInfo:nil];
  23. }
  24. [_componentStore setObject:viewController forKey:componentId];
  25. }
  26. - (void)removeComponent:(NSString*)componentId {
  27. [_componentStore removeObjectForKey:componentId];
  28. }
  29. - (void)removeComponentByViewControllerInstance:(UIViewController*)componentInstance {
  30. NSString *foundKey = [self componentKeyForInstance:componentInstance];
  31. if (foundKey) {
  32. [self removeComponent:foundKey];
  33. }
  34. }
  35. - (void)removeAllComponents {
  36. [_componentStore removeAllObjects];
  37. }
  38. - (void)removeAllComponentsFromWindow:(UIWindow *)window {
  39. for (NSString *key in [self componentsForWindow:window]) {
  40. [_componentStore removeObjectForKey:key];
  41. }
  42. }
  43. - (NSArray *)componentsForWindow:(UIWindow *)window {
  44. NSMutableArray* keyWindowComponents = [NSMutableArray new];
  45. for (NSString* key in _componentStore) {
  46. UIViewController *component = [_componentStore objectForKey:key];
  47. if (component.view.window == window) {
  48. [keyWindowComponents addObject:key];
  49. }
  50. }
  51. return keyWindowComponents;
  52. }
  53. -(void)setReadyToReceiveCommands:(BOOL)isReady {
  54. _isReadyToReceiveCommands = isReady;
  55. }
  56. -(BOOL)isReadyToReceiveCommands {
  57. return _isReadyToReceiveCommands;
  58. }
  59. -(void)clean {
  60. _isReadyToReceiveCommands = false;
  61. [self removeAllComponents];
  62. }
  63. -(NSString*)componentKeyForInstance:(UIViewController*)instance {
  64. for (NSString *key in _componentStore) {
  65. UIViewController *value = [_componentStore objectForKey:key];
  66. if (value == instance) {
  67. return key;
  68. }
  69. }
  70. return nil;
  71. }
  72. - (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback {
  73. [_externalComponentCreators setObject:[callback copy] forKey:name];
  74. }
  75. - (UIViewController *)getExternalComponent:(RNNLayoutInfo *)layoutInfo bridge:(RCTBridge *)bridge {
  76. RNNExternalViewCreator creator = [_externalComponentCreators objectForKey:layoutInfo.name];
  77. return creator(layoutInfo.props, bridge);
  78. }
  79. @end