react-native-navigation的迁移库

RNNEventEmitter.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 AppLaunched = @"RNN.AppLaunched";
  9. static NSString* const CommandCompleted = @"RNN.CommandCompleted";
  10. static NSString* const BottomTabSelected = @"RNN.BottomTabSelected";
  11. static NSString* const ComponentDidAppear = @"RNN.ComponentDidAppear";
  12. static NSString* const ComponentDidDisappear = @"RNN.ComponentDidDisappear";
  13. static NSString* const NavigationButtonPressed = @"RNN.NavigationButtonPressed";
  14. static NSString* const SearchBarUpdated = @"RNN.SearchBarUpdated";
  15. static NSString* const SearchBarCancelPressed = @"RNN.SearchBarCancelPressed";
  16. -(NSArray<NSString *> *)supportedEvents {
  17. return @[AppLaunched,
  18. CommandCompleted,
  19. BottomTabSelected,
  20. ComponentDidAppear,
  21. ComponentDidDisappear,
  22. NavigationButtonPressed,
  23. SearchBarUpdated,
  24. SearchBarCancelPressed];
  25. }
  26. # pragma mark public
  27. -(void)sendOnAppLaunched {
  28. if (_appLaunchedListenerCount > 0) {
  29. [self send:AppLaunched body:nil];
  30. } else {
  31. _appLaunchedEventDeferred = TRUE;
  32. }
  33. }
  34. -(void)sendComponentDidAppear:(NSString *)componentId componentName:(NSString *)componentName {
  35. [self send:ComponentDidAppear body:@{
  36. @"componentId":componentId,
  37. @"componentName": componentName
  38. }];
  39. }
  40. -(void)sendComponentDidDisappear:(NSString *)componentId componentName:(NSString *)componentName{
  41. [self send:ComponentDidDisappear body:@{
  42. @"componentId":componentId,
  43. @"componentName": componentName
  44. }];
  45. }
  46. -(void)sendOnNavigationButtonPressed:(NSString *)componentId buttonId:(NSString*)buttonId {
  47. [self send:NavigationButtonPressed body:@{
  48. @"componentId": componentId,
  49. @"buttonId": buttonId
  50. }];
  51. }
  52. -(void)sendBottomTabSelected:(NSNumber *)selectedTabIndex unselected:(NSNumber*)unselectedTabIndex {
  53. [self send:BottomTabSelected body:@{
  54. @"selectedTabIndex": selectedTabIndex,
  55. @"unselectedTabIndex": unselectedTabIndex
  56. }];
  57. }
  58. -(void)sendOnNavigationCommandCompletion:(NSString *)commandName params:(NSDictionary*)params {
  59. [self send:CommandCompleted body:@{
  60. @"commandId":commandName,
  61. @"params": params,
  62. @"completionTime": [RNNUtils getCurrentTimestamp]
  63. }];
  64. }
  65. -(void)sendOnSearchBarUpdated:(NSString *)componentId
  66. text:(NSString*)text
  67. isFocused:(BOOL)isFocused {
  68. [self send:SearchBarUpdated body:@{
  69. @"componentId": componentId,
  70. @"text": text,
  71. @"isFocused": @(isFocused)
  72. }];
  73. }
  74. - (void)sendOnSearchBarCancelPressed:(NSString *)componentId {
  75. [self send:SearchBarCancelPressed body:@{
  76. @"componentId": componentId
  77. }];
  78. }
  79. - (void)addListener:(NSString *)eventName {
  80. [super addListener:eventName];
  81. if ([eventName isEqualToString:AppLaunched]) {
  82. _appLaunchedListenerCount++;
  83. if (_appLaunchedEventDeferred) {
  84. _appLaunchedEventDeferred = FALSE;
  85. [self sendOnAppLaunched];
  86. }
  87. }
  88. }
  89. # pragma mark private
  90. -(void)send:(NSString *)eventName body:(id)body {
  91. if (self.bridge == nil) {
  92. return;
  93. }
  94. [self sendEventWithName:eventName body:body];
  95. }
  96. @end