react-native-navigation的迁移库

RNNReactView.m 1.9KB

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