react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #import "RNNReactView.h"
  2. @implementation RNNReactView {
  3. BOOL _isAppeared;
  4. }
  5. - (instancetype)initWithBridge:(RCTBridge *)bridge moduleName:(NSString *)moduleName initialProperties:(NSDictionary *)initialProperties eventEmitter:(RNNEventEmitter *)eventEmitter reactViewReadyBlock:(RNNReactViewReadyCompletionBlock)reactViewReadyBlock {
  6. self = [super initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties];
  7. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentDidAppear:) name:RCTContentDidAppearNotification object:nil];
  8. _reactViewReadyBlock = reactViewReadyBlock;
  9. _eventEmitter = eventEmitter;
  10. return self;
  11. }
  12. - (void)contentDidAppear:(NSNotification *)notification {
  13. RNNReactView* appearedView = notification.object;
  14. if ([appearedView.appProperties[@"componentId"] isEqual:self.componentId]) {
  15. [self reactViewReady];
  16. }
  17. }
  18. - (void)reactViewReady {
  19. if (_reactViewReadyBlock) {
  20. _reactViewReadyBlock();
  21. _reactViewReadyBlock = nil;
  22. }
  23. [[NSNotificationCenter defaultCenter] removeObserver:self];
  24. }
  25. - (void)componentDidAppear {
  26. if (!_isAppeared) {
  27. [_eventEmitter sendComponentDidAppear:self.componentId componentName:self.moduleName componentType:self.componentType];
  28. }
  29. _isAppeared = YES;
  30. }
  31. - (void)componentDidDisappear {
  32. if (_isAppeared) {
  33. [_eventEmitter sendComponentDidDisappear:self.componentId componentName:self.moduleName componentType:self.componentType];
  34. }
  35. _isAppeared = NO;
  36. }
  37. - (NSString *)componentId {
  38. return self.appProperties[@"componentId"];
  39. }
  40. - (NSString *)componentType {
  41. @throw [NSException exceptionWithName:@"componentType not implemented" reason:@"Should always subclass RNNReactView" userInfo:nil];
  42. }
  43. @end