-
-
Notifications
You must be signed in to change notification settings - Fork 643
Expand file tree
/
Copy pathRNSTabsBottomAccessoryHelper.mm
More file actions
238 lines (198 loc) · 7.66 KB
/
RNSTabsBottomAccessoryHelper.mm
File metadata and controls
238 lines (198 loc) · 7.66 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#import "RNSTabsBottomAccessoryHelper.h"
#import "RNSTabsBottomAccessoryShadowStateProxy.h"
#if RNS_TABS_BOTTOM_ACCESSORY_AVAILABLE
#import <React/RCTAssert.h>
#import <cxxreact/ReactNativeVersion.h>
namespace react = facebook::react;
static void *RNSTabsBottomAccessoryNativeWrapperViewContext = &RNSTabsBottomAccessoryNativeWrapperViewContext;
@implementation RNSTabsBottomAccessoryHelper {
RNSTabsBottomAccessoryComponentView *__weak _bottomAccessoryView;
UIView *__weak _observedNativeWrapperView;
#if REACT_NATIVE_VERSION_MINOR < 82
BOOL _initialStateUpdateSent;
CADisplayLink *_displayLink;
#else // REACT_NATIVE_VERSION_MINOR < 82
RNSTabsBottomAccessoryContentComponentView *__weak _regularContentView;
RNSTabsBottomAccessoryContentComponentView *__weak _inlineContentView;
#endif // REACT_NATIVE_VERSION_MINOR < 82
id<UITraitChangeRegistration> _traitChangeRegistration;
}
- (instancetype)initWithBottomAccessoryView:(RNSTabsBottomAccessoryComponentView *)bottomAccessoryView
{
if (self = [super init]) {
_bottomAccessoryView = bottomAccessoryView;
[self initState];
_traitChangeRegistration = [self registerForAccessoryEnvironmentChanges];
}
return self;
}
- (void)initState
{
_observedNativeWrapperView = nil;
#if REACT_NATIVE_VERSION_MINOR < 82
_initialStateUpdateSent = NO;
_displayLink = nil;
#else // REACT_NATIVE_VERSION_MINOR < 82
_regularContentView = nil;
_inlineContentView = nil;
#endif // REACT_NATIVE_VERSION_MINOR < 82
}
#pragma mark - Content view switching workaround
#if REACT_NATIVE_VERSION_MINOR >= 82
- (BOOL)isContentViewSwitchingWorkaroundActive
{
return _regularContentView != nil && _inlineContentView != nil;
}
- (void)setContentView:(RNSTabsBottomAccessoryContentComponentView *)contentView
forEnvironment:(RNSTabsBottomAccessoryEnvironment)environment
{
switch (environment) {
case RNSTabsBottomAccessoryEnvironmentRegular:
_regularContentView = contentView;
break;
case RNSTabsBottomAccessoryEnvironmentInline:
_inlineContentView = contentView;
break;
default:
RCTLogError(@"[RNScreens] Unsupported TabsBottomAccessory environment");
}
[self handleContentViewVisibilityForEnvironmentIfNeeded];
}
- (void)handleContentViewVisibilityForEnvironmentIfNeeded
{
if (!self.isContentViewSwitchingWorkaroundActive) {
return;
}
switch (self->_bottomAccessoryView.traitCollection.tabAccessoryEnvironment) {
case UITabAccessoryEnvironmentInline:
_regularContentView.layer.opacity = 0.0;
_inlineContentView.layer.opacity = 1.0;
break;
default:
_regularContentView.layer.opacity = 1.0;
_inlineContentView.layer.opacity = 0.0;
break;
}
}
#endif // REACT_NATIVE_VERSION_MINOR >= 82
#pragma mark - Observing environment changes
- (id<UITraitChangeRegistration>)registerForAccessoryEnvironmentChanges
{
return [_bottomAccessoryView
registerForTraitChanges:@[ [UITraitTabAccessoryEnvironment class] ]
withHandler:^(__kindof id<UITraitEnvironment>, UITraitCollection *previousTraitCollection) {
UITabAccessoryEnvironment environment =
self->_bottomAccessoryView.traitCollection.tabAccessoryEnvironment;
[self->_bottomAccessoryView.reactEventEmitter emitOnEnvironmentChangeIfNeeded:environment];
#if REACT_NATIVE_VERSION_MINOR >= 82
[self handleContentViewVisibilityForEnvironmentIfNeeded];
#endif // REACT_NATIVE_VERSION_MINOR >= 82
}];
}
#pragma mark - Observing frame changes
- (void)unregisterForAccessoryFrameChanges
{
UIView *observedNativeWrapperView = _observedNativeWrapperView;
if (observedNativeWrapperView == nil) {
return;
}
[observedNativeWrapperView removeObserver:self
forKeyPath:@"center"
context:RNSTabsBottomAccessoryNativeWrapperViewContext];
_observedNativeWrapperView = nil;
}
- (void)registerForAccessoryFrameChanges
{
UIView *nativeWrapperView = self.nativeWrapperView;
if (_observedNativeWrapperView == nativeWrapperView) {
return;
}
[self unregisterForAccessoryFrameChanges];
[nativeWrapperView addObserver:self
forKeyPath:@"center"
options:NSKeyValueObservingOptionInitial
context:RNSTabsBottomAccessoryNativeWrapperViewContext];
_observedNativeWrapperView = nativeWrapperView;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (context == RNSTabsBottomAccessoryNativeWrapperViewContext) {
[self notifyWrapperViewFrameHasChanged];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (UIView *)nativeWrapperView
{
RCTAssert(
_bottomAccessoryView.superview.superview != nil,
@"[RNScreens] RNSTabsBottomAccessoryComponentView must be the set as bottom accessory.");
return _bottomAccessoryView.superview.superview;
}
- (void)notifyWrapperViewFrameHasChanged
{
#if REACT_NATIVE_VERSION_MINOR < 82
// Make sure that bottom accessory's size is sent to ShadowNode as soon as possible.
// We set origin to (0,0) because initially self.nativeWrapperView's origin is incorrect.
// We want the enable the display link as well so that it takes over later with correct origin.
if (!_initialStateUpdateSent) {
CGRect frame = CGRectMake(0, 0, self.nativeWrapperView.frame.size.width, self.nativeWrapperView.frame.size.height);
[_bottomAccessoryView.shadowStateProxy updateShadowStateWithFrame:frame];
_initialStateUpdateSent = YES;
}
if (_displayLink == nil) {
[self setupDisplayLink];
}
#else // REACT_NATIVE_VERSION_MINOR < 82
// We use self.nativeWrapperView because it has both the size and the origin
// that we want to send to the ShadowNode.
[_bottomAccessoryView.shadowStateProxy updateShadowStateWithFrame:self.nativeWrapperView.frame];
#endif // REACT_NATIVE_VERSION_MINOR < 82
}
#if REACT_NATIVE_VERSION_MINOR < 82
- (void)setupDisplayLink
{
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)handleDisplayLink:(CADisplayLink *)sender
{
// We use self.nativeWrapperView because it has both the size and the origin
// that we want to send to the ShadowNode.
CGRect presentationFrame = self.nativeWrapperView.layer.presentationLayer.frame;
if (CGRectEqualToRect(presentationFrame, CGRectZero)) {
return;
}
[_bottomAccessoryView.shadowStateProxy updateShadowStateWithFrame:presentationFrame];
// self.nativeWrapperView.frame is set to final value at the beginning of the transition.
// When frame from presentation layer matches self.nativeWrapperView.frame, it indicates that
// the transition is over and we can disable the display link.
if (CGRectEqualToRect(presentationFrame, self.nativeWrapperView.frame)) {
[self invalidateDisplayLink];
}
}
- (void)invalidateDisplayLink
{
[_displayLink invalidate];
_displayLink = nil;
}
#endif // REACT_NATIVE_VERSION_MINOR < 82
#pragma mark - Invalidation
- (void)invalidate
{
[_bottomAccessoryView unregisterForTraitChanges:_traitChangeRegistration];
_traitChangeRegistration = nil;
[self unregisterForAccessoryFrameChanges];
_bottomAccessoryView = nil;
#if REACT_NATIVE_VERSION_MINOR < 82
[self invalidateDisplayLink];
#else // REACT_NATIVE_VERSION_MINOR < 82
_regularContentView = nil;
_inlineContentView = nil;
#endif // REACT_NATIVE_VERSION_MINOR < 82
}
@end
#endif // RNS_TABS_BOTTOM_ACCESSORY_AVAILABLE