Skip to content

Commit ef7350f

Browse files
committed
fix: modifications to theme configs didn't take effect
- refactor: use ThemeStub as placeholder to list theme, and initialize real theme when selecting
1 parent 4e7eec0 commit ef7350f

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

app/src/main/java/com/osfans/trime/data/theme/Theme.kt

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package com.osfans.trime.data.theme
66

7+
import com.osfans.trime.core.Rime
78
import com.osfans.trime.data.theme.mapper.GeneralStyleMapper
89
import com.osfans.trime.data.theme.model.ColorScheme
910
import com.osfans.trime.data.theme.model.GeneralStyle
@@ -15,7 +16,14 @@ import timber.log.Timber
1516
class Theme(
1617
val configId: String,
1718
) {
18-
private val config = Config.create(configId)
19+
private val config: Config
20+
21+
init {
22+
if (!Rime.deployRimeConfigFile(configId, CONFIG_VERSION_KEY)) {
23+
throw IllegalArgumentException("Failed to deploy theme config file '$configId'")
24+
}
25+
config = Config.create(configId)
26+
}
1927

2028
val name = config.getString("name")
2129
val generalStyle = mapToGeneralStyle()
@@ -62,4 +70,8 @@ class Theme(
6270
)
6371
return generalStyle
6472
}
73+
74+
companion object {
75+
private const val CONFIG_VERSION_KEY = "config_version"
76+
}
6577
}

app/src/main/java/com/osfans/trime/data/theme/ThemeFilesManager.kt

+19-12
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,36 @@
55

66
package com.osfans.trime.data.theme
77

8-
import com.osfans.trime.core.Rime
9-
import com.osfans.trime.data.base.DataManager
8+
import com.charleskorn.kaml.AnchorsAndAliases
9+
import com.charleskorn.kaml.Yaml
10+
import com.charleskorn.kaml.YamlConfiguration
1011
import timber.log.Timber
1112
import java.io.File
1213

1314
object ThemeFilesManager {
14-
private const val CONFIG_VERSION_KEY = "config_version"
15+
private val yaml =
16+
Yaml(
17+
configuration =
18+
YamlConfiguration(
19+
strictMode = false,
20+
anchorsAndAliases = AnchorsAndAliases.Permitted(200u),
21+
),
22+
)
1523

16-
fun listThemes(dir: File): MutableList<Theme> {
24+
fun listThemes(dir: File): MutableList<ThemeStub> {
1725
val files = dir.listFiles { _, name -> name.endsWith("trime.yaml") } ?: return mutableListOf()
1826
return files
1927
.sortedByDescending { it.lastModified() }
2028
.mapNotNull decode@{
21-
val deployed = DataManager.userDataDir.resolve("build/${it.name}")
22-
if (!deployed.exists()) {
23-
if (!Rime.deployRimeConfigFile(it.name, CONFIG_VERSION_KEY)) {
24-
Timber.w("Failed to deploy theme file ${it.absolutePath}")
25-
return@decode null
26-
}
27-
}
2829
val theme =
2930
runCatching {
30-
Theme(it.nameWithoutExtension)
31+
yaml
32+
.decodeFromString(
33+
ThemeStub.serializer(),
34+
it.inputStream().bufferedReader().readText(),
35+
).apply {
36+
configId = it.nameWithoutExtension
37+
}
3138
}.getOrElse { e ->
3239
Timber.w("Failed to decode theme file ${it.absolutePath}: ${e.message}")
3340
return@decode null

app/src/main/java/com/osfans/trime/data/theme/ThemeManager.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object ThemeManager {
1515
fun onThemeChange(theme: Theme)
1616
}
1717

18-
fun getAllThemes(): List<Theme> {
18+
fun getAllThemes(): List<ThemeStub> {
1919
val sharedThemes = ThemeFilesManager.listThemes(DataManager.sharedDataDir)
2020
val userThemes = ThemeFilesManager.listThemes(DataManager.userDataDir)
2121
return sharedThemes + userThemes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2015 - 2025 Rime community
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package com.osfans.trime.data.theme
7+
8+
import kotlinx.serialization.Serializable
9+
import kotlinx.serialization.Transient
10+
11+
@Serializable
12+
data class ThemeStub(
13+
@Transient
14+
var configId: String = "",
15+
val name: String,
16+
)

app/src/main/java/com/osfans/trime/ui/main/settings/ThemePickerDialog.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.app.AlertDialog
88
import android.content.Context
99
import androidx.lifecycle.LifecycleCoroutineScope
1010
import com.osfans.trime.R
11+
import com.osfans.trime.data.theme.Theme
1112
import com.osfans.trime.data.theme.ThemeManager
1213
import kotlinx.coroutines.Dispatchers
1314
import kotlinx.coroutines.launch
@@ -39,8 +40,8 @@ object ThemePickerDialog {
3940
scope.launch {
4041
afterConfirm?.invoke()
4142
if (which == selectedIndex) return@launch
42-
val newTheme = allThemes[which]
43-
ThemeManager.selectTheme(newTheme)
43+
val newStub = allThemes[which]
44+
ThemeManager.selectTheme(Theme(newStub.configId))
4445
dialog.dismiss()
4546
}
4647
}

0 commit comments

Comments
 (0)