react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 4.6KB

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