react-native-navigation的迁移库

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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)setReadyToReceiveCommands:(BOOL)isReady {
  36. _isReadyToReceiveCommands = isReady;
  37. }
  38. -(BOOL)isReadyToReceiveCommands {
  39. return _isReadyToReceiveCommands;
  40. }
  41. -(void)clean {
  42. _isReadyToReceiveCommands = false;
  43. [_componentStore removeAllObjects];
  44. }
  45. -(NSString*)componentKeyForInstance:(UIViewController*)instance {
  46. for (NSString *key in _componentStore) {
  47. UIViewController *value = [_componentStore objectForKey:key];
  48. if (value == instance) {
  49. return key;
  50. }
  51. }
  52. return nil;
  53. }
  54. - (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback {
  55. [_externalComponentCreators setObject:[callback copy] forKey:name];
  56. }
  57. - (UIViewController *)getExternalComponent:(NSString *)name props:(NSDictionary*)props bridge:(RCTBridge *)bridge {
  58. RNNExternalViewCreator creator = [_externalComponentCreators objectForKey:name];
  59. return creator(props, bridge);
  60. }
  61. @end