Skip to content

Commit

Permalink
Adding interface callback for dynamic color scheme
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jack Wang authored and facebook-github-bot committed Mar 14, 2020
1 parent ee88e72 commit 45d7df6
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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;
Expand All @@ -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) {
Expand Down

0 comments on commit 45d7df6

Please sign in to comment.