react-native-navigation的迁移库

RNNBridgeModule.m 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 createLayout: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 createLayout: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. - (void)assertReady
  36. {
  37. if (![RNN instance].isReadyToReceiveCommands) {
  38. @throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
  39. }
  40. }
  41. @end