react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 6.8KB

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