|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +#import "RCTSurfaceHostingComponentController.h" |
| 11 | + |
| 12 | +#import <ComponentKit/CKComponentSubclass.h> |
| 13 | +#import <React/RCTAssert.h> |
| 14 | +#import <React/RCTSurface.h> |
| 15 | +#import <React/RCTSurfaceDelegate.h> |
| 16 | +#import <React/RCTSurfaceView.h> |
| 17 | + |
| 18 | +#import "RCTSurfaceHostingComponent+Internal.h" |
| 19 | +#import "RCTSurfaceHostingComponent.h" |
| 20 | +#import "RCTSurfaceHostingComponentState.h" |
| 21 | + |
| 22 | +@interface RCTSurfaceHostingComponentController() <RCTSurfaceDelegate> |
| 23 | +@end |
| 24 | + |
| 25 | +@implementation RCTSurfaceHostingComponentController { |
| 26 | + RCTSurface *_surface; |
| 27 | +} |
| 28 | + |
| 29 | +- (instancetype)initWithComponent:(RCTSurfaceHostingComponent *)component |
| 30 | +{ |
| 31 | + if (self = [super initWithComponent:component]) { |
| 32 | + [self updateSurfaceWithComponent:component]; |
| 33 | + } |
| 34 | + |
| 35 | + return self; |
| 36 | +} |
| 37 | + |
| 38 | +#pragma mark - Lifecycle |
| 39 | + |
| 40 | +- (void)didMount |
| 41 | +{ |
| 42 | + [super didMount]; |
| 43 | + [self mountSurfaceView]; |
| 44 | +} |
| 45 | + |
| 46 | +- (void)didRemount |
| 47 | +{ |
| 48 | + [super didRemount]; |
| 49 | + [self mountSurfaceView]; |
| 50 | +} |
| 51 | + |
| 52 | +- (void)didUpdateComponent |
| 53 | +{ |
| 54 | + [super didUpdateComponent]; |
| 55 | + [self updateSurfaceWithComponent:(RCTSurfaceHostingComponent *)self.component]; |
| 56 | +} |
| 57 | + |
| 58 | +- (void)didUnmount |
| 59 | +{ |
| 60 | + [super didUnmount]; |
| 61 | + [self unmountSurfaceView]; |
| 62 | +} |
| 63 | + |
| 64 | +#pragma mark - Helpers |
| 65 | + |
| 66 | +- (void)updateSurfaceWithComponent:(RCTSurfaceHostingComponent *)component |
| 67 | +{ |
| 68 | + // Updating `surface` |
| 69 | + RCTSurface *const surface = component.surface; |
| 70 | + if (surface != _surface) { |
| 71 | + if (_surface.delegate == self) { |
| 72 | + _surface.delegate = nil; |
| 73 | + } |
| 74 | + |
| 75 | + _surface = surface; |
| 76 | + _surface.delegate = self; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +- (void)setIntrinsicSize:(CGSize)intrinsicSize |
| 81 | +{ |
| 82 | + [self.component updateState:^(RCTSurfaceHostingComponentState *state) { |
| 83 | + return [RCTSurfaceHostingComponentState newWithStage:state.stage |
| 84 | + intrinsicSize:intrinsicSize]; |
| 85 | + } mode:[self suitableStateUpdateMode]]; |
| 86 | +} |
| 87 | + |
| 88 | +- (void)setStage:(RCTSurfaceStage)stage |
| 89 | +{ |
| 90 | + [self.component updateState:^(RCTSurfaceHostingComponentState *state) { |
| 91 | + return [RCTSurfaceHostingComponentState newWithStage:stage |
| 92 | + intrinsicSize:state.intrinsicSize]; |
| 93 | + } mode:[self suitableStateUpdateMode]]; |
| 94 | +} |
| 95 | + |
| 96 | +- (CKUpdateMode)suitableStateUpdateMode |
| 97 | +{ |
| 98 | + return ((RCTSurfaceHostingComponent *)self.component).options.synchronousStateUpdates && RCTIsMainQueue() ? CKUpdateModeSynchronous : CKUpdateModeAsynchronous; |
| 99 | +} |
| 100 | + |
| 101 | +- (void)mountSurfaceView |
| 102 | +{ |
| 103 | + UIView *const surfaceView = _surface.view; |
| 104 | + |
| 105 | + const CKComponentViewContext &context = [[self component] viewContext]; |
| 106 | + |
| 107 | + UIView *const superview = context.view; |
| 108 | + superview.clipsToBounds = YES; |
| 109 | + |
| 110 | + RCTAssert([superview.subviews count] <= 1, @"Should never have more than a single stateful subview."); |
| 111 | + |
| 112 | + UIView *const existingSurfaceView = [superview.subviews lastObject]; |
| 113 | + if (existingSurfaceView != surfaceView) { |
| 114 | + [existingSurfaceView removeFromSuperview]; |
| 115 | + surfaceView.frame = superview.bounds; |
| 116 | + surfaceView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
| 117 | + [superview addSubview:surfaceView]; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +- (void)unmountSurfaceView |
| 122 | +{ |
| 123 | + const CKComponentViewContext &context = [[self component] viewContext]; |
| 124 | + |
| 125 | + UIView *const superview = context.view; |
| 126 | + RCTAssert([superview.subviews count] <= 1, @"Should never have more than a single stateful subview."); |
| 127 | + UIView *const existingSurfaceView = [superview.subviews lastObject]; |
| 128 | + [existingSurfaceView removeFromSuperview]; |
| 129 | +} |
| 130 | + |
| 131 | +#pragma mark - RCTSurfaceDelegate |
| 132 | + |
| 133 | +- (void)surface:(RCTSurface *)surface didChangeIntrinsicSize:(CGSize)intrinsicSize |
| 134 | +{ |
| 135 | + [self setIntrinsicSize:intrinsicSize]; |
| 136 | +} |
| 137 | + |
| 138 | +- (void)surface:(RCTSurface *)surface didChangeStage:(RCTSurfaceStage)stage |
| 139 | +{ |
| 140 | + [self setStage:stage]; |
| 141 | +} |
| 142 | + |
| 143 | +@end |
0 commit comments