From 3d194ccb17fefbdac18d2633f949b65b60bb98c1 Mon Sep 17 00:00:00 2001 From: Alan Hughes Date: Thu, 6 Jul 2023 09:58:11 +0100 Subject: [PATCH 1/2] fix: return the correct default trait collection --- .../React/CoreModules/RCTAppearance.mm | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/react-native/React/CoreModules/RCTAppearance.mm b/packages/react-native/React/CoreModules/RCTAppearance.mm index 2930401fc98f6e..4be686344a7d5b 100644 --- a/packages/react-native/React/CoreModules/RCTAppearance.mm +++ b/packages/react-native/React/CoreModules/RCTAppearance.mm @@ -36,6 +36,18 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride) return sColorSchemeOverride; } +UITraitCollection* getKeyWindowTraitCollection() { + if ([NSThread isMainThread]) { + return [UIApplication sharedApplication].delegate.window.traitCollection; + } else { + __block UITraitCollection* traitCollection = nil; + dispatch_sync(dispatch_get_main_queue(), ^{ + traitCollection = [UIApplication sharedApplication].delegate.window.traitCollection; + }); + return traitCollection; + } +} + NSString *RCTColorSchemePreference(UITraitCollection *traitCollection) { static NSDictionary *appearances; @@ -57,7 +69,7 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride) return RCTAppearanceColorSchemeLight; } - traitCollection = traitCollection ?: [UITraitCollection currentTraitCollection]; + traitCollection = traitCollection ?: getKeyWindowTraitCollection(); return appearances[@(traitCollection.userInterfaceStyle)] ?: RCTAppearanceColorSchemeLight; // Default to light on older OS version - same behavior as Android. From 3da09de305405921dfac0d606bbed14968115d81 Mon Sep 17 00:00:00 2001 From: Alan Hughes Date: Sun, 10 Sep 2023 00:21:01 +0100 Subject: [PATCH 2/2] make requested changes --- .../React/CoreModules/RCTAppearance.mm | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTAppearance.mm b/packages/react-native/React/CoreModules/RCTAppearance.mm index 4be686344a7d5b..99ff9bfcbd5160 100644 --- a/packages/react-native/React/CoreModules/RCTAppearance.mm +++ b/packages/react-native/React/CoreModules/RCTAppearance.mm @@ -37,15 +37,11 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride) } UITraitCollection* getKeyWindowTraitCollection() { - if ([NSThread isMainThread]) { - return [UIApplication sharedApplication].delegate.window.traitCollection; - } else { - __block UITraitCollection* traitCollection = nil; - dispatch_sync(dispatch_get_main_queue(), ^{ - traitCollection = [UIApplication sharedApplication].delegate.window.traitCollection; - }); - return traitCollection; - } + __block UITraitCollection* traitCollection = nil; + RCTExecuteOnMainQueue(^{ + traitCollection = RCTSharedApplication().delegate.window.traitCollection; + }); + return traitCollection; } NSString *RCTColorSchemePreference(UITraitCollection *traitCollection)