react-native-navigation的迁移库

RNNBridgeModule.m 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #import "RNNBridgeModule.h"
  2. #import "RNN.h"
  3. #import "RNNControllerFactory.h"
  4. #import "RNNReactRootViewCreator.h"
  5. #import "RNNStore.h"
  6. #import "RNNModalManager.h"
  7. @implementation RNNBridgeModule
  8. RCT_EXPORT_MODULE();
  9. - (dispatch_queue_t)methodQueue
  10. {
  11. return dispatch_get_main_queue();
  12. }
  13. RCT_EXPORT_METHOD(setRoot:(NSDictionary*)layout)
  14. {
  15. [self assertReady];
  16. RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
  17. UIViewController *vc = [factory createLayoutAndSaveToStore:layout];
  18. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  19. [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
  20. }
  21. RCT_EXPORT_METHOD(push:(NSString*)containerId layout:(NSDictionary*)layout)
  22. {
  23. [self assertReady];
  24. RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
  25. UIViewController *newVc = [factory createLayoutAndSaveToStore:layout];
  26. UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
  27. [[vc navigationController] pushViewController:newVc animated:true];
  28. }
  29. RCT_EXPORT_METHOD(pop:(NSString*)containerId)
  30. {
  31. [self assertReady];
  32. UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
  33. [[vc navigationController] popViewControllerAnimated:YES];
  34. [[RNN instance].store removeContainer:containerId];
  35. }
  36. RCT_EXPORT_METHOD(showModal:(NSDictionary*)layout)
  37. {
  38. [self assertReady];
  39. RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
  40. UIViewController *newVc = [factory createLayoutAndSaveToStore:layout];
  41. [[[RNNModalManager alloc] initWithStore:[RNN instance].store] showModal:newVc];
  42. }
  43. RCT_EXPORT_METHOD(dismissModal:(NSString*)containerId)
  44. {
  45. [self assertReady];
  46. [[[RNNModalManager alloc] initWithStore:[RNN instance].store] dismissModal:containerId];
  47. }
  48. - (void)assertReady
  49. {
  50. if (![RNN instance].isReadyToReceiveCommands) {
  51. @throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
  52. }
  53. }
  54. @end