Skip to content

Commit

Permalink
fix: use system preference (not app level) on Android, since app leve…
Browse files Browse the repository at this point in the history
…l doesn't force the keyboard to match the preferred theme
  • Loading branch information
kirillzyusko committed Dec 9, 2024
1 parent 7f2c3d6 commit d570735
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit d570735

Please sign in to comment.