Skip to content

Commit

Permalink
Fix dark mode (#105)
Browse files Browse the repository at this point in the history
* Delete duplicated test

* test

* test

* test

* Cleanup

* Bring back EMGLocale
  • Loading branch information
trevor-e authored Nov 2, 2023
1 parent e172796 commit 5e491e7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 57 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.res.Configuration.UI_MODE_NIGHT_MASK
import android.content.res.Resources
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density
Expand All @@ -22,16 +23,22 @@ fun SnapshotVariantProvider(
val fontScale = config.fontScale ?: 1.0f
val fontScaleDensity = Density(fontScale = fontScale, density = LocalDensity.current.density)

val locale = config.locale?.let { EMGLocale.forLanguageCode(it) } ?: Locale.getDefault()

val wrappedContext = SnapshotVariantContextWrapper(
LocalContext.current,
config.locale?.let {
EMGLocale.forLanguageCode(it)
} ?: Locale.getDefault(),
config.uiMode,
newLocale = locale,
newUiMode = config.uiMode,
)

val localConfiguration = Configuration(LocalConfiguration.current).apply {
config.uiMode?.let { uiMode = it }
setLocale(locale)
}

val providedValues = arrayOf(
LocalContext provides wrappedContext,
LocalConfiguration provides localConfiguration,
config.fontScale?.let { LocalDensity provides fontScaleDensity }
)
CompositionLocalProvider(
Expand All @@ -48,28 +55,39 @@ class SnapshotVariantContextWrapper(
private val newLocale: Locale?,
private val newUiMode: Int?
) : ContextWrapper(base) {
private var customResources: Resources? = null

private var wrappedContext: Context? = null

override fun getResources(): Resources {
if (customResources == null) {
val res = super.getResources()
val newConfig = Configuration(res.configuration)
newLocale?.let {
newConfig.setLocale(it)
}
newUiMode?.let {
newConfig.uiMode = it or (newConfig.uiMode and UI_MODE_NIGHT_MASK.inv())
}
customResources = super.createConfigurationContext(newConfig).resources
if (wrappedContext == null) {
wrappedContext = updateContext()
}
return customResources!!
return wrappedContext!!.resources
}

override fun createConfigurationContext(overrideConfiguration: Configuration): Context {
return SnapshotVariantContextWrapper(
super.createConfigurationContext(overrideConfiguration),
newLocale,
newUiMode
)
val context = super.createConfigurationContext(overrideConfiguration)
newLocale?.let {
overrideConfiguration.setLocale(it)
}
newUiMode?.let {
overrideConfiguration.uiMode = it or (overrideConfiguration.uiMode and UI_MODE_NIGHT_MASK.inv())
}
return context
}

private fun updateContext(): Context {
val res = super.getResources()
val config = Configuration(res.configuration)

newLocale?.let {
config.setLocale(it)
}

newUiMode?.let {
config.uiMode = it or (config.uiMode and UI_MODE_NIGHT_MASK.inv())
}

return createConfigurationContext(config)
}
}

0 comments on commit 5e491e7

Please sign in to comment.