forked from zoontek/react-native-bootsplash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNBootSplash.mm
executable file
·198 lines (157 loc) · 5.63 KB
/
RNBootSplash.mm
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
#import "RNBootSplash.h"
#import <React/RCTUtils.h>
#if RCT_NEW_ARCH_ENABLED
#import <React/RCTFabricSurfaceHostingProxyRootView.h>
#import <React/RCTSurfaceHostingView.h>
#else
#import <React/RCTRootView.h>
#endif
static NSMutableArray<RCTPromiseResolveBlock> *_resolveQueue = [[NSMutableArray alloc] init];
static UIView *_loadingView = nil;
static UIView *_rootView = nil;
static bool _fade = false;
static bool _nativeHidden = false;
@implementation RNBootSplash
RCT_EXPORT_MODULE();
+ (BOOL)requiresMainQueueSetup {
return NO;
}
- (dispatch_queue_t)methodQueue {
return dispatch_get_main_queue();
}
+ (bool)isLoadingViewVisible {
return _loadingView != nil && ![_loadingView isHidden];
}
+ (void)clearResolveQueue {
while ([_resolveQueue count] > 0) {
RCTPromiseResolveBlock resolve = [_resolveQueue objectAtIndex:0];
[_resolveQueue removeObjectAtIndex:0];
resolve(@(true));
}
}
+ (void)hideAndClearPromiseQueue {
if (![self isLoadingViewVisible]) {
return [RNBootSplash clearResolveQueue];
}
if (_fade) {
dispatch_async(dispatch_get_main_queue(), ^{
[UIView transitionWithView:_rootView
duration:0.250
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
_loadingView.hidden = YES;
}
completion:^(__unused BOOL finished) {
[_loadingView removeFromSuperview];
_loadingView = nil;
return [RNBootSplash clearResolveQueue];
}];
});
} else {
_loadingView.hidden = YES;
[_loadingView removeFromSuperview];
_loadingView = nil;
return [RNBootSplash clearResolveQueue];
}
}
+ (void)initWithStoryboard:(NSString * _Nonnull)storyboardName
rootView:(UIView * _Nullable)rootView {
if (RCTRunningInAppExtension()) {
return;
}
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^(void) {
[NSTimer scheduledTimerWithTimeInterval:0.35
repeats:NO
block:^(NSTimer * _Nonnull timer) {
// wait for native iOS launch screen to fade out
_nativeHidden = true;
// hide has been called before native launch screen fade out
if ([_resolveQueue count] > 0) {
[self hideAndClearPromiseQueue];
}
}];
#ifdef RCT_NEW_ARCH_ENABLED
if (rootView != nil && [rootView isKindOfClass:[RCTFabricSurfaceHostingProxyRootView class]]) {
RCTFabricSurfaceHostingProxyRootView *proxy = (RCTFabricSurfaceHostingProxyRootView *)rootView;
_rootView = (RCTSurfaceHostingView *)proxy.surface.view;
#else
if (rootView != nil && [rootView isKindOfClass:[RCTRootView class]]) {
_rootView = (RCTRootView *)rootView;
#endif
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
_loadingView = [[storyboard instantiateInitialViewController] view];
_loadingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_loadingView.frame = _rootView.bounds;
_loadingView.center = (CGPoint){CGRectGetMidX(_rootView.bounds), CGRectGetMidY(_rootView.bounds)};
_loadingView.hidden = NO;
[_rootView addSubview:_loadingView];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onJavaScriptDidLoad)
name:RCTJavaScriptDidLoadNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onJavaScriptDidFailToLoad)
name:RCTJavaScriptDidFailToLoadNotification
object:nil];
}
});
}
+ (void)onJavaScriptDidLoad {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
+ (void)onJavaScriptDidFailToLoad {
[self hideAndClearPromiseQueue];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSDictionary *)constantsToExport {
return @{
@"logoSizeRatio": @(1),
@"navigationBarHeight": @(0),
@"statusBarHeight": @(0)
};
}
- (void)hideImpl:(BOOL)fade
resolve:(RCTPromiseResolveBlock)resolve {
if (RCTRunningInAppExtension()) {
return resolve(@(true));
}
[_resolveQueue addObject:resolve];
_fade = fade;
if (_nativeHidden) {
return [RNBootSplash hideAndClearPromiseQueue];
}
}
- (void)isVisibleImpl:(RCTPromiseResolveBlock)resolve {
resolve(@([RNBootSplash isLoadingViewVisible]));
}
#ifdef RCT_NEW_ARCH_ENABLED
// New architecture
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params {
return std::make_shared<facebook::react::NativeRNBootSplashSpecJSI>(params);
}
- (facebook::react::ModuleConstants<JS::NativeRNBootSplash::Constants::Builder>)getConstants {
return [self constantsToExport];
}
- (void)hide:(BOOL)fade
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject {
[self hideImpl:fade resolve:resolve];
}
- (void)isVisible:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject {
[self isVisibleImpl:resolve];
}
#else
// Old architecture
RCT_EXPORT_METHOD(hide:(BOOL)fade
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
[self hideImpl:fade resolve:resolve];
}
RCT_EXPORT_METHOD(isVisible:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
[self isVisibleImpl:resolve];
}
#endif
@end