react-native-navigation的迁移库

UIViewController+LayoutProtocol.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. self = [self init];
  11. self.options = options;
  12. self.defaultOptions = defaultOptions;
  13. self.layoutInfo = layoutInfo;
  14. self.creator = creator;
  15. self.eventEmitter = eventEmitter;
  16. self.presenter = presenter;
  17. [self.presenter bindViewController:self];
  18. [self.presenter applyOptionsOnInit:self.options];
  19. return self;
  20. }
  21. - (RNNNavigationOptions *)resolveOptions {
  22. return [(RNNNavigationOptions *)[self.options mergeInOptions:self.getCurrentChild.resolveOptions.copy] withDefault:self.defaultOptions];
  23. }
  24. - (void)mergeOptions:(RNNNavigationOptions *)options {
  25. [self.presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
  26. [((UIViewController *)self.parentViewController) mergeOptions:options];
  27. }
  28. - (void)overrideOptions:(RNNNavigationOptions *)options {
  29. [self.options overrideOptions:options];
  30. }
  31. - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  32. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  33. dispatch_group_t group = dispatch_group_create();
  34. for (UIViewController* childViewController in self.childViewControllers) {
  35. dispatch_group_enter(group);
  36. dispatch_async(dispatch_get_main_queue(), ^{
  37. [childViewController renderTreeAndWait:wait perform:^{
  38. dispatch_group_leave(group);
  39. }];
  40. });
  41. }
  42. dispatch_group_enter(group);
  43. [self.presenter renderComponents:self.resolveOptions perform:^{
  44. dispatch_group_leave(group);
  45. }];
  46. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  47. dispatch_async(dispatch_get_main_queue(), ^{
  48. readyBlock();
  49. });
  50. });
  51. }
  52. - (UIViewController *)getCurrentChild {
  53. return nil;
  54. }
  55. - (void)onChildWillAppear {
  56. [self.presenter applyOptions:self.resolveOptions];
  57. [((UISplitViewController *)self.parentViewController) onChildWillAppear];
  58. }
  59. - (void)willMoveToParentViewController:(UIViewController *)parent {
  60. if (parent) {
  61. [self.presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  62. }
  63. }
  64. #pragma mark getters and setters to associated object
  65. - (RNNNavigationOptions *)options {
  66. return objc_getAssociatedObject(self, @selector(options));
  67. }
  68. - (void)setOptions:(RNNNavigationOptions *)options {
  69. objc_setAssociatedObject(self, @selector(options), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  70. }
  71. - (RNNNavigationOptions *)defaultOptions {
  72. return objc_getAssociatedObject(self, @selector(defaultOptions));
  73. }
  74. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  75. objc_setAssociatedObject(self, @selector(defaultOptions), defaultOptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  76. }
  77. - (RNNLayoutInfo *)layoutInfo {
  78. return objc_getAssociatedObject(self, @selector(layoutInfo));
  79. }
  80. - (void)setLayoutInfo:(RNNLayoutInfo *)layoutInfo {
  81. objc_setAssociatedObject(self, @selector(layoutInfo), layoutInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  82. }
  83. - (RNNBasePresenter *)presenter {
  84. return objc_getAssociatedObject(self, @selector(presenter));
  85. }
  86. - (void)setPresenter:(RNNBasePresenter *)presenter {
  87. objc_setAssociatedObject(self, @selector(presenter), presenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  88. }
  89. - (RNNEventEmitter *)eventEmitter {
  90. return objc_getAssociatedObject(self, @selector(eventEmitter));
  91. }
  92. - (void)setEventEmitter:(RNNEventEmitter *)eventEmitter {
  93. objc_setAssociatedObject(self, @selector(eventEmitter), eventEmitter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  94. }
  95. - (id<RNNRootViewCreator>)creator {
  96. return objc_getAssociatedObject(self, @selector(creator));
  97. }
  98. - (void)setCreator:(id<RNNRootViewCreator>)creator {
  99. objc_setAssociatedObject(self, @selector(creator), creator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  100. }
  101. @end