Skip to content

Commit 7d0d04d

Browse files
committed
get initial value in initializer
1 parent a4564b1 commit 7d0d04d

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

packages/react-native/React/CoreModules/RCTAppearance.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ RCT_EXTERN NSString *RCTCurrentOverrideAppearancePreference();
1717
RCT_EXTERN NSString *RCTColorSchemePreference(UITraitCollection *traitCollection);
1818

1919
@interface RCTAppearance : RCTEventEmitter <RCTBridgeModule>
20+
- (instancetype)init;
2021
@end

packages/react-native/React/CoreModules/RCTAppearance.mm

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride)
3636
return sColorSchemeOverride;
3737
}
3838

39-
static UITraitCollection *getKeyWindowTraitCollection()
40-
{
41-
__block UITraitCollection *traitCollection = nil;
42-
RCTUnsafeExecuteOnMainQueueSync(^{
43-
traitCollection = RCTSharedApplication().delegate.window.traitCollection;
44-
});
45-
return traitCollection;
46-
}
47-
4839
NSString *RCTColorSchemePreference(UITraitCollection *traitCollection)
4940
{
5041
static NSDictionary *appearances;
@@ -66,7 +57,6 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride)
6657
return RCTAppearanceColorSchemeLight;
6758
}
6859

69-
traitCollection = traitCollection ?: getKeyWindowTraitCollection();
7060
return appearances[@(traitCollection.userInterfaceStyle)] ?: RCTAppearanceColorSchemeLight;
7161
}
7262

@@ -77,6 +67,15 @@ @implementation RCTAppearance {
7767
NSString *_currentColorScheme;
7868
}
7969

70+
- (instancetype)init
71+
{
72+
if ((self = [super init])) {
73+
UITraitCollection *traitCollection = RCTSharedApplication().delegate.window.traitCollection;
74+
_currentColorScheme = RCTColorSchemePreference(traitCollection);
75+
}
76+
return self;
77+
}
78+
8079
RCT_EXPORT_MODULE(Appearance)
8180

8281
+ (BOOL)requiresMainQueueSetup
@@ -106,9 +105,6 @@ - (dispatch_queue_t)methodQueue
106105

107106
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme)
108107
{
109-
if (_currentColorScheme == nil) {
110-
_currentColorScheme = RCTColorSchemePreference(nil);
111-
}
112108
return _currentColorScheme;
113109
}
114110

0 commit comments

Comments
 (0)