react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. self.extendedLayoutIncludesOpaqueBars = YES;
  20. if ([self respondsToSelector:@selector(setViewControllers:)]) {
  21. [self performSelector:@selector(setViewControllers:) withObject:childViewControllers];
  22. }
  23. [self.presenter applyOptionsOnInit:self.resolveOptions];
  24. return self;
  25. }
  26. - (void)mergeOptions:(RNNNavigationOptions *)options {
  27. [self.options overrideOptions:options];
  28. [self.presenter mergeOptions:options resolvedOptions:self.resolveOptions];
  29. [self.parentViewController mergeChildOptions:options child:self];
  30. }
  31. - (void)mergeChildOptions:(RNNNavigationOptions *)options child:(UIViewController *)child {
  32. [self.presenter mergeOptions:options resolvedOptions:self.resolveOptions];
  33. [self.parentViewController mergeChildOptions:options child:child];
  34. }
  35. - (RNNNavigationOptions *)resolveOptions {
  36. return (RNNNavigationOptions *) [self.options mergeInOptions:self.getCurrentChild.resolveOptions.copy];
  37. }
  38. - (RNNNavigationOptions *)resolveOptionsWithDefault {
  39. return [(RNNNavigationOptions *) [self.options mergeInOptions:self.getCurrentChild.resolveOptions.copy] withDefault:self.defaultOptions];
  40. }
  41. - (void)overrideOptions:(RNNNavigationOptions *)options {
  42. [self.options overrideOptions:options];
  43. }
  44. - (UINavigationController *)stack {
  45. if ([self isKindOfClass:UINavigationController.class]) {
  46. return (UINavigationController *)self;
  47. } else {
  48. return self.navigationController;
  49. }
  50. }
  51. - (void)render {
  52. if (!self.waitForRender) {
  53. [self readyForPresentation];
  54. }
  55. [self.getCurrentChild setReactViewReadyCallback:^{
  56. [self.presenter renderComponents:self.resolveOptionsWithDefault perform:^{
  57. [self readyForPresentation];
  58. }];
  59. }];
  60. [self.getCurrentChild render];
  61. }
  62. - (void)readyForPresentation {
  63. if (self.reactViewReadyCallback) {
  64. self.reactViewReadyCallback();
  65. self.reactViewReadyCallback = nil;
  66. }
  67. [self.parentViewController readyForPresentation];
  68. }
  69. - (UIViewController *)getCurrentChild {
  70. for (UIViewController* childViewController in self.childViewControllers.reverseObjectEnumerator.allObjects) {
  71. if (childViewController.layoutInfo) {
  72. return childViewController;
  73. }
  74. }
  75. return nil;
  76. }
  77. - (UIViewController *)presentedComponentViewController {
  78. UIViewController* currentChild = self.getCurrentChild;
  79. return currentChild ? currentChild.presentedComponentViewController : self;
  80. }
  81. - (UIViewController *)topMostViewController {
  82. if (self.parentViewController) {
  83. return [self.parentViewController topMostViewController];
  84. } else
  85. return self;
  86. }
  87. - (UIViewController *)findViewController:(UIViewController *)child {
  88. if (self == child) return child;
  89. for (UIViewController* childController in self.childViewControllers) {
  90. UIViewController* fromChild = [childController findViewController:child];
  91. if (fromChild) return childController;
  92. }
  93. return nil;
  94. }
  95. - (CGFloat)getTopBarHeight {
  96. for(UIViewController * child in [self childViewControllers]) {
  97. CGFloat childTopBarHeight = [child getTopBarHeight];
  98. if (childTopBarHeight > 0) return childTopBarHeight;
  99. }
  100. return 0;
  101. }
  102. - (CGFloat)getBottomTabsHeight {
  103. for(UIViewController * child in [self childViewControllers]) {
  104. CGFloat childBottomTabsHeight = [child getBottomTabsHeight];
  105. if (childBottomTabsHeight > 0) return childBottomTabsHeight;
  106. }
  107. return 0;
  108. }
  109. - (void)onChildWillAppear {
  110. [self.presenter applyOptions:self.resolveOptions];
  111. [self.parentViewController onChildWillAppear];
  112. }
  113. - (void)onChildAddToParent:(UIViewController *)child options:(RNNNavigationOptions *)options {
  114. [self.parentViewController onChildAddToParent:child options:options];
  115. }
  116. - (void)componentDidAppear {
  117. [self.presenter componentDidAppear];
  118. [self.parentViewController componentDidAppear];
  119. }
  120. - (void)componentDidDisappear {
  121. [self.presenter componentDidDisappear];
  122. [self.parentViewController componentDidDisappear];
  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