File tree 5 files changed +52
-16
lines changed
app/src/main/java/com/osfans/trime
5 files changed +52
-16
lines changed Original file line number Diff line number Diff line change 4
4
5
5
package com.osfans.trime.data.theme
6
6
7
+ import com.osfans.trime.core.Rime
7
8
import com.osfans.trime.data.theme.mapper.GeneralStyleMapper
8
9
import com.osfans.trime.data.theme.model.ColorScheme
9
10
import com.osfans.trime.data.theme.model.GeneralStyle
@@ -15,7 +16,14 @@ import timber.log.Timber
15
16
class Theme (
16
17
val configId : String ,
17
18
) {
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
+ }
19
27
20
28
val name = config.getString(" name" )
21
29
val generalStyle = mapToGeneralStyle()
@@ -62,4 +70,8 @@ class Theme(
62
70
)
63
71
return generalStyle
64
72
}
73
+
74
+ companion object {
75
+ private const val CONFIG_VERSION_KEY = " config_version"
76
+ }
65
77
}
Original file line number Diff line number Diff line change 5
5
6
6
package com.osfans.trime.data.theme
7
7
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
10
11
import timber.log.Timber
11
12
import java.io.File
12
13
13
14
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
+ )
15
23
16
- fun listThemes (dir : File ): MutableList <Theme > {
24
+ fun listThemes (dir : File ): MutableList <ThemeStub > {
17
25
val files = dir.listFiles { _, name -> name.endsWith(" trime.yaml" ) } ? : return mutableListOf ()
18
26
return files
19
27
.sortedByDescending { it.lastModified() }
20
28
.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
- }
28
29
val theme =
29
30
runCatching {
30
- Theme (it.nameWithoutExtension)
31
+ yaml
32
+ .decodeFromString(
33
+ ThemeStub .serializer(),
34
+ it.inputStream().bufferedReader().readText(),
35
+ ).apply {
36
+ configId = it.nameWithoutExtension
37
+ }
31
38
}.getOrElse { e ->
32
39
Timber .w(" Failed to decode theme file ${it.absolutePath} : ${e.message} " )
33
40
return @decode null
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ object ThemeManager {
15
15
fun onThemeChange (theme : Theme )
16
16
}
17
17
18
- fun getAllThemes (): List <Theme > {
18
+ fun getAllThemes (): List <ThemeStub > {
19
19
val sharedThemes = ThemeFilesManager .listThemes(DataManager .sharedDataDir)
20
20
val userThemes = ThemeFilesManager .listThemes(DataManager .userDataDir)
21
21
return sharedThemes + userThemes
Original file line number Diff line number Diff line change
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
+ )
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import android.app.AlertDialog
8
8
import android.content.Context
9
9
import androidx.lifecycle.LifecycleCoroutineScope
10
10
import com.osfans.trime.R
11
+ import com.osfans.trime.data.theme.Theme
11
12
import com.osfans.trime.data.theme.ThemeManager
12
13
import kotlinx.coroutines.Dispatchers
13
14
import kotlinx.coroutines.launch
@@ -39,8 +40,8 @@ object ThemePickerDialog {
39
40
scope.launch {
40
41
afterConfirm?.invoke()
41
42
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) )
44
45
dialog.dismiss()
45
46
}
46
47
}
You can’t perform that action at this time.
0 commit comments