react-native-navigation的迁移库

RNNStore.m 2.4KB

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