/
RNCSafeAreaView.m
101 lines (78 loc) · 2.55 KB
/
RNCSafeAreaView.m
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
#import "RNCSafeAreaView.h"
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
#import "RNCSafeAreaViewLocalData.h"
#import "RNCSafeAreaViewEdges.h"
#import "RCTView+SafeAreaCompat.h"
@implementation RNCSafeAreaView {
__weak RCTBridge *_bridge;
UIEdgeInsets _currentSafeAreaInsets;
RNCSafeAreaViewEdges _edges;
}
- (instancetype)initWithBridge:(RCTBridge *)bridge
{
if (self = [super initWithFrame:CGRectZero]) {
_bridge = bridge;
// Defaults
_emulateUnlessSupported = YES;
_edges = RNCSafeAreaViewEdgesAll;
}
return self;
}
RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)decoder)
RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
- (NSString *)description
{
NSString *superDescription = [super description];
// Cutting the last `>` character.
if (superDescription.length > 0 && [superDescription characterAtIndex:superDescription.length - 1] == '>') {
superDescription = [superDescription substringToIndex:superDescription.length - 1];
}
return [NSString stringWithFormat:@"%@; RNCSafeAreaInsets = %@; appliedRNCSafeAreaInsets = %@>",
superDescription,
NSStringFromUIEdgeInsets([self realOrEmulateSafeAreaInsets:self.emulateUnlessSupported]),
NSStringFromUIEdgeInsets(_currentSafeAreaInsets)];
}
- (void)safeAreaInsetsDidChange
{
[super safeAreaInsetsDidChange];
[self invalidateSafeAreaInsets];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (!self.nativeSafeAreaSupport && self.emulateUnlessSupported) {
[self invalidateSafeAreaInsets];
}
}
- (void)invalidateSafeAreaInsets
{
UIEdgeInsets safeAreaInsets = [self realOrEmulateSafeAreaInsets:self.emulateUnlessSupported];
if (UIEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
return;
}
_currentSafeAreaInsets = safeAreaInsets;
[self updateLocalData];
}
- (void)updateLocalData
{
RNCSafeAreaViewLocalData *localData = [[RNCSafeAreaViewLocalData alloc] initWithInsets:_currentSafeAreaInsets
edges:_edges];
[_bridge.uiManager setLocalData:localData forView:self];
}
- (void)setEmulateUnlessSupported:(BOOL)emulateUnlessSupported
{
if (_emulateUnlessSupported == emulateUnlessSupported) {
return;
}
_emulateUnlessSupported = emulateUnlessSupported;
if ([self nativeSafeAreaSupport]) {
return;
}
[self invalidateSafeAreaInsets];
}
- (void)setEdges:(RNCSafeAreaViewEdges)edges {
_edges = edges;
[self updateLocalData];
}
@end