react-native-navigation的迁移库

RNNEventEmitter.m 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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)sendOnSearchBarCancelPressed:(NSString *)componentId {
  52. [self send:navigationEvent body:@{@"name": @"searchBarCancelPressed",
  53. @"params": @{
  54. @"componentId": componentId}}];
  55. }
  56. - (void)addListener:(NSString *)eventName {
  57. [super addListener:eventName];
  58. if ([eventName isEqualToString:onAppLaunched]) {
  59. _appLaunchedListenerCount++;
  60. if (_appLaunchedEventDeferred) {
  61. _appLaunchedEventDeferred = FALSE;
  62. [self sendOnAppLaunched];
  63. }
  64. }
  65. }
  66. # pragma mark private
  67. -(void)send:(NSString *)eventName body:(id)body {
  68. if ([eventName isEqualToString:componentDidDisappear] && self.bridge == nil) {
  69. return;
  70. }
  71. [self sendEventWithName:eventName body:body];
  72. }
  73. @end