react-native-navigation的迁移库

RNNBridgeModule.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #import "RNNBridgeModule.h"
  2. #import "RNN.h"
  3. #import "RNNControllerFactory.h"
  4. #import "RNNReactRootViewCreator.h"
  5. #import "RNNStore.h"
  6. @implementation RNNBridgeModule
  7. RCT_EXPORT_MODULE();
  8. - (dispatch_queue_t)methodQueue
  9. {
  10. return dispatch_get_main_queue();
  11. }
  12. RCT_EXPORT_METHOD(setRoot:(NSDictionary*)layout)
  13. {
  14. [self assertReady];
  15. RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
  16. UIViewController *vc = [factory createLayoutAndSaveToStore:layout];
  17. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  18. [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
  19. }
  20. RCT_EXPORT_METHOD(push:(NSString*)containerId layout:(NSDictionary*)layout)
  21. {
  22. [self assertReady];
  23. RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
  24. UIViewController *newVc = [factory createLayoutAndSaveToStore:layout];
  25. UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
  26. [[vc navigationController] pushViewController:newVc animated:true];
  27. }
  28. RCT_EXPORT_METHOD(pop:(NSString*)containerId)
  29. {
  30. [self assertReady];
  31. UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
  32. [[vc navigationController] popViewControllerAnimated:YES];
  33. [[RNN instance].store removeContainer:containerId];
  34. }
  35. RCT_EXPORT_METHOD(showModal:(NSDictionary*)layout)
  36. {
  37. [self assertReady];
  38. RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
  39. UIViewController *newVc = [factory createLayoutAndSaveToStore:layout];
  40. UIViewController *root = [self topPresentedVC];
  41. [root presentViewController:newVc animated:YES completion:^{
  42. }];
  43. }
  44. RCT_EXPORT_METHOD(dismissModal:(NSString*)containerId)
  45. {
  46. UIViewController *modalToDismiss = [[RNN instance].store findContainerForId:containerId];
  47. if (modalToDismiss) {
  48. UIViewController *topVC = [self topPresentedVC];
  49. if (modalToDismiss == topVC) {
  50. [modalToDismiss dismissViewControllerAnimated:YES completion:^{
  51. [self removeNextModal];
  52. }];
  53. }
  54. else {
  55. [[RNN instance].store.modalsToDismissArray addObject:containerId];
  56. }
  57. }
  58. }
  59. -(void)removeNextModal {
  60. NSString *nextContainerId = [[RNN instance].store.modalsToDismissArray lastObject];
  61. UIViewController *vc = [[RNN instance].store findContainerForId:nextContainerId];
  62. if (vc) {
  63. UIViewController *topVC = [self topPresentedVC];
  64. if (vc == topVC) {
  65. [vc dismissViewControllerAnimated:YES completion:^{
  66. [[RNN instance].store.modalsToDismissArray removeObject:nextContainerId];
  67. [self removeNextModal];
  68. }];
  69. }
  70. }
  71. }
  72. - (void)assertReady
  73. {
  74. if (![RNN instance].isReadyToReceiveCommands) {
  75. @throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
  76. }
  77. }
  78. -(UIViewController*)topPresentedVC {
  79. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  80. while(root.presentedViewController) {
  81. root = root.presentedViewController;
  82. }
  83. return root;
  84. }
  85. @end