From d570735615bed895e69fb993f63ef55358782810 Mon Sep 17 00:00:00 2001 From: kirillzyusko Date: Tue, 10 Dec 2024 00:04:24 +0100 Subject: [PATCH] fix: use system preference (not app level) on Android, since app level doesn't force the keyboard to match the preferred theme --- .../extensions/ThemedReactContext.kt | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/android/src/main/java/com/reactnativekeyboardcontroller/extensions/ThemedReactContext.kt b/android/src/main/java/com/reactnativekeyboardcontroller/extensions/ThemedReactContext.kt index 0422328f9..d9749898e 100644 --- a/android/src/main/java/com/reactnativekeyboardcontroller/extensions/ThemedReactContext.kt +++ b/android/src/main/java/com/reactnativekeyboardcontroller/extensions/ThemedReactContext.kt @@ -1,6 +1,7 @@ package com.reactnativekeyboardcontroller.extensions -import android.content.res.Configuration +import android.content.Context +import android.os.Build import com.facebook.react.bridge.ReactContext import com.facebook.react.bridge.WritableMap import com.facebook.react.modules.core.DeviceEventManagerModule @@ -38,10 +39,16 @@ fun ThemedReactContext?.emitEvent( val ThemedReactContext?.appearance: String get() = - this?.let { - when (it.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { - Configuration.UI_MODE_NIGHT_NO -> "light" - Configuration.UI_MODE_NIGHT_YES -> "dark" - else -> "default" - } - } ?: "default" + when { + this == null -> "default" + isSystemDarkMode(this) -> "dark" + else -> "light" + } + +private fun isSystemDarkMode(context: Context): Boolean = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + (context.getSystemService(Context.UI_MODE_SERVICE) as? android.app.UiModeManager) + ?.nightMode == android.app.UiModeManager.MODE_NIGHT_YES + } else { + false + }