Skip to content

Commit

Permalink
[ReactAndroid] Respect local night mode configuration for activity to…
Browse files Browse the repository at this point in the history
… 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 <kudo@expo.io>
Date:   Tue Jul 27 16:49:09 2021 +0800

    [android] Upgrade androidx.appcompat to 1.2.0

    Backport from: expo/expo@58fa52e
  • Loading branch information
brentvatne authored and gabrieldonadel committed Oct 19, 2023
1 parent 7a803eb commit 6e1d017
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

/** Module that exposes the user's preferred color scheme. */
@ReactModule(name = NativeAppearanceSpec.NAME)
public class AppearanceModule extends NativeAppearanceSpec {
Expand Down Expand Up @@ -50,10 +53,23 @@ public AppearanceModule(
mColorScheme = colorSchemeForCurrentConfiguration(reactContext);
}

// NOTE(brentvatne): this was static previously, but it wasn't necessary and we need it to not be
// in order to use getCurrentActivity
private String colorSchemeForCurrentConfiguration(Context context) {
if (mOverrideColorScheme != null) {
return mOverrideColorScheme.getScheme();
}
// NOTE(brentvatne): Same code (roughly) that we use in ExpoAppearanceModule to get the config
// as set by ExperienceActivityUtils to force the dark/light mode config on the activity
if (getCurrentActivity() instanceof AppCompatActivity) {
int mode = ((AppCompatActivity) getCurrentActivity()).getDelegate().getLocalNightMode();
switch (mode) {
case AppCompatDelegate.MODE_NIGHT_YES:
return "dark";
case AppCompatDelegate.MODE_NIGHT_NO:
return "light";
}
}
int currentNightMode =
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
Expand Down

0 comments on commit 6e1d017

Please sign in to comment.