react-native-navigation的迁移库

RNNBridgeModule.m 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 = UIApplication.sharedApplication.delegate.window.rootViewController;
  41. while(root.presentedViewController) {
  42. root = root.presentedViewController;
  43. }
  44. [root presentViewController:newVc animated:YES completion:^{
  45. }];
  46. }
  47. RCT_EXPORT_METHOD(dismissModal:(NSString*)containerId)
  48. {
  49. UIViewController *root = [[RNN instance].store findContainerForId:containerId];
  50. // while(root.presentedViewController) {
  51. // root = root.presentedViewController;
  52. // }
  53. if (root) {
  54. [root dismissViewControllerAnimated:YES completion:^{
  55. }];
  56. }
  57. }
  58. - (void)assertReady
  59. {
  60. if (![RNN instance].isReadyToReceiveCommands) {
  61. @throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
  62. }
  63. }
  64. @end