From 326a3b1e78f9d92a2bae5f7ea8fe03de25bea45a Mon Sep 17 00:00:00 2001 From: filip131311 Date: Tue, 30 Apr 2024 14:28:13 +0200 Subject: [PATCH 1/2] fix dark mode on application intitial run --- packages/react-native/React/CoreModules/RCTAppearance.mm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTAppearance.mm b/packages/react-native/React/CoreModules/RCTAppearance.mm index 2ad68a589464..9513a04d357a 100644 --- a/packages/react-native/React/CoreModules/RCTAppearance.mm +++ b/packages/react-native/React/CoreModules/RCTAppearance.mm @@ -70,8 +70,6 @@ @implementation RCTAppearance { - (instancetype)init { if ((self = [super init])) { - UITraitCollection *traitCollection = RCTKeyWindow().traitCollection; - _currentColorScheme = RCTColorSchemePreference(traitCollection); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appearanceChanged:) name:RCTUserInterfaceStyleDidChangeNotification @@ -109,6 +107,10 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme) { + if (_currentColorScheme == nil) { + UITraitCollection *traitCollection = RCTKeyWindow().traitCollection; + _currentColorScheme = RCTColorSchemePreference(traitCollection); + } return _currentColorScheme; } From 6a6a7a99bb6af648d980352c4432dacf08c4f266 Mon Sep 17 00:00:00 2001 From: filip131311 Date: Mon, 6 May 2024 15:36:34 +0200 Subject: [PATCH 2/2] make sure _currentColorScheme always exist --- .../react-native/React/CoreModules/RCTAppearance.mm | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTAppearance.mm b/packages/react-native/React/CoreModules/RCTAppearance.mm index 9513a04d357a..37bb7a9901a2 100644 --- a/packages/react-native/React/CoreModules/RCTAppearance.mm +++ b/packages/react-native/React/CoreModules/RCTAppearance.mm @@ -19,6 +19,8 @@ NSString *const RCTAppearanceColorSchemeLight = @"light"; NSString *const RCTAppearanceColorSchemeDark = @"dark"; +static BOOL sIsAppearancePreferenceSet = NO; + static BOOL sAppearancePreferenceEnabled = YES; void RCTEnableAppearancePreference(BOOL enabled) { @@ -57,7 +59,12 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride) return RCTAppearanceColorSchemeLight; } - return appearances[@(traitCollection.userInterfaceStyle)] ?: RCTAppearanceColorSchemeLight; + if (appearances[@(traitCollection.userInterfaceStyle)]){ + sIsAppearancePreferenceSet = YES; + return appearances[@(traitCollection.userInterfaceStyle)]; + } + + return RCTAppearanceColorSchemeLight; } @interface RCTAppearance () @@ -70,6 +77,8 @@ @implementation RCTAppearance { - (instancetype)init { if ((self = [super init])) { + UITraitCollection *traitCollection = RCTKeyWindow().traitCollection; + _currentColorScheme = RCTColorSchemePreference(traitCollection); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appearanceChanged:) name:RCTUserInterfaceStyleDidChangeNotification @@ -107,7 +116,7 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme) { - if (_currentColorScheme == nil) { + if (!sIsAppearancePreferenceSet) { UITraitCollection *traitCollection = RCTKeyWindow().traitCollection; _currentColorScheme = RCTColorSchemePreference(traitCollection); }