Skip to content

Commit 6cd33f6

Browse files
alanleedevfacebook-github-bot
authored andcommitted
Add ThemePreferences listener for dark mode updates
Summary: This change implements the internal Facebook-specific listener mechanism for dynamic dark mode updates in FB4A. It builds on the OSS changes in D88427482 which added the `addListener()` API to `OverrideColorScheme` interface. **Why this is needed:** Previously, when users toggled dark mode in FB4A Settings, React Native JavaScript would not be notified of the change. The `AppearanceModule` would only reflect the initial color scheme state from instantiation, causing UI inconsistencies when users changed their theme preference in native settings UI. **How it works:** 1. User toggles dark mode in FB4A Settings → `ThemePreferences.setDarkMode()` is called 2. `ThemePreferences` notifies all registered listeners (including `FB4AThemeOverrideColorScheme`) 3. `FB4AThemeOverrideColorScheme` forwards the notification to `AppearanceModule` via the listener callback 4. `AppearanceModule.onConfigurationChanged()` is invoked (set up in OSS changes) 5. If the color scheme changed, an `appearanceChanged` event is emitted to React Native JavaScript Reviewed By: xiphirx Differential Revision: D88431860
1 parent f965a0c commit 6cd33f6

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,6 +2465,7 @@ public final class com/facebook/react/modules/appearance/AppearanceModule : com/
24652465
public fun addListener (Ljava/lang/String;)V
24662466
public final fun emitAppearanceChanged (Ljava/lang/String;)V
24672467
public fun getColorScheme ()Ljava/lang/String;
2468+
public fun invalidate ()V
24682469
public final fun invalidatePlatformColorCache ()V
24692470
public final fun onConfigurationChanged (Landroid/content/Context;)V
24702471
public fun removeListeners (D)V

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ constructor(
2828

2929
private var lastEmittedColorScheme: String? = null
3030

31+
private val schemeChangeListener: () -> Unit = {
32+
val activity = reactApplicationContext.getCurrentActivity()
33+
onConfigurationChanged(activity ?: reactApplicationContext)
34+
}
35+
3136
init {
3237
// Register as a listener for color scheme changes if override is provided
33-
overrideColorScheme?.addSchemeChangeListener {
34-
val activity = reactApplicationContext.getCurrentActivity()
35-
onConfigurationChanged(activity ?: reactApplicationContext)
36-
}
38+
overrideColorScheme?.addSchemeChangeListener(schemeChangeListener)
3739
}
3840

3941
/** Optional override to the current color scheme */
@@ -125,6 +127,12 @@ constructor(
125127
Companion.invalidatePlatformColorCache?.run()
126128
}
127129

130+
public override fun invalidate() {
131+
overrideColorScheme?.removeSchemeChangeListener(schemeChangeListener)
132+
invalidatePlatformColorCache()
133+
super.invalidate()
134+
}
135+
128136
public companion object {
129137
public const val NAME: String = NativeAppearanceSpec.NAME
130138
private const val APPEARANCE_CHANGED_EVENT_NAME = "appearanceChanged"

0 commit comments

Comments
 (0)