-
-
Notifications
You must be signed in to change notification settings - Fork 632
Expand file tree
/
Copy pathRNSStackHostComponentView.mm
More file actions
153 lines (129 loc) · 5.16 KB
/
RNSStackHostComponentView.mm
File metadata and controls
153 lines (129 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#import "RNSStackHostComponentView.h"
#import <React/RCTConversions.h>
#import <React/RCTMountingTransactionObserving.h>
#import <React/UIView+React.h>
#import <react/renderer/components/rnscreens/ComponentDescriptors.h>
#import <react/renderer/components/rnscreens/EventEmitters.h>
#import <react/renderer/components/rnscreens/Props.h>
#import <react/renderer/components/rnscreens/RCTComponentViewHelpers.h>
#import "RNSDefines.h"
#import "RNSLog.h"
#import "RNSStackNavigationController.h"
#import "RNSStackOperationCoordinator.h"
#import "RNSStackScreenComponentView.h"
#import "Swift-Bridging.h"
namespace react = facebook::react;
@interface RNSStackHostComponentView () <RCTMountingTransactionObserving>
@end
@implementation RNSStackHostComponentView {
RNSStackNavigationController *_Nonnull _stackNavigationController;
RNSStackOperationCoordinator *_Nonnull _stackOperationCoordinator;
NSMutableArray<RNSStackScreenComponentView *> *_Nonnull _renderedScreens;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self initState];
}
return self;
}
- (void)initState
{
_stackNavigationController = [RNSStackNavigationController new];
_stackOperationCoordinator = [RNSStackOperationCoordinator new];
_renderedScreens = [NSMutableArray new];
}
- (void)didMoveToWindow
{
RNSLog(@"[RNScreens] StackHost [%ld] attached to window", self.tag);
[self reactAddControllerToClosestParent:_stackNavigationController];
}
- (void)reactAddControllerToClosestParent:(nonnull UIViewController *)controller
{
if (!controller.parentViewController) {
UIView *parentView = (UIView *)self.reactSuperview;
while (parentView) {
if (parentView.reactViewController) {
[parentView.reactViewController addChildViewController:controller];
[self addSubview:controller.view];
[controller didMoveToParentViewController:parentView.reactViewController];
break;
}
parentView = (UIView *)parentView.reactSuperview;
}
return;
}
}
#pragma mark - Communication with StackScreen
- (void)stackScreenChangedActivityMode:(nonnull RNSStackScreenComponentView *)stackScreen
{
switch (stackScreen.activityMode) {
case RNSStackScreenActivityModeAttached:
[_stackOperationCoordinator addPushOperation:stackScreen];
break;
case RNSStackScreenActivityModeDetached:
[_stackOperationCoordinator addPopOperation:stackScreen];
break;
}
}
#pragma mark - RCTComponentViewProtocol
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
RCTAssert([childComponentView isKindOfClass:RNSStackScreenComponentView.class],
@"[RNScreens] Attempt to mount child of unsupported type: %@, expected %@",
childComponentView.class,
RNSStackScreenComponentView.class);
auto *childScreen = static_cast<RNSStackScreenComponentView *>(childComponentView);
childScreen.stackHost = self;
[_renderedScreens insertObject:childScreen atIndex:index];
[self addPushOperationIfNeeded:childScreen];
}
- (void)addPushOperationIfNeeded:(nonnull RNSStackScreenComponentView *)stackScreen
{
if (stackScreen.activityMode == RNSStackScreenActivityModeAttached) {
[_stackOperationCoordinator addPushOperation:stackScreen];
}
}
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
RCTAssert([childComponentView isKindOfClass:RNSStackScreenComponentView.class],
@"[RNScreens] Attempt to unmount child of unsupported type: %@, expected %@",
childComponentView.class,
RNSStackScreenComponentView.class);
auto *childScreen = static_cast<RNSStackScreenComponentView *>(childComponentView);
[_renderedScreens removeObject:childScreen];
childScreen.stackHost = nil;
[self addPopOperationIfNeeded:childScreen];
}
- (void)addPopOperationIfNeeded:(nonnull RNSStackScreenComponentView *)stackScreen
{
if (stackScreen.activityMode == RNSStackScreenActivityModeAttached && !stackScreen.isNativelyDismissed) {
// This shouldn't happen in typical scenarios but it can happen with fast-refresh.
[_stackOperationCoordinator addPopOperation:stackScreen];
} else {
RNSLog(@"[RNScreens] ignoring pop operation of %@, already not attached or natively dismissed",
stackScreen.screenKey);
}
}
+ (react::ComponentDescriptorProvider)componentDescriptorProvider
{
return react::concreteComponentDescriptorProvider<react::RNSStackHostComponentDescriptor>();
}
+ (BOOL)shouldBeRecycled
{
// There won't be tens of instances of this component usually & it's easier for now.
// We could consider enabling it someday though.
return NO;
}
#pragma mark - RCTMountingTransactionObserving
- (void)mountingTransactionDidMount:(const facebook::react::MountingTransaction &)transaction
withSurfaceTelemetry:(const facebook::react::SurfaceTelemetry &)surfaceTelemetry
{
[_stackOperationCoordinator executePendingOperationsIfNeeded:_stackNavigationController
withRenderedScreens:_renderedScreens];
}
@end
Class<RCTComponentViewProtocol> RNSStackHostCls(void)
{
return RNSStackHostComponentView.class;
}