Skip to content

Commit

Permalink
update android AppearanceModule to support dark mode in all OS versions
Browse files Browse the repository at this point in the history
Summary:
We no longer need to gate by OS version since we want to allow in-app theming. This diff ensures that we are passing in the updated system context to retrieve the correct app theme.

Changelog:
[Android] Enable AppearanceModule for all OS versions

Reviewed By: mdvacca

Differential Revision: D18224915

fbshipit-source-id: 42d5db8497d8bead32c49e3e2a25d4ba779e2b33
  • Loading branch information
Elisa Lou authored and facebook-github-bot committed Oct 31, 2019
1 parent b5080f7 commit 27d7d3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -727,12 +727,14 @@ public void onWindowFocusChange(boolean hasFocus) {

/** Call this from {@link Activity#onConfigurationChanged()}. */
@ThreadConfined(UI)
public void onConfigurationChanged(@Nullable Configuration newConfig) {
public void onConfigurationChanged(Context updatedContext, @Nullable Configuration newConfig) {
UiThreadUtil.assertOnUiThread();

ReactContext currentContext = getCurrentReactContext();
if (currentContext != null) {
currentContext.getNativeModule(AppearanceModule.class).onConfigurationChanged();
ReactContext currentReactContext = getCurrentReactContext();
if (currentReactContext != null) {
currentReactContext
.getNativeModule(AppearanceModule.class)
.onConfigurationChanged(updatedContext);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand All @@ -18,7 +17,7 @@
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;

/** Module that exposes the user's preferred color scheme. For API >= 29. */
/** Module that exposes the user's preferred color scheme. */
@ReactModule(name = AppearanceModule.NAME)
public class AppearanceModule extends ReactContextBaseJavaModule {

Expand All @@ -35,16 +34,13 @@ public AppearanceModule(ReactApplicationContext reactContext) {
}

private static String colorSchemeForCurrentConfiguration(Context context) {
// Night Mode is only available in Android P and up.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
int currentNightMode =
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return "light";
case Configuration.UI_MODE_NIGHT_YES:
return "dark";
}
int currentNightMode =
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return "light";
case Configuration.UI_MODE_NIGHT_YES:
return "dark";
}

return "light";
Expand Down Expand Up @@ -73,8 +69,8 @@ public void removeListeners(double count) {}
* Call this from your root activity whenever configuration changes. If the
* color scheme has changed, an event will emitted.
*/
public void onConfigurationChanged() {
String newColorScheme = colorSchemeForCurrentConfiguration(getReactApplicationContext());
public void onConfigurationChanged(Context currentContext) {
String newColorScheme = colorSchemeForCurrentConfiguration(currentContext);
if (!mColorScheme.equals(newColorScheme)) {
mColorScheme = newColorScheme;
emitAppearanceChanged(mColorScheme);
Expand Down

0 comments on commit 27d7d3f

Please sign in to comment.