react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #import "UIViewController+LayoutProtocol.h"
  2. #import <objc/runtime.h>
  3. @implementation UIViewController (LayoutProtocol)
  4. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
  5. creator:(id<RNNRootViewCreator>)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. - (RNNNavigationOptions *)resolveOptions {
  26. return [(RNNNavigationOptions *)[self.options mergeInOptions:self.getCurrentChild.resolveOptions.copy] withDefault:self.defaultOptions];
  27. }
  28. - (RNNNavigationOptions *)resolveChildOptions:(UIViewController *) child {
  29. return [(RNNNavigationOptions *)[self.options mergeInOptions:child.resolveOptions.copy] withDefault:self.defaultOptions];
  30. }
  31. - (void)mergeOptions:(RNNNavigationOptions *)options {
  32. [self.presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
  33. [self.parentViewController mergeOptions:options];
  34. }
  35. - (void)overrideOptions:(RNNNavigationOptions *)options {
  36. [self.options overrideOptions:options];
  37. }
  38. - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  39. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  40. dispatch_group_t group = dispatch_group_create();
  41. for (UIViewController* childViewController in self.childViewControllers) {
  42. dispatch_group_enter(group);
  43. dispatch_async(dispatch_get_main_queue(), ^{
  44. [childViewController renderTreeAndWait:wait perform:^{
  45. dispatch_group_leave(group);
  46. }];
  47. });
  48. }
  49. dispatch_group_enter(group);
  50. [self.presenter renderComponents:self.resolveOptions perform:^{
  51. dispatch_group_leave(group);
  52. }];
  53. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  54. dispatch_async(dispatch_get_main_queue(), ^{
  55. readyBlock();
  56. });
  57. });
  58. }
  59. - (UIViewController *)getCurrentChild {
  60. return nil;
  61. }
  62. - (CGFloat)getTopBarHeight {
  63. return 0;
  64. }
  65. - (void)onChildWillAppear {
  66. [self.presenter applyOptions:self.resolveOptions];
  67. [((UISplitViewController *)self.parentViewController) onChildWillAppear];
  68. }
  69. - (void)willMoveToParentViewController:(UIViewController *)parent {
  70. if (parent) {
  71. [self.presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  72. }
  73. }
  74. #pragma mark getters and setters to associated object
  75. - (RNNNavigationOptions *)options {
  76. return objc_getAssociatedObject(self, @selector(options));
  77. }
  78. - (void)setOptions:(RNNNavigationOptions *)options {
  79. objc_setAssociatedObject(self, @selector(options), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  80. }
  81. - (RNNNavigationOptions *)defaultOptions {
  82. return objc_getAssociatedObject(self, @selector(defaultOptions));
  83. }
  84. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  85. objc_setAssociatedObject(self, @selector(defaultOptions), defaultOptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  86. }
  87. - (RNNLayoutInfo *)layoutInfo {
  88. return objc_getAssociatedObject(self, @selector(layoutInfo));
  89. }
  90. - (void)setLayoutInfo:(RNNLayoutInfo *)layoutInfo {
  91. objc_setAssociatedObject(self, @selector(layoutInfo), layoutInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  92. }
  93. - (RNNBasePresenter *)presenter {
  94. return objc_getAssociatedObject(self, @selector(presenter));
  95. }
  96. - (void)setPresenter:(RNNBasePresenter *)presenter {
  97. objc_setAssociatedObject(self, @selector(presenter), presenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  98. }
  99. - (RNNEventEmitter *)eventEmitter {
  100. return objc_getAssociatedObject(self, @selector(eventEmitter));
  101. }
  102. - (void)setEventEmitter:(RNNEventEmitter *)eventEmitter {
  103. objc_setAssociatedObject(self, @selector(eventEmitter), eventEmitter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  104. }
  105. - (id<RNNRootViewCreator>)creator {
  106. return objc_getAssociatedObject(self, @selector(creator));
  107. }
  108. - (void)setCreator:(id<RNNRootViewCreator>)creator {
  109. objc_setAssociatedObject(self, @selector(creator), creator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  110. }
  111. @end