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
  • Loading branch information
brentvatne committed Jun 17, 2020
1 parent 28ec77d commit 48e27f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ dependencies {
api("com.facebook.infer.annotation:infer-annotation:0.11.2")
api("com.facebook.yoga:proguard-annotations:1.14.1")
api("javax.inject:javax.inject:1")
api("androidx.appcompat:appcompat:1.0.2")
api("androidx.appcompat:appcompat:1.1.0")
api("androidx.swiperefreshlayout:swiperefreshlayout:1.0.0")
api("com.facebook.fresco:fresco:${FRESCO_VERSION}")
api("com.facebook.fresco:imagepipeline-okhttp3:${FRESCO_VERSION}")
api("com.facebook.soloader:soloader:${SO_LOADER_VERSION}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;

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

/** Module that exposes the user's preferred color scheme. */
@ReactModule(name = AppearanceModule.NAME)
public class AppearanceModule extends ReactContextBaseJavaModule {
Expand All @@ -33,7 +36,21 @@ public AppearanceModule(ReactApplicationContext reactContext) {
mColorScheme = colorSchemeForCurrentConfiguration(reactContext);
}

private static String colorSchemeForCurrentConfiguration(Context context) {
// 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) {
// 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 48e27f6

Please sign in to comment.