react-native-navigation的迁移库

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