react-native-navigation的迁移库

RNNEventEmitter.m 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 onAppLaunched = @"RNN.appLaunched";
  9. static NSString* const componentDidAppear = @"RNN.componentDidAppear";
  10. static NSString* const componentDidDisappear = @"RNN.componentDidDisappear";
  11. static NSString* const commandComplete = @"RNN.commandCompleted";
  12. static NSString* const navigationEvent = @"RNN.nativeEvent";
  13. -(NSArray<NSString *> *)supportedEvents {
  14. return @[onAppLaunched, componentDidAppear, componentDidDisappear, commandComplete, navigationEvent];
  15. }
  16. # pragma mark public
  17. -(void)sendOnAppLaunched {
  18. if (_appLaunchedListenerCount > 0) {
  19. [self send:onAppLaunched body:nil];
  20. } else {
  21. _appLaunchedEventDeferred = TRUE;
  22. }
  23. }
  24. -(void)sendComponentDidAppear:(NSString *)componentId componentName:(NSString *)componentName {
  25. [self send:componentDidAppear body:@{@"componentId":componentId, @"componentName": componentName}];
  26. }
  27. -(void)sendComponentDidDisappear:(NSString *)componentId componentName:(NSString *)componentName{
  28. [self send:componentDidDisappear body:@{@"componentId":componentId, @"componentName": componentName}];
  29. }
  30. -(void)sendOnNavigationButtonPressed:(NSString *)componentId buttonId:(NSString*)buttonId {
  31. [self send:navigationEvent body:@{@"name": @"buttonPressed", @"params": @{@"componentId": componentId , @"buttonId": buttonId}}];
  32. }
  33. -(void)sendOnNavigationCommand:(NSString *)commandName params:(NSDictionary*)params {
  34. [self send:navigationEvent body:@{@"name":commandName , @"params": params}];
  35. }
  36. -(void)sendOnNavigationCommandCompletion:(NSString *)commandName params:(NSDictionary*)params {
  37. NSMutableDictionary* mutableParams = [NSMutableDictionary dictionaryWithDictionary:params];
  38. [mutableParams setObject:[RNNUtils getCurrentTimestamp] forKey:@"timestamp"];
  39. [self send:commandComplete body:@{@"name":commandName , @"params": mutableParams}];
  40. }
  41. -(void)sendOnNavigationEvent:(NSString *)commandName params:(NSDictionary*)params {
  42. [self send:navigationEvent body:@{@"name":commandName , @"params": params}];
  43. }
  44. -(void)sendOnSearchBarUpdated:(NSString *)componentId
  45. text:(NSString*)text
  46. isFocused:(BOOL)isFocused {
  47. [self send:navigationEvent body:@{@"name": @"searchBarUpdated",
  48. @"params": @{
  49. @"componentId": componentId,
  50. @"text": text,
  51. @"isFocused": @(isFocused)}}];
  52. }
  53. - (void)addListener:(NSString *)eventName {
  54. [super addListener:eventName];
  55. if ([eventName isEqualToString:onAppLaunched]) {
  56. _appLaunchedListenerCount++;
  57. if (_appLaunchedEventDeferred) {
  58. _appLaunchedEventDeferred = FALSE;
  59. [self sendOnAppLaunched];
  60. }
  61. }
  62. }
  63. # pragma mark private
  64. -(void)send:(NSString *)eventName body:(id)body {
  65. if ([eventName isEqualToString:componentDidDisappear] && self.bridge == nil) {
  66. return;
  67. }
  68. [self sendEventWithName:eventName body:body];
  69. }
  70. @end