Skip to content

Commit

Permalink
Add hostname to loading banner on iOS
Browse files Browse the repository at this point in the history
Summary:
This diff adds a hostname to the loading banner on iOS so it's clear which server you're loading from.

Changelog: [Added] [iOS] Added hostname to loading banner.

Reviewed By: PeteTheHeat

Differential Revision: D21280252

fbshipit-source-id: d7733c056f5fb63e32b247a4fa1476ab42c7da17
  • Loading branch information
rickhanlonii authored and facebook-github-bot committed Apr 29, 2020
1 parent e9f29a3 commit 9699933
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions React/CoreModules/RCTDevLoadingView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ @interface RCTDevLoadingView () <NativeDevLoadingViewSpec>
@implementation RCTDevLoadingView {
UIWindow *_window;
UILabel *_label;
UILabel *_host;
NSDate *_showDate;
}

Expand Down Expand Up @@ -64,6 +65,24 @@ - (void)setBridge:(RCTBridge *)bridge
}
}

- (UIColor *)dimColor:(UIColor *)c
{
// Given a color, return a slightly lighter or darker color for dim effect.
CGFloat h, s, b, a;
if ([c getHue:&h saturation:&s brightness:&b alpha:&a])
return [UIColor colorWithHue:h saturation:s brightness:b < 0.5 ? b * 1.25 : b * 0.75 alpha:a];
return nil;
}

- (NSString *)getTextForHost
{
if (self->_bridge.bundleURL == nil || self->_bridge.bundleURL.fileURL) {
return @"React Native";
}

return [NSString stringWithFormat:@"%@:%@", self->_bridge.bundleURL.host, self->_bridge.bundleURL.port];
}

- (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor
{
if (!RCTDevLoadingViewGetEnabled()) {
Expand All @@ -78,18 +97,25 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(
if (@available(iOS 11.0, *)) {
UIWindow *window = RCTSharedApplication().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)];
[[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, window.safeAreaInsets.top + 52)];
self->_label = [[UILabel alloc] initWithFrame:CGRectMake(0, window.safeAreaInsets.top, screenSize.width, 22)];
self->_host =
[[UILabel alloc] initWithFrame:CGRectMake(0, window.safeAreaInsets.top + 20, screenSize.width, 22)];
self->_host.font = [UIFont monospacedDigitSystemFontOfSize:10.0 weight:UIFontWeightRegular];
self->_host.textAlignment = NSTextAlignmentCenter;

[self->_window addSubview:self->_label];
[self->_window addSubview:self->_host];

} 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];
// TODO: Add host to iOS < 11.0
}
[self->_window addSubview:self->_label];
#if TARGET_OS_TV
self->_window.windowLevel = UIWindowLevelNormal + 1;
#else

self->_window.windowLevel = UIWindowLevelStatusBar + 1;
#endif
// set a root VC so rotation is supported
self->_window.rootViewController = [UIViewController new];

Expand All @@ -99,6 +125,12 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(

self->_label.text = message;
self->_label.textColor = color;

if (self->_host != nil) {
self->_host.text = [self getTextForHost];
self->_host.textColor = [self dimColor:color];
}

self->_window.backgroundColor = backgroundColor;
self->_window.hidden = NO;

Expand Down

0 comments on commit 9699933

Please sign in to comment.