Skip to content

Commit 4b28ec3

Browse files
brentvatneKudo
authored andcommitted
[ReactAndroid] Respect local night mode configuration for activity to play nicely with Expo client color scheme locking mechanism. expo/expo#8793
This is a squashed commits with the following changes: commit 945096e Author: Kudo Chien <[email protected]> Date: Tue Jul 27 16:49:09 2021 +0800 [android] Upgrade androidx.appcompat to 1.2.0 Backport from: expo/expo@58fa52e
1 parent 86950d4 commit 4b28ec3

File tree

1 file changed

+16
-0
lines changed
  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance

1 file changed

+16
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import com.facebook.react.bridge.WritableMap;
1919
import com.facebook.react.module.annotations.ReactModule;
2020

21+
import androidx.appcompat.app.AppCompatActivity;
22+
import androidx.appcompat.app.AppCompatDelegate;
23+
2124
/** Module that exposes the user's preferred color scheme. */
2225
@ReactModule(name = NativeAppearanceSpec.NAME)
2326
public class AppearanceModule extends NativeAppearanceSpec {
@@ -50,10 +53,23 @@ public AppearanceModule(
5053
mColorScheme = colorSchemeForCurrentConfiguration(reactContext);
5154
}
5255

56+
// NOTE(brentvatne): this was static previously, but it wasn't necessary and we need it to not be
57+
// in order to use getCurrentActivity
5358
private String colorSchemeForCurrentConfiguration(Context context) {
5459
if (mOverrideColorScheme != null) {
5560
return mOverrideColorScheme.getScheme();
5661
}
62+
// NOTE(brentvatne): Same code (roughly) that we use in ExpoAppearanceModule to get the config
63+
// as set by ExperienceActivityUtils to force the dark/light mode config on the activity
64+
if (getCurrentActivity() instanceof AppCompatActivity) {
65+
int mode = ((AppCompatActivity) getCurrentActivity()).getDelegate().getLocalNightMode();
66+
switch (mode) {
67+
case AppCompatDelegate.MODE_NIGHT_YES:
68+
return "dark";
69+
case AppCompatDelegate.MODE_NIGHT_NO:
70+
return "light";
71+
}
72+
}
5773
int currentNightMode =
5874
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
5975
switch (currentNightMode) {

0 commit comments

Comments
 (0)