react-native-navigation的迁移库

RNNEventEmitter.m 3.6KB

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