react-native-navigation的迁移库

RNNEventEmitter.m 4.5KB

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