react-native-navigation的迁移库

RNNEventEmitter.m 5.3KB

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