react-native-navigation的迁移库

RNNEventEmitter.m 4.7KB

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