react-native-navigation的迁移库

RNNEventEmitter.m 4.0KB

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