react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 6.1KB

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