Skip to content

Commit 68a4887

Browse files
committed
get initial value in initializer
1 parent e01e081 commit 68a4887

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
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: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,6 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride)
3636
return sColorSchemeOverride;
3737
}
3838

39-
<<<<<<< HEAD
40-
||||||| parent of 5715b1498ee (Make requesting the trait collection synchronous)
41-
static UITraitCollection *getKeyWindowTraitCollection()
42-
{
43-
__block UITraitCollection *traitCollection = nil;
44-
RCTExecuteOnMainQueue(^{
45-
traitCollection = RCTSharedApplication().delegate.window.traitCollection;
46-
});
47-
return traitCollection;
48-
}
49-
50-
=======
51-
static UITraitCollection *getKeyWindowTraitCollection()
52-
{
53-
__block UITraitCollection *traitCollection = nil;
54-
RCTUnsafeExecuteOnMainQueueSync(^{
55-
traitCollection = RCTSharedApplication().delegate.window.traitCollection;
56-
});
57-
return traitCollection;
58-
}
59-
60-
>>>>>>> 5715b1498ee (Make requesting the trait collection synchronous)
6139
NSString *RCTColorSchemePreference(UITraitCollection *traitCollection)
6240
{
6341
static NSDictionary *appearances;
@@ -79,7 +57,6 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride)
7957
return RCTAppearanceColorSchemeLight;
8058
}
8159

82-
traitCollection = traitCollection ?: [UITraitCollection currentTraitCollection];
8360
return appearances[@(traitCollection.userInterfaceStyle)] ?: RCTAppearanceColorSchemeLight;
8461
}
8562

@@ -90,6 +67,19 @@ @implementation RCTAppearance {
9067
NSString *_currentColorScheme;
9168
}
9269

70+
- (instancetype)init
71+
{
72+
if ((self = [super init])) {
73+
UITraitCollection *traitCollection = RCTSharedApplication().delegate.window.traitCollection;
74+
_currentColorScheme = RCTColorSchemePreference(traitCollection);
75+
[[NSNotificationCenter defaultCenter] addObserver:self
76+
selector:@selector(appearanceChanged:)
77+
name:RCTUserInterfaceStyleDidChangeNotification
78+
object:nil];
79+
}
80+
return self;
81+
}
82+
9383
RCT_EXPORT_MODULE(Appearance)
9484

9585
+ (BOOL)requiresMainQueueSetup
@@ -119,9 +109,6 @@ - (dispatch_queue_t)methodQueue
119109

120110
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme)
121111
{
122-
if (_currentColorScheme == nil) {
123-
_currentColorScheme = RCTColorSchemePreference(nil);
124-
}
125112
return _currentColorScheme;
126113
}
127114

@@ -148,10 +135,7 @@ - (void)appearanceChanged:(NSNotification *)notification
148135

149136
- (void)startObserving
150137
{
151-
[[NSNotificationCenter defaultCenter] addObserver:self
152-
selector:@selector(appearanceChanged:)
153-
name:RCTUserInterfaceStyleDidChangeNotification
154-
object:nil];
138+
155139
}
156140

157141
- (void)stopObserving

0 commit comments

Comments
 (0)