react-native-navigation的迁移库

RNNEventEmitter.m 5.0KB

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