react-native-navigation的迁移库

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