react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. return 0;
  70. }
  71. - (void)onChildWillAppear {
  72. [self.presenter applyOptions:self.resolveOptions];
  73. [((UISplitViewController *)self.parentViewController) onChildWillAppear];
  74. }
  75. - (void)willMoveToParentViewController:(UIViewController *)parent {
  76. if (parent) {
  77. [self.presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  78. }
  79. }
  80. #pragma mark getters and setters to associated object
  81. - (RNNNavigationOptions *)options {
  82. return objc_getAssociatedObject(self, @selector(options));
  83. }
  84. - (void)setOptions:(RNNNavigationOptions *)options {
  85. objc_setAssociatedObject(self, @selector(options), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  86. }
  87. - (RNNNavigationOptions *)defaultOptions {
  88. return objc_getAssociatedObject(self, @selector(defaultOptions));
  89. }
  90. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  91. objc_setAssociatedObject(self, @selector(defaultOptions), defaultOptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  92. }
  93. - (RNNLayoutInfo *)layoutInfo {
  94. return objc_getAssociatedObject(self, @selector(layoutInfo));
  95. }
  96. - (void)setLayoutInfo:(RNNLayoutInfo *)layoutInfo {
  97. objc_setAssociatedObject(self, @selector(layoutInfo), layoutInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  98. }
  99. - (RNNBasePresenter *)presenter {
  100. return objc_getAssociatedObject(self, @selector(presenter));
  101. }
  102. - (void)setPresenter:(RNNBasePresenter *)presenter {
  103. objc_setAssociatedObject(self, @selector(presenter), presenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  104. }
  105. - (RNNEventEmitter *)eventEmitter {
  106. return objc_getAssociatedObject(self, @selector(eventEmitter));
  107. }
  108. - (void)setEventEmitter:(RNNEventEmitter *)eventEmitter {
  109. objc_setAssociatedObject(self, @selector(eventEmitter), eventEmitter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  110. }
  111. - (id<RNNComponentViewCreator>)creator {
  112. return objc_getAssociatedObject(self, @selector(creator));
  113. }
  114. - (void)setCreator:(id<RNNComponentViewCreator>)creator {
  115. objc_setAssociatedObject(self, @selector(creator), creator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  116. }
  117. @end