react-native-navigation的迁移库

RNNEventEmitter.m 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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)addListener:(NSString *)eventName {
  82. [super addListener:eventName];
  83. if ([eventName isEqualToString:AppLaunched]) {
  84. _appLaunchedListenerCount++;
  85. if (_appLaunchedEventDeferred) {
  86. _appLaunchedEventDeferred = FALSE;
  87. [self sendOnAppLaunched];
  88. }
  89. }
  90. }
  91. # pragma mark private
  92. -(void)send:(NSString *)eventName body:(id)body {
  93. if (self.bridge == nil) {
  94. return;
  95. }
  96. [self sendEventWithName:eventName body:body];
  97. }
  98. @end