react-native-navigation的迁移库

RNNEventEmitter.m 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #import "RNNEventEmitter.h"
  2. #import "RNNUtils.h"
  3. @implementation RNNEventEmitter {
  4. NSInteger _appLaunchedListenerCount;
  5. BOOL _appLaunchedEventDeferred;
  6. }
  7. RCT_EXPORT_MODULE();
  8. static NSString* const onAppLaunched = @"RNN.appLaunched";
  9. static NSString* const componentDidAppear = @"RNN.componentDidAppear";
  10. static NSString* const componentDidDisappear = @"RNN.componentDidDisappear";
  11. static NSString* const commandComplete = @"RNN.commandCompleted";
  12. static NSString* const navigationEvent = @"RNN.nativeEvent";
  13. -(NSArray<NSString *> *)supportedEvents {
  14. return @[onAppLaunched, componentDidAppear, componentDidDisappear, commandComplete, navigationEvent];
  15. }
  16. # pragma mark public
  17. -(void)sendOnAppLaunched {
  18. if (_appLaunchedListenerCount > 0) {
  19. [self send:onAppLaunched body:nil];
  20. } else {
  21. _appLaunchedEventDeferred = TRUE;
  22. }
  23. }
  24. -(void)sendComponentDidAppear:(NSString *)componentId componentName:(NSString *)componentName {
  25. [self send:componentDidAppear body:@{@"componentId":componentId, @"componentName": componentName}];
  26. }
  27. -(void)sendComponentDidDisappear:(NSString *)componentId componentName:(NSString *)componentName{
  28. [self send:componentDidDisappear body:@{@"componentId":componentId, @"componentName": componentName}];
  29. }
  30. -(void)sendOnNavigationButtonPressed:(NSString *)componentId buttonId:(NSString*)buttonId {
  31. [self send:navigationEvent body:@{@"name": @"buttonPressed", @"params": @{@"componentId": componentId , @"buttonId": buttonId}}];
  32. }
  33. -(void)sendOnNavigationCommand:(NSString *)commandName params:(NSDictionary*)params {
  34. [self send:navigationEvent body:@{@"name":commandName , @"params": params}];
  35. }
  36. -(void)sendOnNavigationCommandCompletion:(NSString *)commandName params:(NSDictionary*)params {
  37. [self send:commandComplete body:@{@"commandId":commandName , @"params": params, @"completionTime": [RNNUtils getCurrentTimestamp] }];
  38. }
  39. -(void)sendOnNavigationEvent:(NSString *)commandName params:(NSDictionary*)params {
  40. [self send:navigationEvent body:@{@"name":commandName , @"params": params}];
  41. }
  42. -(void)sendOnSearchBarUpdated:(NSString *)componentId
  43. text:(NSString*)text
  44. isFocused:(BOOL)isFocused {
  45. [self send:navigationEvent body:@{@"name": @"searchBarUpdated",
  46. @"params": @{
  47. @"componentId": componentId,
  48. @"text": text,
  49. @"isFocused": @(isFocused)}}];
  50. }
  51. - (void)addListener:(NSString *)eventName {
  52. [super addListener:eventName];
  53. if ([eventName isEqualToString:onAppLaunched]) {
  54. _appLaunchedListenerCount++;
  55. if (_appLaunchedEventDeferred) {
  56. _appLaunchedEventDeferred = FALSE;
  57. [self sendOnAppLaunched];
  58. }
  59. }
  60. }
  61. # pragma mark private
  62. -(void)send:(NSString *)eventName body:(id)body {
  63. if ([eventName isEqualToString:componentDidDisappear] && self.bridge == nil) {
  64. return;
  65. }
  66. [self sendEventWithName:eventName body:body];
  67. }
  68. @end