react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 6.9KB

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