react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 boundViewController: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. - (void)overrideOptions:(RNNNavigationOptions *)options {
  38. [self.options overrideOptions:options];
  39. }
  40. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  41. UIInterfaceOrientationMask interfaceOrientationMask = self.presenter ? [self.presenter getOrientation:[self resolveOptions]] : [[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:[[UIApplication sharedApplication] keyWindow]];
  42. return interfaceOrientationMask;
  43. }
  44. - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  45. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  46. dispatch_group_t group = dispatch_group_create();
  47. for (UIViewController* childViewController in self.childViewControllers) {
  48. dispatch_group_enter(group);
  49. dispatch_async(dispatch_get_main_queue(), ^{
  50. [childViewController renderTreeAndWait:wait perform:^{
  51. dispatch_group_leave(group);
  52. }];
  53. });
  54. }
  55. dispatch_group_enter(group);
  56. [self.presenter renderComponents:self.resolveOptions perform:^{
  57. dispatch_group_leave(group);
  58. }];
  59. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  60. dispatch_async(dispatch_get_main_queue(), ^{
  61. readyBlock();
  62. });
  63. });
  64. }
  65. - (UIViewController *)getCurrentChild {
  66. return nil;
  67. }
  68. - (CGFloat)getTopBarHeight {
  69. for(UIViewController * child in [self childViewControllers]) {
  70. CGFloat childTopBarHeight = [child getTopBarHeight];
  71. if (childTopBarHeight > 0) return childTopBarHeight;
  72. }
  73. return 0;
  74. }
  75. - (CGFloat)getBottomTabsHeight {
  76. for(UIViewController * child in [self childViewControllers]) {
  77. CGFloat childBottomTabsHeight = [child getBottomTabsHeight];
  78. if (childBottomTabsHeight > 0) return childBottomTabsHeight;
  79. }
  80. return 0;
  81. }
  82. - (void)onChildWillAppear {
  83. [self.presenter applyOptions:self.resolveOptions];
  84. [((UISplitViewController *)self.parentViewController) onChildWillAppear];
  85. }
  86. - (void)willMoveToParentViewController:(UIViewController *)parent {
  87. if (parent) {
  88. [self.presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  89. }
  90. }
  91. #pragma mark getters and setters to associated object
  92. - (RNNNavigationOptions *)options {
  93. return objc_getAssociatedObject(self, @selector(options));
  94. }
  95. - (void)setOptions:(RNNNavigationOptions *)options {
  96. objc_setAssociatedObject(self, @selector(options), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  97. }
  98. - (RNNNavigationOptions *)defaultOptions {
  99. return objc_getAssociatedObject(self, @selector(defaultOptions));
  100. }
  101. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  102. objc_setAssociatedObject(self, @selector(defaultOptions), defaultOptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  103. }
  104. - (RNNLayoutInfo *)layoutInfo {
  105. return objc_getAssociatedObject(self, @selector(layoutInfo));
  106. }
  107. - (void)setLayoutInfo:(RNNLayoutInfo *)layoutInfo {
  108. objc_setAssociatedObject(self, @selector(layoutInfo), layoutInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  109. }
  110. - (RNNBasePresenter *)presenter {
  111. return objc_getAssociatedObject(self, @selector(presenter));
  112. }
  113. - (void)setPresenter:(RNNBasePresenter *)presenter {
  114. objc_setAssociatedObject(self, @selector(presenter), presenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  115. }
  116. - (RNNEventEmitter *)eventEmitter {
  117. return objc_getAssociatedObject(self, @selector(eventEmitter));
  118. }
  119. - (void)setEventEmitter:(RNNEventEmitter *)eventEmitter {
  120. objc_setAssociatedObject(self, @selector(eventEmitter), eventEmitter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  121. }
  122. - (id<RNNComponentViewCreator>)creator {
  123. return objc_getAssociatedObject(self, @selector(creator));
  124. }
  125. - (void)setCreator:(id<RNNComponentViewCreator>)creator {
  126. objc_setAssociatedObject(self, @selector(creator), creator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  127. }
  128. @end