From 7041ed28f092e7c6c1dac35f0ce6af095cd9a072 Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Mon, 9 Sep 2024 04:30:50 -0700 Subject: [PATCH] fix(android): ensure Appearance change listener does not skip events (#46017) Summary: I'm able to reproduce a case when Appearance module methods are called in the following order: starting point: dark mode enabled 1. call `setColorScheme` light 2. call `getColorScheme`, which sets `colorScheme` to light [here](https://github.com/facebook/react-native/blob/7bb7a6037bd78bbfa6d9e8499973ea921e9c70e1/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt#L57) 3. [onConfigurationChanged](https://github.com/facebook/react-native/blob/7bb7a6037bd78bbfa6d9e8499973ea921e9c70e1/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt#L82) is called but `if (colorScheme != newColorScheme)` does not evaluate to true, so no event is dispatched to JS. That means JS is not in sync with the native state. The issue was the `getColorScheme` had a side-effect of setting `colorScheme` private member (not sure what its use was). The fix remembers the last emitted color scheme value and emits event if new value is different. ## Changelog: [ANDROID] [FIXED] - ensure Appearance change listener does not skip events Pull Request resolved: https://github.com/facebook/react-native/pull/46017 Test Plan: tested locally with RN tester Reviewed By: NickGerleman Differential Revision: D62016949 Pulled By: cipolleschi fbshipit-source-id: b7b5755d38becda655cf376749d9a996daff7e07 --- .../modules/appearance/AppearanceModule.kt | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt index 6aaf2b1cfbb..e58f108f1eb 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt @@ -24,7 +24,7 @@ constructor( private val overrideColorScheme: OverrideColorScheme? = null ) : NativeAppearanceSpec(reactContext) { - private var colorScheme = colorSchemeForCurrentConfiguration(reactContext) + private var lastEmittedColorScheme: String? = null /** Optional override to the current color scheme */ public fun interface OverrideColorScheme { @@ -42,10 +42,10 @@ constructor( val currentNightMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - when (currentNightMode) { - Configuration.UI_MODE_NIGHT_NO -> return "light" - Configuration.UI_MODE_NIGHT_YES -> return "dark" - else -> return "light" + return when (currentNightMode) { + Configuration.UI_MODE_NIGHT_NO -> "light" + Configuration.UI_MODE_NIGHT_YES -> "dark" + else -> "light" } } @@ -54,16 +54,16 @@ constructor( // scheme. This covers the scenario when AppCompatDelegate.setDefaultNightMode() // is called directly (which can occur in Brownfield apps for example). val activity = getCurrentActivity() - colorScheme = colorSchemeForCurrentConfiguration(activity ?: getReactApplicationContext()) - return colorScheme + return colorSchemeForCurrentConfiguration(activity ?: getReactApplicationContext()) } public override fun setColorScheme(style: String) { - when { - style == "dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) - style == "light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) - style == "unspecified" -> + when (style) { + "dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) + "light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) + "unspecified" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) + } } @@ -79,9 +79,9 @@ constructor( */ public fun onConfigurationChanged(currentContext: Context) { val newColorScheme = colorSchemeForCurrentConfiguration(currentContext) - if (colorScheme != newColorScheme) { - colorScheme = newColorScheme - emitAppearanceChanged(colorScheme) + if (lastEmittedColorScheme != newColorScheme) { + lastEmittedColorScheme = newColorScheme + emitAppearanceChanged(newColorScheme) } }