2015-08-08 09:58:30 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <QuartzCore/QuartzCore.h>
|
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTDevLoadingView.h"
|
|
|
|
#import "RCTDefines.h"
|
|
|
|
#import "RCTUtils.h"
|
2016-02-25 17:52:01 +00:00
|
|
|
#import "RCTModalHostViewController.h"
|
2015-08-08 09:58:30 +00:00
|
|
|
|
|
|
|
#if RCT_DEV
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2016-05-03 16:08:03 +00:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
// We're only overriding this to ensure the module gets created at startup
|
2016-05-04 14:06:09 +00:00
|
|
|
// TODO (t11106126): Remove once we have more declarative control over module setup.
|
2016-05-03 16:08:03 +00:00
|
|
|
return [super init];
|
|
|
|
}
|
|
|
|
|
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];
|
2015-08-08 09:58:30 +00:00
|
|
|
[self showWithURL:bridge.bundleURL];
|
|
|
|
}
|
|
|
|
|
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(), ^{
|
|
|
|
_showDate = [NSDate date];
|
|
|
|
if (!_window && !RCTRunningInTestEnvironment()) {
|
2015-08-24 10:14:33 +00:00
|
|
|
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
|
2015-08-08 09:58:30 +00:00
|
|
|
_window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 22)];
|
|
|
|
_window.windowLevel = UIWindowLevelStatusBar + 1;
|
|
|
|
|
2016-02-25 17:52:01 +00:00
|
|
|
// set a root VC so rotation is supported
|
2016-04-01 19:30:47 +00:00
|
|
|
_window.rootViewController = [UIViewController new];
|
2016-02-25 17:52:01 +00:00
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
_label = [[UILabel alloc] initWithFrame:_window.bounds];
|
|
|
|
_label.font = [UIFont systemFontOfSize:12.0];
|
|
|
|
_label.textAlignment = NSTextAlignmentCenter;
|
|
|
|
|
|
|
|
[_window addSubview:_label];
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:41:04 +00:00
|
|
|
_label.text = message;
|
|
|
|
_label.textColor = color;
|
|
|
|
_window.backgroundColor = backgroundColor;
|
2015-08-08 09:58:30 +00:00
|
|
|
_window.hidden = NO;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
NSTimeInterval presentedTime = [[NSDate date] timeIntervalSinceDate:_showDate];
|
|
|
|
NSTimeInterval delay = MAX(0, MIN_PRESENTED_TIME - presentedTime);
|
|
|
|
CGRect windowFrame = _window.frame;
|
|
|
|
[UIView animateWithDuration:0.25
|
|
|
|
delay:delay
|
|
|
|
options:0
|
|
|
|
animations:^{
|
|
|
|
_window.frame = CGRectOffset(windowFrame, 0, -windowFrame.size.height);
|
|
|
|
} completion:^(__unused BOOL finished) {
|
|
|
|
_window.frame = windowFrame;
|
|
|
|
_window.hidden = YES;
|
|
|
|
_window = nil;
|
|
|
|
}];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:41:04 +00:00
|
|
|
- (void)showWithURL:(NSURL *)URL
|
|
|
|
{
|
|
|
|
UIColor *color;
|
|
|
|
UIColor *backgroundColor;
|
|
|
|
NSString *source;
|
|
|
|
if (URL.fileURL) {
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
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 { }
|
2015-08-25 11:27:09 +00:00
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
#endif
|