react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. - (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.getCurrentChild setReactViewReadyCallback:^{
  62. [self.presenter renderComponents:self.resolveOptionsWithDefault perform:^{
  63. [self readyForPresentation];
  64. }];
  65. }];
  66. [self.getCurrentChild 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. - (UIViewController *)findViewController:(UIViewController *)child {
  94. if (self == child) return child;
  95. for (UIViewController* childController in self.childViewControllers) {
  96. UIViewController* fromChild = [childController findViewController:child];
  97. if (fromChild) return childController;
  98. }
  99. return nil;
  100. }
  101. - (CGFloat)getTopBarHeight {
  102. for(UIViewController * child in [self childViewControllers]) {
  103. CGFloat childTopBarHeight = [child getTopBarHeight];
  104. if (childTopBarHeight > 0) return childTopBarHeight;
  105. }
  106. return 0;
  107. }
  108. - (CGFloat)getBottomTabsHeight {
  109. for(UIViewController * child in [self childViewControllers]) {
  110. CGFloat childBottomTabsHeight = [child getBottomTabsHeight];
  111. if (childBottomTabsHeight > 0) return childBottomTabsHeight;
  112. }
  113. return 0;
  114. }
  115. - (void)onChildWillAppear {
  116. [self.presenter applyOptions:self.resolveOptions];
  117. [self.parentViewController onChildWillAppear];
  118. }
  119. - (void)onChildAddToParent:(UIViewController *)child options:(RNNNavigationOptions *)options {
  120. [self.parentViewController onChildAddToParent:child options:options];
  121. }
  122. - (void)componentDidAppear {
  123. [self.presenter componentDidAppear];
  124. [self.parentViewController componentDidAppear];
  125. }
  126. - (void)componentDidDisappear {
  127. [self.presenter componentDidDisappear];
  128. [self.parentViewController componentDidDisappear];
  129. }
  130. - (void)willMoveToParentViewController:(UIViewController *)parent {
  131. if (parent) {
  132. [self.presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  133. [self onChildAddToParent:self options:self.resolveOptions];
  134. }
  135. }
  136. #pragma mark getters and setters to associated object
  137. - (RNNNavigationOptions *)options {
  138. return objc_getAssociatedObject(self, @selector(options));
  139. }
  140. - (void)setOptions:(RNNNavigationOptions *)options {
  141. objc_setAssociatedObject(self, @selector(options), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  142. }
  143. - (RNNNavigationOptions *)defaultOptions {
  144. return objc_getAssociatedObject(self, @selector(defaultOptions));
  145. }
  146. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  147. objc_setAssociatedObject(self, @selector(defaultOptions), defaultOptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  148. }
  149. - (RNNLayoutInfo *)layoutInfo {
  150. return objc_getAssociatedObject(self, @selector(layoutInfo));
  151. }
  152. - (void)setLayoutInfo:(RNNLayoutInfo *)layoutInfo {
  153. objc_setAssociatedObject(self, @selector(layoutInfo), layoutInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  154. }
  155. - (RNNBasePresenter *)presenter {
  156. return objc_getAssociatedObject(self, @selector(presenter));
  157. }
  158. - (void)setPresenter:(RNNBasePresenter *)presenter {
  159. objc_setAssociatedObject(self, @selector(presenter), presenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  160. }
  161. - (RNNEventEmitter *)eventEmitter {
  162. return objc_getAssociatedObject(self, @selector(eventEmitter));
  163. }
  164. - (void)setEventEmitter:(RNNEventEmitter *)eventEmitter {
  165. objc_setAssociatedObject(self, @selector(eventEmitter), eventEmitter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  166. }
  167. - (id<RNNComponentViewCreator>)creator {
  168. return objc_getAssociatedObject(self, @selector(creator));
  169. }
  170. - (void)setCreator:(id<RNNComponentViewCreator>)creator {
  171. objc_setAssociatedObject(self, @selector(creator), creator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  172. }
  173. - (RNNReactViewReadyCompletionBlock)reactViewReadyCallback {
  174. return objc_getAssociatedObject(self, @selector(reactViewReadyCallback));
  175. }
  176. - (void)setReactViewReadyCallback:(RNNReactViewReadyCompletionBlock)reactViewReadyCallback {
  177. objc_setAssociatedObject(self, @selector(reactViewReadyCallback), reactViewReadyCallback, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  178. }
  179. - (BOOL)waitForRender {
  180. return [objc_getAssociatedObject(self.parentViewController ?: self, @selector(waitForRender)) boolValue];
  181. }
  182. - (void)setWaitForRender:(BOOL)waitForRender {
  183. objc_setAssociatedObject(self, @selector(waitForRender), [NSNumber numberWithBool:waitForRender], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  184. }
  185. @end