@@ -32,6 +32,7 @@ @implementation RCTDevLoadingView {
32
32
UILabel *_host;
33
33
NSDate *_showDate;
34
34
BOOL _hiding;
35
+ dispatch_block_t _initialMessageBlock;
35
36
}
36
37
37
38
@synthesize bridge = _bridge;
@@ -66,6 +67,27 @@ - (void)setBridge:(RCTBridge *)bridge
66
67
}
67
68
}
68
69
70
+ - (void )clearInitialMessageDelay
71
+ {
72
+ if (self->_initialMessageBlock != nil ) {
73
+ dispatch_block_cancel (self->_initialMessageBlock );
74
+ self->_initialMessageBlock = nil ;
75
+ }
76
+ }
77
+
78
+ - (void )showInitialMessageDelayed : (void (^)())initialMessage
79
+ {
80
+ self->_initialMessageBlock = dispatch_block_create (static_cast <dispatch_block_flags_t >(0 ), initialMessage);
81
+
82
+ // We delay the initial loading message to prevent flashing it
83
+ // when loading progress starts quickly. To do that, we
84
+ // schedule the message to be shown in a block, and cancel
85
+ // the block later when the progress starts coming in.
86
+ // If the progress beats this timer, this message is not shown.
87
+ dispatch_after (
88
+ dispatch_time (DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), dispatch_get_main_queue (), self->_initialMessageBlock );
89
+ }
90
+
69
91
- (UIColor *)dimColor : (UIColor *)c
70
92
{
71
93
// Given a color, return a slightly lighter or darker color for dim effect.
@@ -159,6 +181,9 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(
159
181
return ;
160
182
}
161
183
184
+ // Cancel the initial message block so it doesn't display later and get stuck.
185
+ [self clearInitialMessageDelay ];
186
+
162
187
dispatch_async (dispatch_get_main_queue (), ^{
163
188
self->_hiding = true ;
164
189
const NSTimeInterval MIN_PRESENTED_TIME = 0.6 ;
@@ -186,28 +211,47 @@ - (void)showWithURL:(NSURL *)URL
186
211
UIColor *backgroundColor;
187
212
NSString *message;
188
213
if (URL.fileURL ) {
189
- // If dev mode is not enabled, we don't want to show this kind of notification
214
+ // If dev mode is not enabled, we don't want to show this kind of notification.
190
215
#if !RCT_DEV
191
216
return ;
192
217
#endif
193
218
color = [UIColor whiteColor ];
194
219
backgroundColor = [UIColor colorWithHue: 105 saturation: 0 brightness: .25 alpha: 1 ];
195
220
message = [NSString stringWithFormat: @" Connect to %@ to develop JavaScript." , RCT_PACKAGER_NAME];
221
+ [self showMessage: message color: color backgroundColor: backgroundColor];
196
222
} else {
197
223
color = [UIColor whiteColor ];
198
224
backgroundColor = [UIColor colorWithHue: 105 saturation: 0 brightness: .25 alpha: 1 ];
199
225
message = [NSString stringWithFormat: @" Loading from %@ \u2026 " , RCT_PACKAGER_NAME];
226
+
227
+ [self showInitialMessageDelayed: ^{
228
+ [self showMessage: message color: color backgroundColor: backgroundColor];
229
+ }];
200
230
}
201
- [self showMessage: message color: color backgroundColor: backgroundColor];
202
231
}
203
232
204
233
- (void )updateProgress : (RCTLoadingProgress *)progress
205
234
{
206
235
if (!progress) {
207
236
return ;
208
237
}
238
+
239
+ // Cancel the initial message block so it's not flashed before progress.
240
+ [self clearInitialMessageDelay ];
241
+
209
242
dispatch_async (dispatch_get_main_queue (), ^{
210
- self->_label .text = [progress description ];
243
+ if (self->_window == nil ) {
244
+ // If we didn't show the initial message, then there's no banner window.
245
+ // We need to create it here so that the progress is actually displayed.
246
+ UIColor *color = [UIColor whiteColor ];
247
+ UIColor *backgroundColor = [UIColor colorWithHue: 105 saturation: 0 brightness: .25 alpha: 1 ];
248
+ [self showMessage: [progress description ] color: color backgroundColor: backgroundColor];
249
+ } else {
250
+ // This is an optimization. Since the progress can come in quickly,
251
+ // we want to do the minimum amount of work to update the UI,
252
+ // which is to only update the label text.
253
+ self->_label .text = [progress description ];
254
+ }
211
255
});
212
256
}
213
257
0 commit comments