react-native-navigation的迁移库

RNNStore.m 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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)removeAllComponents {
  30. [_componentStore removeAllObjects];
  31. }
  32. - (void)removeAllComponentsFromWindow:(UIWindow *)window {
  33. for (NSString *key in [self componentsForWindow:window]) {
  34. [_componentStore removeObjectForKey:key];
  35. }
  36. }
  37. - (NSArray *)componentsForWindow:(UIWindow *)window {
  38. NSMutableArray* keyWindowComponents = [NSMutableArray new];
  39. for (NSString* key in _componentStore) {
  40. UIViewController *component = [_componentStore objectForKey:key];
  41. if (component.view.window == window) {
  42. [keyWindowComponents addObject:key];
  43. }
  44. }
  45. return keyWindowComponents;
  46. }
  47. -(void)setReadyToReceiveCommands:(BOOL)isReady {
  48. _isReadyToReceiveCommands = isReady;
  49. }
  50. -(BOOL)isReadyToReceiveCommands {
  51. return _isReadyToReceiveCommands;
  52. }
  53. -(void)clean {
  54. _isReadyToReceiveCommands = false;
  55. [self removeAllComponents];
  56. }
  57. -(NSString*)componentKeyForInstance:(UIViewController*)instance {
  58. for (NSString *key in _componentStore) {
  59. UIViewController *value = [_componentStore objectForKey:key];
  60. if (value == instance) {
  61. return key;
  62. }
  63. }
  64. return nil;
  65. }
  66. - (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback {
  67. [_externalComponentCreators setObject:[callback copy] forKey:name];
  68. }
  69. - (UIViewController *)getExternalComponent:(RNNLayoutInfo *)layoutInfo bridge:(RCTBridge *)bridge {
  70. RNNExternalViewCreator creator = [_externalComponentCreators objectForKey:layoutInfo.name];
  71. return creator(layoutInfo.props, bridge);
  72. }
  73. @end