Skip to content

Commit 94c45af

Browse files
rickhanloniifacebook-github-bot
authored andcommitted
Add dark mode to loading progress
Summary: This diff updates the loading banner to respect the RCTAppearance dev mode setting. Changelog: [General] [iOS] Add dark mode support to loading banner Reviewed By: fkgozali Differential Revision: D21429148 fbshipit-source-id: d7d9e778245112a19accf813dcff693f0d187a38
1 parent b01fcee commit 94c45af

File tree

4 files changed

+77
-39
lines changed

4 files changed

+77
-39
lines changed

Libraries/Utilities/LoadingView.ios.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,38 @@
1212

1313
import processColor from '../StyleSheet/processColor';
1414
import NativeDevLoadingView from './NativeDevLoadingView';
15+
import Appearance from './Appearance';
1516

1617
module.exports = {
1718
showMessage(message: string, type: 'load' | 'refresh') {
1819
if (NativeDevLoadingView) {
19-
const loadColor = processColor('#404040');
20-
const refreshColor = processColor('#2584e8');
21-
const white = processColor('#ffffff');
20+
if (type === 'refresh') {
21+
const backgroundColor = processColor('#2584e8');
22+
const textColor = processColor('#ffffff');
2223

23-
NativeDevLoadingView.showMessage(
24-
message,
25-
typeof white === 'number' ? white : null,
26-
type && type === 'load'
27-
? typeof loadColor === 'number'
28-
? loadColor
29-
: null
30-
: typeof refreshColor === 'number'
31-
? refreshColor
32-
: null,
33-
);
24+
NativeDevLoadingView.showMessage(
25+
message,
26+
typeof textColor === 'number' ? textColor : null,
27+
typeof backgroundColor === 'number' ? backgroundColor : null,
28+
);
29+
} else if (type === 'load') {
30+
let backgroundColor;
31+
let textColor;
32+
33+
if (Appearance.getColorScheme() === 'dark') {
34+
backgroundColor = processColor('#fafafa');
35+
textColor = processColor('#242526');
36+
} else {
37+
backgroundColor = processColor('#404040');
38+
textColor = processColor('#ffffff');
39+
}
40+
41+
NativeDevLoadingView.showMessage(
42+
message,
43+
typeof textColor === 'number' ? textColor : null,
44+
typeof backgroundColor === 'number' ? backgroundColor : null,
45+
);
46+
}
3447
}
3548
},
3649
hide() {

React/CoreModules/RCTAppearance.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
RCT_EXTERN void RCTEnableAppearancePreference(BOOL enabled);
1414
RCT_EXTERN void RCTOverrideAppearancePreference(NSString *const);
15+
RCT_EXTERN NSString *RCTColorSchemePreference(UITraitCollection *traitCollection);
1516

1617
@interface RCTAppearance : RCTEventEmitter <RCTBridgeModule>
1718
@end

React/CoreModules/RCTAppearance.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride)
3030
sColorSchemeOverride = colorSchemeOverride;
3131
}
3232

33-
static NSString *RCTColorSchemePreference(UITraitCollection *traitCollection)
33+
NSString *RCTColorSchemePreference(UITraitCollection *traitCollection)
3434
{
3535
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
3636
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0

React/CoreModules/RCTDevLoadingView.mm

+48-24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <QuartzCore/QuartzCore.h>
1111

1212
#import <FBReactNativeSpec/FBReactNativeSpec.h>
13+
#import <React/RCTAppearance.h>
1314
#import <React/RCTBridge.h>
1415
#import <React/RCTConvert.h>
1516
#import <React/RCTDefines.h>
@@ -190,27 +191,61 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(
190191
});
191192
}
192193

194+
- (void)showProgressMessage:(NSString *)message
195+
{
196+
if (self->_window != nil) {
197+
// This is an optimization. Since the progress can come in quickly,
198+
// we want to do the minimum amount of work to update the UI,
199+
// which is to only update the label text.
200+
self->_label.text = message;
201+
return;
202+
}
203+
204+
UIColor *color = [UIColor whiteColor];
205+
UIColor *backgroundColor = [UIColor colorWithHue:105 saturation:0 brightness:.25 alpha:1];
206+
207+
if ([self isDarkModeEnabled]) {
208+
color = [UIColor colorWithHue:208 saturation:0.03 brightness:.14 alpha:1];
209+
backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.98 alpha:1];
210+
}
211+
212+
[self showMessage:message color:color backgroundColor:backgroundColor];
213+
}
214+
215+
- (void)showOfflineMessage
216+
{
217+
UIColor *color = [UIColor whiteColor];
218+
UIColor *backgroundColor = [UIColor blackColor];
219+
220+
if ([self isDarkModeEnabled]) {
221+
color = [UIColor blackColor];
222+
backgroundColor = [UIColor whiteColor];
223+
}
224+
225+
NSString *message = [NSString stringWithFormat:@"Connect to %@ to develop JavaScript.", RCT_PACKAGER_NAME];
226+
[self showMessage:message color:color backgroundColor:backgroundColor];
227+
}
228+
229+
- (BOOL)isDarkModeEnabled
230+
{
231+
// We pass nil here to match the behavior of the native module.
232+
// If we were to pass a view, then it's possible that this native
233+
// banner would have a different color than the JavaScript banner
234+
// (which always passes nil). This would result in an inconsistent UI.
235+
return [RCTColorSchemePreference(nil) isEqualToString:@"dark"];
236+
}
193237
- (void)showWithURL:(NSURL *)URL
194238
{
195-
UIColor *color;
196-
UIColor *backgroundColor;
197-
NSString *message;
198239
if (URL.fileURL) {
199240
// If dev mode is not enabled, we don't want to show this kind of notification.
200241
#if !RCT_DEV
201242
return;
202243
#endif
203-
color = [UIColor whiteColor];
204-
backgroundColor = [UIColor blackColor];
205-
message = [NSString stringWithFormat:@"Connect to %@ to develop JavaScript.", RCT_PACKAGER_NAME];
206-
[self showMessage:message color:color backgroundColor:backgroundColor];
244+
[self showOfflineMessage];
207245
} else {
208-
color = [UIColor whiteColor];
209-
backgroundColor = [UIColor colorWithHue:105 saturation:0 brightness:.25 alpha:1];
210-
message = [NSString stringWithFormat:@"Loading from %@\u2026", RCT_PACKAGER_NAME];
211-
212246
[self showInitialMessageDelayed:^{
213-
[self showMessage:message color:color backgroundColor:backgroundColor];
247+
NSString *message = [NSString stringWithFormat:@"Loading from %@\u2026", RCT_PACKAGER_NAME];
248+
[self showProgressMessage:message];
214249
}];
215250
}
216251
}
@@ -225,18 +260,7 @@ - (void)updateProgress:(RCTLoadingProgress *)progress
225260
[self clearInitialMessageDelay];
226261

227262
dispatch_async(dispatch_get_main_queue(), ^{
228-
if (self->_window == nil) {
229-
// If we didn't show the initial message, then there's no banner window.
230-
// We need to create it here so that the progress is actually displayed.
231-
UIColor *color = [UIColor whiteColor];
232-
UIColor *backgroundColor = [UIColor colorWithHue:105 saturation:0 brightness:.25 alpha:1];
233-
[self showMessage:[progress description] color:color backgroundColor:backgroundColor];
234-
} else {
235-
// This is an optimization. Since the progress can come in quickly,
236-
// we want to do the minimum amount of work to update the UI,
237-
// which is to only update the label text.
238-
self->_label.text = [progress description];
239-
}
263+
[self showProgressMessage:[progress description]];
240264
});
241265
}
242266

0 commit comments

Comments
 (0)