mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Adding interface callback for dynamic color scheme
Summary: Changelog: [Android] [Added] Adding: - OverrideColorScheme interface to AppearanceModule - setOverrideColorScheme method to AppearanceModule This allows RN surfaces's color scheme to be overriden by custom theme from the app. When set, AppearanceModule will use the value from OverrideColorScheme instead of system theme (light/dark) Reviewed By: JoshuaGross Differential Revision: D20405810 fbshipit-source-id: 8e562148a75231781649b615fdaf3368beeb477d
This commit is contained in:
committed by
Facebook GitHub Bot
parent
ee88e72c02
commit
45d7df6cf7
+23
-1
@@ -9,6 +9,7 @@ package com.facebook.react.modules.appearance;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.fbreact.specs.NativeAppearanceSpec;
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
@@ -26,13 +27,34 @@ public class AppearanceModule extends NativeAppearanceSpec {
|
||||
|
||||
private String mColorScheme = "light";
|
||||
|
||||
private final @Nullable OverrideColorScheme mOverrideColorScheme;
|
||||
|
||||
/** Optional override to the current color scheme */
|
||||
public interface OverrideColorScheme {
|
||||
|
||||
/**
|
||||
* Color scheme will use the return value instead of the current system configuration. Available
|
||||
* scheme: {light, dark}
|
||||
*/
|
||||
public String getScheme();
|
||||
}
|
||||
|
||||
public AppearanceModule(ReactApplicationContext reactContext) {
|
||||
this(reactContext, null);
|
||||
}
|
||||
|
||||
public AppearanceModule(
|
||||
ReactApplicationContext reactContext, @Nullable OverrideColorScheme overrideColorScheme) {
|
||||
super(reactContext);
|
||||
|
||||
mColorScheme = colorSchemeForCurrentConfiguration(reactContext);
|
||||
mOverrideColorScheme = overrideColorScheme;
|
||||
}
|
||||
|
||||
private static String colorSchemeForCurrentConfiguration(Context context) {
|
||||
private String colorSchemeForCurrentConfiguration(Context context) {
|
||||
if (mOverrideColorScheme != null) {
|
||||
return mOverrideColorScheme.getScheme();
|
||||
}
|
||||
int currentNightMode =
|
||||
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
|
||||
switch (currentNightMode) {
|
||||
|
||||
Reference in New Issue
Block a user