2015-08-08 09:58:30 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-08-08 09:58:30 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-08-08 09:58:30 +00:00
|
|
|
*/
|
|
|
|
|
2017-04-20 15:20:03 +00:00
|
|
|
#import "RCTDevLoadingView.h"
|
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
#import <QuartzCore/QuartzCore.h>
|
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTDefines.h"
|
2016-02-25 17:52:01 +00:00
|
|
|
#import "RCTModalHostViewController.h"
|
2017-04-20 15:20:03 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-08-08 09:58:30 +00:00
|
|
|
|
2018-07-09 10:21:05 +00:00
|
|
|
#if RCT_DEV | RCT_ENABLE_LOADING_VIEW
|
2015-08-08 09:58:30 +00:00
|
|
|
|
2015-09-04 10:44:55 +00:00
|
|
|
static BOOL isEnabled = YES;
|
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
@implementation RCTDevLoadingView
|
|
|
|
{
|
|
|
|
UIWindow *_window;
|
|
|
|
UILabel *_label;
|
|
|
|
NSDate *_showDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-09-04 10:44:55 +00:00
|
|
|
+ (void)setEnabled:(BOOL)enabled
|
|
|
|
{
|
|
|
|
isEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2017-08-07 13:45:24 +00:00
|
|
|
+ (BOOL)requiresMainQueueSetup
|
2016-05-03 16:08:03 +00:00
|
|
|
{
|
2017-08-07 13:45:24 +00:00
|
|
|
return YES;
|
2016-05-03 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
_bridge = bridge;
|
2015-11-25 11:09:00 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(hide)
|
|
|
|
name:RCTJavaScriptDidLoadNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(hide)
|
|
|
|
name:RCTJavaScriptDidFailToLoadNotification
|
|
|
|
object:nil];
|
2016-07-18 14:12:23 +00:00
|
|
|
|
|
|
|
if (bridge.loading) {
|
|
|
|
[self showWithURL:bridge.bundleURL];
|
|
|
|
}
|
2015-08-08 09:58:30 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:41:04 +00:00
|
|
|
RCT_EXPORT_METHOD(showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor)
|
2015-08-08 09:58:30 +00:00
|
|
|
{
|
2015-09-04 10:44:55 +00:00
|
|
|
if (!isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_showDate = [NSDate date];
|
|
|
|
if (!self->_window && !RCTRunningInTestEnvironment()) {
|
2018-02-12 06:21:02 +00:00
|
|
|
CGSize screenSize = [UIScreen mainScreen].bounds.size;
|
2018-07-17 14:03:21 +00:00
|
|
|
|
2018-05-13 06:19:27 +00:00
|
|
|
if (@available(iOS 11.0, *)) {
|
|
|
|
UIWindow *window = UIApplication.sharedApplication.keyWindow;
|
|
|
|
self->_window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, window.safeAreaInsets.top + 30)];
|
|
|
|
self->_label = [[UILabel alloc] initWithFrame:CGRectMake(0, window.safeAreaInsets.top, screenSize.width, 30)];
|
2018-02-12 06:21:02 +00:00
|
|
|
} else {
|
|
|
|
self->_window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, 22)];
|
|
|
|
self->_label = [[UILabel alloc] initWithFrame:self->_window.bounds];
|
|
|
|
}
|
|
|
|
[self->_window addSubview:self->_label];
|
2016-09-27 13:19:45 +00:00
|
|
|
#if TARGET_OS_TV
|
|
|
|
self->_window.windowLevel = UIWindowLevelNormal + 1;
|
|
|
|
#else
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_window.windowLevel = UIWindowLevelStatusBar + 1;
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2016-02-25 17:52:01 +00:00
|
|
|
// set a root VC so rotation is supported
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_window.rootViewController = [UIViewController new];
|
2016-02-25 17:52:01 +00:00
|
|
|
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_label.font = [UIFont systemFontOfSize:12.0];
|
|
|
|
self->_label.textAlignment = NSTextAlignmentCenter;
|
2015-08-08 09:58:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_label.text = message;
|
|
|
|
self->_label.textColor = color;
|
|
|
|
self->_window.backgroundColor = backgroundColor;
|
|
|
|
self->_window.hidden = NO;
|
2015-08-08 09:58:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:41:04 +00:00
|
|
|
RCT_EXPORT_METHOD(hide)
|
2015-08-08 09:58:30 +00:00
|
|
|
{
|
2015-09-04 10:44:55 +00:00
|
|
|
if (!isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
const NSTimeInterval MIN_PRESENTED_TIME = 0.6;
|
2016-07-07 19:36:56 +00:00
|
|
|
NSTimeInterval presentedTime = [[NSDate date] timeIntervalSinceDate:self->_showDate];
|
2015-08-08 09:58:30 +00:00
|
|
|
NSTimeInterval delay = MAX(0, MIN_PRESENTED_TIME - presentedTime);
|
2016-07-07 19:36:56 +00:00
|
|
|
CGRect windowFrame = self->_window.frame;
|
2015-08-08 09:58:30 +00:00
|
|
|
[UIView animateWithDuration:0.25
|
|
|
|
delay:delay
|
|
|
|
options:0
|
|
|
|
animations:^{
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_window.frame = CGRectOffset(windowFrame, 0, -windowFrame.size.height);
|
2015-08-08 09:58:30 +00:00
|
|
|
} completion:^(__unused BOOL finished) {
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_window.frame = windowFrame;
|
|
|
|
self->_window.hidden = YES;
|
|
|
|
self->_window = nil;
|
2015-08-08 09:58:30 +00:00
|
|
|
}];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:41:04 +00:00
|
|
|
- (void)showWithURL:(NSURL *)URL
|
|
|
|
{
|
|
|
|
UIColor *color;
|
|
|
|
UIColor *backgroundColor;
|
|
|
|
NSString *source;
|
|
|
|
if (URL.fileURL) {
|
2018-07-17 14:03:21 +00:00
|
|
|
// If dev mode is not enabled, we don't want to show this kind of notification
|
|
|
|
#if !RCT_DEV
|
|
|
|
return;
|
|
|
|
#endif
|
2016-02-01 20:41:04 +00:00
|
|
|
color = [UIColor grayColor];
|
|
|
|
backgroundColor = [UIColor blackColor];
|
|
|
|
source = @"pre-bundled file";
|
|
|
|
} else {
|
|
|
|
color = [UIColor whiteColor];
|
|
|
|
backgroundColor = [UIColor colorWithHue:1./3 saturation:1 brightness:.35 alpha:1];
|
|
|
|
source = [NSString stringWithFormat:@"%@:%@", URL.host, URL.port];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self showMessage:[NSString stringWithFormat:@"Loading from %@...", source]
|
|
|
|
color:color
|
|
|
|
backgroundColor:backgroundColor];
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:42:30 +00:00
|
|
|
- (void)updateProgress:(RCTLoadingProgress *)progress
|
|
|
|
{
|
|
|
|
if (!progress) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
self->_label.text = [progress description];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
@implementation RCTDevLoadingView
|
|
|
|
|
2015-08-25 11:27:09 +00:00
|
|
|
+ (NSString *)moduleName { return nil; }
|
2015-09-04 10:44:55 +00:00
|
|
|
+ (void)setEnabled:(BOOL)enabled { }
|
2018-03-14 18:04:56 +00:00
|
|
|
- (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor { }
|
2017-05-02 16:52:47 +00:00
|
|
|
- (void)showWithURL:(NSURL *)URL { }
|
|
|
|
- (void)updateProgress:(RCTLoadingProgress *)progress { }
|
|
|
|
- (void)hide { }
|
2015-08-25 11:27:09 +00:00
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
#endif
|