react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #import "UIViewController+LayoutProtocol.h"
  2. #import <objc/runtime.h>
  3. @implementation UIViewController (LayoutProtocol)
  4. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
  5. creator:(id<RNNComponentViewCreator>)creator
  6. options:(RNNNavigationOptions *)options
  7. defaultOptions:(RNNNavigationOptions *)defaultOptions
  8. presenter:(RNNBasePresenter *)presenter
  9. eventEmitter:(RNNEventEmitter *)eventEmitter
  10. childViewControllers:(NSArray *)childViewControllers {
  11. self = [self init];
  12. self.options = options;
  13. self.defaultOptions = defaultOptions;
  14. self.layoutInfo = layoutInfo;
  15. self.creator = creator;
  16. self.eventEmitter = eventEmitter;
  17. if ([self respondsToSelector:@selector(setViewControllers:)]) {
  18. [self performSelector:@selector(setViewControllers:) withObject:childViewControllers];
  19. }
  20. self.presenter = presenter;
  21. [self.presenter bindViewController:self];
  22. [self.presenter applyOptionsOnInit:self.resolveOptions];
  23. return self;
  24. }
  25. - (void)mergeOptions:(RNNNavigationOptions *)options {
  26. [self.options overrideOptions:options];
  27. [self.presenter mergeOptions:options resolvedOptions:self.resolveOptions];
  28. [self.parentViewController mergeChildOptions:options child:self];
  29. }
  30. - (void)mergeChildOptions:(RNNNavigationOptions *)options child:(UIViewController *)child {
  31. [self.presenter mergeOptions:options resolvedOptions:self.resolveOptions];
  32. [self.parentViewController mergeChildOptions:options child:child];
  33. }
  34. - (RNNNavigationOptions *)resolveOptions {
  35. return (RNNNavigationOptions *) [self.options mergeInOptions:self.getCurrentChild.resolveOptions.copy];
  36. }
  37. - (RNNNavigationOptions *)resolveOptionsWithDefault {
  38. return [(RNNNavigationOptions *) [self.options mergeInOptions:self.getCurrentChild.resolveOptions.copy] withDefault:self.defaultOptions];
  39. }
  40. - (void)overrideOptions:(RNNNavigationOptions *)options {
  41. [self.options overrideOptions:options];
  42. }
  43. - (BOOL)extendedLayoutIncludesOpaqueBars {
  44. return YES;
  45. }
  46. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  47. UIInterfaceOrientationMask interfaceOrientationMask = self.presenter ? [self.presenter getOrientation:[self resolveOptions]] : [[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:[[UIApplication sharedApplication] keyWindow]];
  48. return interfaceOrientationMask;
  49. }
  50. - (UINavigationController *)stack {
  51. if ([self isKindOfClass:UINavigationController.class]) {
  52. return (UINavigationController *)self;
  53. } else {
  54. return self.navigationController;
  55. }
  56. }
  57. - (void)render {
  58. if (!self.waitForRender) {
  59. [self readyForPresentation];
  60. }
  61. [self.presentedComponentViewController setReactViewReadyCallback:^{
  62. [self.presenter renderComponents:self.resolveOptionsWithDefault perform:^{
  63. [self readyForPresentation];
  64. }];
  65. }];
  66. [self.presentedComponentViewController render];
  67. }
  68. - (void)readyForPresentation {
  69. if (self.reactViewReadyCallback) {
  70. self.reactViewReadyCallback();
  71. self.reactViewReadyCallback = nil;
  72. }
  73. [self.parentViewController readyForPresentation];
  74. }
  75. - (UIViewController *)getCurrentChild {
  76. for (UIViewController* childViewController in self.childViewControllers.reverseObjectEnumerator.allObjects) {
  77. if (childViewController.layoutInfo) {
  78. return childViewController;
  79. }
  80. }
  81. return nil;
  82. }
  83. - (UIViewController *)presentedComponentViewController {
  84. UIViewController* currentChild = self.getCurrentChild;
  85. return currentChild ? currentChild.presentedComponentViewController : self;
  86. }
  87. - (UIViewController *)topMostViewController {
  88. if (self.parentViewController) {
  89. return [self.parentViewController topMostViewController];
  90. } else
  91. return self;
  92. }
  93. - (CGFloat)getTopBarHeight {
  94. for(UIViewController * child in [self childViewControllers]) {
  95. CGFloat childTopBarHeight = [child getTopBarHeight];
  96. if (childTopBarHeight > 0) return childTopBarHeight;
  97. }
  98. return 0;
  99. }
  100. - (CGFloat)getBottomTabsHeight {
  101. for(UIViewController * child in [self childViewControllers]) {
  102. CGFloat childBottomTabsHeight = [child getBottomTabsHeight];
  103. if (childBottomTabsHeight > 0) return childBottomTabsHeight;
  104. }
  105. return 0;
  106. }
  107. - (void)onChildWillAppear {
  108. [self.presenter applyOptions:self.resolveOptions];
  109. [self.parentViewController onChildWillAppear];
  110. }
  111. - (void)componentDidAppear {
  112. [self.presenter componentDidAppear];
  113. [self.parentViewController componentDidAppear];
  114. }
  115. - (void)componentDidDisappear {
  116. [self.presenter componentDidDisappear];
  117. [self.parentViewController componentDidDisappear];
  118. }
  119. - (void)willMoveToParentViewController:(UIViewController *)parent {
  120. if (parent) {
  121. [self.presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  122. }
  123. }
  124. #pragma mark getters and setters to associated object
  125. - (RNNNavigationOptions *)options {
  126. return objc_getAssociatedObject(self, @selector(options));
  127. }
  128. - (void)setOptions:(RNNNavigationOptions *)options {
  129. objc_setAssociatedObject(self, @selector(options), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  130. }
  131. - (RNNNavigationOptions *)defaultOptions {
  132. return objc_getAssociatedObject(self, @selector(defaultOptions));
  133. }
  134. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  135. objc_setAssociatedObject(self, @selector(defaultOptions), defaultOptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  136. }
  137. - (RNNLayoutInfo *)layoutInfo {
  138. return objc_getAssociatedObject(self, @selector(layoutInfo));
  139. }
  140. - (void)setLayoutInfo:(RNNLayoutInfo *)layoutInfo {
  141. objc_setAssociatedObject(self, @selector(layoutInfo), layoutInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  142. }
  143. - (RNNBasePresenter *)presenter {
  144. return objc_getAssociatedObject(self, @selector(presenter));
  145. }
  146. - (void)setPresenter:(RNNBasePresenter *)presenter {
  147. objc_setAssociatedObject(self, @selector(presenter), presenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  148. }
  149. - (RNNEventEmitter *)eventEmitter {
  150. return objc_getAssociatedObject(self, @selector(eventEmitter));
  151. }
  152. - (void)setEventEmitter:(RNNEventEmitter *)eventEmitter {
  153. objc_setAssociatedObject(self, @selector(eventEmitter), eventEmitter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  154. }
  155. - (id<RNNComponentViewCreator>)creator {
  156. return objc_getAssociatedObject(self, @selector(creator));
  157. }
  158. - (void)setCreator:(id<RNNComponentViewCreator>)creator {
  159. objc_setAssociatedObject(self, @selector(creator), creator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  160. }
  161. - (RNNReactViewReadyCompletionBlock)reactViewReadyCallback {
  162. return objc_getAssociatedObject(self, @selector(reactViewReadyCallback));
  163. }
  164. - (void)setReactViewReadyCallback:(RNNReactViewReadyCompletionBlock)reactViewReadyCallback {
  165. objc_setAssociatedObject(self, @selector(reactViewReadyCallback), reactViewReadyCallback, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  166. }
  167. - (BOOL)waitForRender {
  168. return [objc_getAssociatedObject(self.parentViewController ?: self, @selector(waitForRender)) boolValue];
  169. }
  170. - (void)setWaitForRender:(BOOL)waitForRender {
  171. objc_setAssociatedObject(self, @selector(waitForRender), [NSNumber numberWithBool:waitForRender], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  172. }
  173. @end