react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. [((UISplitViewController *)self.parentViewController) onChildWillAppear];
  94. }
  95. - (void)willMoveToParentViewController:(UIViewController *)parent {
  96. if (parent) {
  97. [self.presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  98. }
  99. }
  100. #pragma mark getters and setters to associated object
  101. - (RNNNavigationOptions *)options {
  102. return objc_getAssociatedObject(self, @selector(options));
  103. }
  104. - (void)setOptions:(RNNNavigationOptions *)options {
  105. objc_setAssociatedObject(self, @selector(options), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  106. }
  107. - (RNNNavigationOptions *)defaultOptions {
  108. return objc_getAssociatedObject(self, @selector(defaultOptions));
  109. }
  110. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  111. objc_setAssociatedObject(self, @selector(defaultOptions), defaultOptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  112. }
  113. - (RNNLayoutInfo *)layoutInfo {
  114. return objc_getAssociatedObject(self, @selector(layoutInfo));
  115. }
  116. - (void)setLayoutInfo:(RNNLayoutInfo *)layoutInfo {
  117. objc_setAssociatedObject(self, @selector(layoutInfo), layoutInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  118. }
  119. - (RNNBasePresenter *)presenter {
  120. return objc_getAssociatedObject(self, @selector(presenter));
  121. }
  122. - (void)setPresenter:(RNNBasePresenter *)presenter {
  123. objc_setAssociatedObject(self, @selector(presenter), presenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  124. }
  125. - (RNNEventEmitter *)eventEmitter {
  126. return objc_getAssociatedObject(self, @selector(eventEmitter));
  127. }
  128. - (void)setEventEmitter:(RNNEventEmitter *)eventEmitter {
  129. objc_setAssociatedObject(self, @selector(eventEmitter), eventEmitter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  130. }
  131. - (id<RNNComponentViewCreator>)creator {
  132. return objc_getAssociatedObject(self, @selector(creator));
  133. }
  134. - (void)setCreator:(id<RNNComponentViewCreator>)creator {
  135. objc_setAssociatedObject(self, @selector(creator), creator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  136. }
  137. - (RNNReactViewReadyCompletionBlock)reactViewReadyCallback {
  138. return objc_getAssociatedObject(self, @selector(reactViewReadyCallback));
  139. }
  140. - (void)setReactViewReadyCallback:(RNNReactViewReadyCompletionBlock)reactViewReadyCallback {
  141. objc_setAssociatedObject(self, @selector(reactViewReadyCallback), reactViewReadyCallback, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  142. }
  143. - (BOOL)waitForRender {
  144. return [objc_getAssociatedObject(self.parentViewController ?: self, @selector(waitForRender)) boolValue];
  145. }
  146. - (void)setWaitForRender:(BOOL)waitForRender {
  147. objc_setAssociatedObject(self, @selector(waitForRender), [NSNumber numberWithBool:waitForRender], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  148. }
  149. @end