Skip to content

Commit

Permalink
fix: retain current scheme ID when changing day/night theme
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Mar 15, 2024
1 parent 5c3e867 commit 1f9fd83
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions app/src/main/java/com/osfans/trime/data/theme/ColorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,28 @@ object ColorManager {
}

/**
* 切换到指定配色,切换成功后写入 AppPrefs
* 切换到指定配色后,写入 AppPrefs
* @param colorSchemeId 配色 id
* */
fun setColorScheme(colorSchemeId: String) {
switchColorScheme(colorSchemeId)
selectedColor = colorSchemeId
prefs.selectedColor = colorSchemeId
}

/**
* 切换到指定配色
* @param colorSchemeId 配色 id
* */
private fun switchColorScheme(colorSchemeId: String) {
if (!presetColorSchemes.containsKey(colorSchemeId)) {
Timber.w("Color scheme %s not found", colorSchemeId)
return
}
Timber.d("switch color scheme from %s to %s", selectedColor, colorSchemeId)
selectedColor = colorSchemeId
// 刷新配色
val isFirst = currentColors.isEmpty()
refreshColorValues()
refreshColorValues(colorSchemeId)
if (isNightMode) {
lastDarkColorSchemeId = colorSchemeId
} else {
Expand All @@ -148,15 +157,14 @@ object ColorManager {
Timber.d("Initialization finished")
}
}
prefs.selectedColor = colorSchemeId
if (!isFirst) fireChange()
}

/** 切换深色/亮色模式 */
private fun switchNightMode(isNightMode: Boolean) {
this.isNightMode = isNightMode
val newId = getColorSchemeId()
if (newId != null) setColorScheme(newId)
if (newId != null) switchColorScheme(newId)
Timber.d(
"System changing color, current ColorScheme: $selectedColor, isDarkMode=$isNightMode",
)
Expand Down Expand Up @@ -185,9 +193,9 @@ object ColorManager {
return selectedColor
}

private fun refreshColorValues() {
private fun refreshColorValues(colorSchemeId: String) {
currentColors.clear()
val colorMap = presetColorSchemes[selectedColor]
val colorMap = presetColorSchemes[colorSchemeId]
colorMap?.forEach { (key, value) ->
when (key) {
"name", "author", "light_scheme", "dark_scheme", "sound" -> {}
Expand Down

0 comments on commit 1f9fd83

Please sign in to comment.