react-native-navigation的迁移库

RNNBridgeModule.m 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if([[vc navigationController] topViewController] == vc ) {
  34. [[vc navigationController] popViewControllerAnimated:YES];
  35. }
  36. else {
  37. NSMutableArray * vcs = [vc navigationController].viewControllers.mutableCopy;
  38. [vcs removeObject:vc];
  39. [[vc navigationController] setViewControllers:vcs animated:NO];
  40. }
  41. [[RNN instance].store removeContainer:containerId];
  42. }
  43. RCT_EXPORT_METHOD(showModal:(NSDictionary*)layout)
  44. {
  45. [self assertReady];
  46. RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
  47. UIViewController *newVc = [factory createLayoutAndSaveToStore:layout];
  48. [[[RNNModalManager alloc] initWithStore:[RNN instance].store] showModal:newVc];
  49. }
  50. RCT_EXPORT_METHOD(dismissModal:(NSString*)containerId)
  51. {
  52. [self assertReady];
  53. [[[RNNModalManager alloc] initWithStore:[RNN instance].store] dismissModal:containerId];
  54. }
  55. RCT_EXPORT_METHOD(dismissAllModals)
  56. {
  57. [self assertReady];
  58. [[[RNNModalManager alloc] initWithStore:[RNN instance].store] dismissAllModals];
  59. }
  60. - (void)assertReady
  61. {
  62. if (![RNN instance].isReadyToReceiveCommands) {
  63. @throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
  64. }
  65. }
  66. @end