-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: over reporting trait changes #39439
Closed
alanjhughes
wants to merge
6
commits into
facebook:main
from
alanjhughes:fix/overreporting-trait-changes
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65eae90
fix: don't report trait changes when in background
alanjhughes 0aae245
Make requesting the trait collection synchronous
alanjhughes e01e081
use RCTUnsafeExecuteOnMainQueueSync
alanjhughes 68a4887
get initial value in initializer
alanjhughes de742dc
add invalidate
alanjhughes 6ef6687
remove observers
alanjhughes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,11 +57,7 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride) | |
return RCTAppearanceColorSchemeLight; | ||
} | ||
|
||
traitCollection = traitCollection ?: [UITraitCollection currentTraitCollection]; | ||
return appearances[@(traitCollection.userInterfaceStyle)] ?: RCTAppearanceColorSchemeLight; | ||
|
||
// Default to light on older OS version - same behavior as Android. | ||
return RCTAppearanceColorSchemeLight; | ||
} | ||
|
||
@interface RCTAppearance () <NativeAppearanceSpec> | ||
|
@@ -71,6 +67,19 @@ @implementation RCTAppearance { | |
NSString *_currentColorScheme; | ||
} | ||
|
||
- (instancetype)init | ||
{ | ||
if ((self = [super init])) { | ||
UITraitCollection *traitCollection = RCTSharedApplication().delegate.window.traitCollection; | ||
_currentColorScheme = RCTColorSchemePreference(traitCollection); | ||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(appearanceChanged:) | ||
name:RCTUserInterfaceStyleDidChangeNotification | ||
object:nil]; | ||
} | ||
Comment on lines
+75
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should move the logic from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
return self; | ||
} | ||
|
||
RCT_EXPORT_MODULE(Appearance) | ||
|
||
+ (BOOL)requiresMainQueueSetup | ||
|
@@ -100,9 +109,6 @@ - (dispatch_queue_t)methodQueue | |
|
||
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme) | ||
{ | ||
if (_currentColorScheme == nil) { | ||
_currentColorScheme = RCTColorSchemePreference(nil); | ||
} | ||
return _currentColorScheme; | ||
} | ||
|
||
|
@@ -127,16 +133,8 @@ - (void)appearanceChanged:(NSNotification *)notification | |
return @[ @"appearanceChanged" ]; | ||
} | ||
|
||
- (void)startObserving | ||
{ | ||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(appearanceChanged:) | ||
name:RCTUserInterfaceStyleDidChangeNotification | ||
object:nil]; | ||
} | ||
|
||
- (void)stopObserving | ||
{ | ||
- (void)invalidate { | ||
[super invalidate]; | ||
[[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To confirm: where do we update the trait collection when the application foregrounds? If you change to dark mode while the app is backgrounded, do we pick it up on foreground?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll get it on the foreground but we're in the foreground state before the user sees it so the change isn't visible to them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main issue I was trying to solve is when
_currentColorScheme
was set to nil initially, the default color scheme waslight
so on first render it would belight
regardless of the users settings. So if the users device was in dark mode on initial render it would be reported aslight
until you toggled it and then it synced upThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, the changes to
_currentColorScheme
makes sense, I'm just a bit confused about how the RCTRootView changes are related here. Is it required as part of this PR?Where does this happen? Does
traitCollectionDidChange
when the application is foregrounded?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, currently
traitCollectionDidChange
will fire twice when the app moves to the background because the OS is taking the snapshots for the app switcher. It'll take one using the current scheme. Switch to the opposite and then switch back to the original. The changes inRCTRootView
is just to ignore those changes.