RQNavigationControllerStack.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // RQNavigationControllerStack.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/14.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQNavigationControllerStack.h"
  9. @interface RQNavigationControllerStack ()
  10. @property (nonatomic, strong) id<RQViewModelServices> services;
  11. @property (nonatomic, strong) NSMutableArray *navigationControllers;
  12. @end
  13. @implementation RQNavigationControllerStack
  14. - (instancetype)initWithServices:(id<RQViewModelServices>)services {
  15. self = [super init];
  16. if (self) {
  17. self.services = services;
  18. self.navigationControllers = [[NSMutableArray alloc] init];
  19. [self registerNavigationHooks];
  20. }
  21. return self;
  22. }
  23. - (void)pushNavigationController:(UINavigationController *)navigationController {
  24. if ([self.navigationControllers containsObject:navigationController]) return;
  25. [self.navigationControllers addObject:navigationController];
  26. }
  27. - (UINavigationController *)popNavigationController {
  28. UINavigationController *navigationController = self.navigationControllers.lastObject;
  29. [self.navigationControllers removeLastObject];
  30. return navigationController;
  31. }
  32. - (UINavigationController *)topNavigationController {
  33. return self.navigationControllers.lastObject;
  34. }
  35. - (void)registerNavigationHooks {
  36. @weakify(self)
  37. [[(NSObject *)self.services
  38. rac_signalForSelector:@selector(pushViewModel:animated:)]
  39. subscribeNext:^(RACTuple *tuple) {
  40. @strongify(self)
  41. RQBaseViewController *topViewController = (RQBaseViewController *)[self.navigationControllers.lastObject topViewController];
  42. if (topViewController.tabBarController) {
  43. topViewController.snapshot = [topViewController.tabBarController.view snapshotViewAfterScreenUpdates:NO];
  44. } else {
  45. topViewController.snapshot = [[self.navigationControllers.lastObject view] snapshotViewAfterScreenUpdates:NO];
  46. }
  47. UIViewController *viewController = (UIViewController *)[RQRouter.sharedInstance viewControllerForViewModel:tuple.first];
  48. [self.navigationControllers.lastObject pushViewController:viewController animated:[tuple.second boolValue]];
  49. }];
  50. [[(NSObject *)self.services
  51. rac_signalForSelector:@selector(popViewModelAnimated:)]
  52. subscribeNext:^(RACTuple *tuple) {
  53. @strongify(self)
  54. [self.navigationControllers.lastObject popViewControllerAnimated:[tuple.first boolValue]];
  55. }];
  56. [[(NSObject *)self.services
  57. rac_signalForSelector:@selector(popToRootViewModelAnimated:)]
  58. subscribeNext:^(RACTuple *tuple) {
  59. @strongify(self)
  60. [self.navigationControllers.lastObject popToRootViewControllerAnimated:[tuple.first boolValue]];
  61. }];
  62. [[(NSObject *)self.services
  63. rac_signalForSelector:@selector(presentViewModel:animated:completion:)]
  64. subscribeNext:^(RACTuple *tuple) {
  65. @strongify(self)
  66. UIViewController *viewController = (UIViewController *)[RQRouter.sharedInstance viewControllerForViewModel:tuple.first];
  67. UINavigationController *presentingViewController = self.navigationControllers.lastObject;
  68. if (![viewController isKindOfClass:UINavigationController.class]) {
  69. viewController = [[RQBaseNavigationController alloc] initWithRootViewController:viewController];
  70. }
  71. [self pushNavigationController:(UINavigationController *)viewController];
  72. viewController.modalPresentationStyle = UIModalPresentationFullScreen;
  73. [presentingViewController presentViewController:viewController animated:[tuple.second boolValue] completion:tuple.third];
  74. }];
  75. [[(NSObject *)self.services
  76. rac_signalForSelector:@selector(dismissViewModelAnimated:completion:)]
  77. subscribeNext:^(RACTuple *tuple) {
  78. @strongify(self)
  79. [self popNavigationController];
  80. [self.navigationControllers.lastObject dismissViewControllerAnimated:[tuple.first boolValue] completion:tuple.second];
  81. }];
  82. [[(NSObject *)self.services
  83. rac_signalForSelector:@selector(resetRootViewModel:)]
  84. subscribeNext:^(RACTuple *tuple) {
  85. @strongify(self)
  86. [self.navigationControllers removeAllObjects];
  87. /// VM映射VC
  88. UIViewController *viewController = (UIViewController *)[RQRouter.sharedInstance viewControllerForViewModel:tuple.first];
  89. if (![viewController isKindOfClass:[UINavigationController class]] &&
  90. ![viewController isKindOfClass:[RQTabBarController class]]) {
  91. viewController = [[RQBaseNavigationController alloc] initWithRootViewController:viewController];
  92. [self pushNavigationController:(UINavigationController *)viewController];
  93. }
  94. RQSharedAppDelegate.window.rootViewController = viewController;
  95. }];
  96. }
  97. @end