Skip to content

Commit 45d7df6

Browse files
Jack Wangfacebook-github-bot
Jack Wang
authored andcommitted
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
1 parent ee88e72 commit 45d7df6

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import android.content.Context;
1111
import android.content.res.Configuration;
12+
import androidx.annotation.Nullable;
1213
import com.facebook.fbreact.specs.NativeAppearanceSpec;
1314
import com.facebook.react.bridge.Arguments;
1415
import com.facebook.react.bridge.ReactApplicationContext;
@@ -26,13 +27,34 @@ public class AppearanceModule extends NativeAppearanceSpec {
2627

2728
private String mColorScheme = "light";
2829

30+
private final @Nullable OverrideColorScheme mOverrideColorScheme;
31+
32+
/** Optional override to the current color scheme */
33+
public interface OverrideColorScheme {
34+
35+
/**
36+
* Color scheme will use the return value instead of the current system configuration. Available
37+
* scheme: {light, dark}
38+
*/
39+
public String getScheme();
40+
}
41+
2942
public AppearanceModule(ReactApplicationContext reactContext) {
43+
this(reactContext, null);
44+
}
45+
46+
public AppearanceModule(
47+
ReactApplicationContext reactContext, @Nullable OverrideColorScheme overrideColorScheme) {
3048
super(reactContext);
3149

3250
mColorScheme = colorSchemeForCurrentConfiguration(reactContext);
51+
mOverrideColorScheme = overrideColorScheme;
3352
}
3453

35-
private static String colorSchemeForCurrentConfiguration(Context context) {
54+
private String colorSchemeForCurrentConfiguration(Context context) {
55+
if (mOverrideColorScheme != null) {
56+
return mOverrideColorScheme.getScheme();
57+
}
3658
int currentNightMode =
3759
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
3860
switch (currentNightMode) {

0 commit comments

Comments
 (0)