react-native-navigation的迁移库

RNNStore.m 1.9KB

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