react-native-navigation的迁移库

RNNEventEmitter.m 4.3KB

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