react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. self.presenter = presenter;
  18. [self.presenter bindViewController:self];
  19. if ([self respondsToSelector:@selector(setViewControllers:)]) {
  20. [self performSelector:@selector(setViewControllers:) withObject:childViewControllers];
  21. }
  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. - (BOOL)prefersStatusBarHidden {
  51. return [self.presenter isStatusBarVisibility:self.navigationController resolvedOptions:self.resolveOptions];
  52. }
  53. - (UINavigationController *)stack {
  54. if ([self isKindOfClass:UINavigationController.class]) {
  55. return (UINavigationController *)self;
  56. } else {
  57. return self.navigationController;
  58. }
  59. }
  60. - (void)render {
  61. if (!self.waitForRender) {
  62. [self readyForPresentation];
  63. }
  64. [self.getCurrentChild setReactViewReadyCallback:^{
  65. [self.presenter renderComponents:self.resolveOptionsWithDefault perform:^{
  66. [self readyForPresentation];
  67. }];
  68. }];
  69. [self.getCurrentChild render];
  70. }
  71. - (void)readyForPresentation {
  72. if (self.reactViewReadyCallback) {
  73. self.reactViewReadyCallback();
  74. self.reactViewReadyCallback = nil;
  75. }
  76. [self.parentViewController readyForPresentation];
  77. }
  78. - (UIViewController *)getCurrentChild {
  79. for (UIViewController* childViewController in self.childViewControllers.reverseObjectEnumerator.allObjects) {
  80. if (childViewController.layoutInfo) {
  81. return childViewController;
  82. }
  83. }
  84. return nil;
  85. }
  86. - (UIViewController *)presentedComponentViewController {
  87. UIViewController* currentChild = self.getCurrentChild;
  88. return currentChild ? currentChild.presentedComponentViewController : self;
  89. }
  90. - (UIViewController *)topMostViewController {
  91. if (self.parentViewController) {
  92. return [self.parentViewController topMostViewController];
  93. } else
  94. return self;
  95. }
  96. - (UIViewController *)findViewController:(UIViewController *)child {
  97. if (self == child) return child;
  98. for (UIViewController* childController in self.childViewControllers) {
  99. UIViewController* fromChild = [childController findViewController:child];
  100. if (fromChild) return childController;
  101. }
  102. return nil;
  103. }
  104. - (CGFloat)getTopBarHeight {
  105. for(UIViewController * child in [self childViewControllers]) {
  106. CGFloat childTopBarHeight = [child getTopBarHeight];
  107. if (childTopBarHeight > 0) return childTopBarHeight;
  108. }
  109. return 0;
  110. }
  111. - (CGFloat)getBottomTabsHeight {
  112. for(UIViewController * child in [self childViewControllers]) {
  113. CGFloat childBottomTabsHeight = [child getBottomTabsHeight];
  114. if (childBottomTabsHeight > 0) return childBottomTabsHeight;
  115. }
  116. return 0;
  117. }
  118. - (void)onChildWillAppear {
  119. [self.presenter applyOptions:self.resolveOptions];
  120. [self.parentViewController onChildWillAppear];
  121. }
  122. - (void)onChildAddToParent:(UIViewController *)child options:(RNNNavigationOptions *)options {
  123. [self.parentViewController onChildAddToParent:child options:options];
  124. }
  125. - (void)componentDidAppear {
  126. [self.presenter componentDidAppear];
  127. [self.parentViewController componentDidAppear];
  128. }
  129. - (void)componentDidDisappear {
  130. [self.presenter componentDidDisappear];
  131. [self.parentViewController componentDidDisappear];
  132. }
  133. - (void)willMoveToParentViewController:(UIViewController *)parent {
  134. if (parent) {
  135. [self.presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  136. [self onChildAddToParent:self options:self.resolveOptions];
  137. }
  138. }
  139. #pragma mark getters and setters to associated object
  140. - (RNNNavigationOptions *)options {
  141. return objc_getAssociatedObject(self, @selector(options));
  142. }
  143. - (void)setOptions:(RNNNavigationOptions *)options {
  144. objc_setAssociatedObject(self, @selector(options), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  145. }
  146. - (RNNNavigationOptions *)defaultOptions {
  147. return objc_getAssociatedObject(self, @selector(defaultOptions));
  148. }
  149. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  150. objc_setAssociatedObject(self, @selector(defaultOptions), defaultOptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  151. }
  152. - (RNNLayoutInfo *)layoutInfo {
  153. return objc_getAssociatedObject(self, @selector(layoutInfo));
  154. }
  155. - (void)setLayoutInfo:(RNNLayoutInfo *)layoutInfo {
  156. objc_setAssociatedObject(self, @selector(layoutInfo), layoutInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  157. }
  158. - (RNNBasePresenter *)presenter {
  159. return objc_getAssociatedObject(self, @selector(presenter));
  160. }
  161. - (void)setPresenter:(RNNBasePresenter *)presenter {
  162. objc_setAssociatedObject(self, @selector(presenter), presenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  163. }
  164. - (RNNEventEmitter *)eventEmitter {
  165. return objc_getAssociatedObject(self, @selector(eventEmitter));
  166. }
  167. - (void)setEventEmitter:(RNNEventEmitter *)eventEmitter {
  168. objc_setAssociatedObject(self, @selector(eventEmitter), eventEmitter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  169. }
  170. - (id<RNNComponentViewCreator>)creator {
  171. return objc_getAssociatedObject(self, @selector(creator));
  172. }
  173. - (void)setCreator:(id<RNNComponentViewCreator>)creator {
  174. objc_setAssociatedObject(self, @selector(creator), creator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  175. }
  176. - (RNNReactViewReadyCompletionBlock)reactViewReadyCallback {
  177. return objc_getAssociatedObject(self, @selector(reactViewReadyCallback));
  178. }
  179. - (void)setReactViewReadyCallback:(RNNReactViewReadyCompletionBlock)reactViewReadyCallback {
  180. objc_setAssociatedObject(self, @selector(reactViewReadyCallback), reactViewReadyCallback, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  181. }
  182. - (BOOL)waitForRender {
  183. return [objc_getAssociatedObject(self.parentViewController ?: self, @selector(waitForRender)) boolValue];
  184. }
  185. - (void)setWaitForRender:(BOOL)waitForRender {
  186. objc_setAssociatedObject(self, @selector(waitForRender), [NSNumber numberWithBool:waitForRender], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  187. }
  188. @end