react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 6.5KB

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