Skip to content

Commit 5f33a55

Browse files
committed
fix(data): make all properties optional when deserializing a schema config
A schema config (*.schema.yaml) can only require `schema/schema_id` and without any other property.
1 parent 0f86f10 commit 5f33a55

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

app/src/main/java/com/osfans/trime/data/schema/RimeSchema.kt

+13-13
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,45 @@ import kotlinx.serialization.Transient
77
@Serializable
88
data class RimeSchema(
99
@SerialName("__build_info")
10-
val buildInfo: BuildInfo, // 构建信息
10+
val buildInfo: BuildInfo? = null, // 构建信息
1111
val schema: Schema, // 方案信息
12-
val switches: List<Switch>?, // 选项开关
13-
val punctuator: Punctuator, // 标点
14-
val speller: Speller // 拼写器
12+
val switches: List<Switch> = listOf(), // 选项开关
13+
val punctuator: Punctuator? = null, // 标点
14+
val speller: Speller? = null // 拼写器
1515
) {
1616
@Serializable
1717
data class BuildInfo(
1818
@SerialName("rime_version")
1919
val rimeVersion: String,
20-
val timestamps: Map<String, Long>
20+
val timestamps: Map<String, Long> = mapOf()
2121
)
2222

2323
@Serializable
2424
data class Schema(
25-
val author: List<String>,
25+
val author: List<String> = listOf(),
2626
@SerialName("schema_id")
2727
val schemaId: String,
28-
val version: String,
29-
val dependencies: List<String>,
30-
val description: String
28+
val version: String? = null,
29+
val dependencies: List<String> = listOf(),
30+
val description: String? = null
3131
)
3232

3333
@Serializable
3434
data class Switch(
3535
val name: String? = null,
36-
val options: List<String>? = null,
36+
val options: List<String> = listOf(),
3737
val reset: Int? = null,
38-
val states: List<String>? = null,
38+
val states: List<String> = listOf(),
3939
@Transient var enabled: Int = 0,
4040
)
4141

4242
@Serializable
4343
data class Punctuator(
44-
val symbols: Map<String, List<String>>?
44+
val symbols: Map<String, List<String>> = mapOf()
4545
)
4646

4747
@Serializable
4848
data class Speller(
49-
val alphabet: String?
49+
val alphabet: String? = null
5050
)
5151
}

app/src/main/java/com/osfans/trime/data/schema/SchemaManager.kt

+10-15
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ object SchemaManager {
3030
RimeSchema.serializer(),
3131
raw
3232
)
33-
visibleSwitches = currentSchema.switches ?: listOf<RimeSchema.Switch>()
34-
.filter { !it.states.isNullOrEmpty() } // 剔除没有 states 条目项的值,它们不作为开关使用
33+
visibleSwitches = currentSchema.switches
34+
.filter { it.states.isNotEmpty() } // 剔除没有 states 条目项的值,它们不作为开关使用
3535
updateSwitchOptions()
3636
}
3737

@@ -41,17 +41,12 @@ object SchemaManager {
4141
@JvmStatic
4242
fun updateSwitchOptions() {
4343
if (!this::visibleSwitches.isInitialized || visibleSwitches.isEmpty()) return // 無方案
44-
for (s in visibleSwitches) {
45-
if (s.options.isNullOrEmpty()) { // 只有单 Rime 运行时选项的开关,开关名即选项名,标记其启用状态
46-
s.enabled = if (Rime.getRimeOption(s.name!!)) 1 else 0
44+
visibleSwitches.forEach { s ->
45+
s.enabled = if (s.options.isEmpty()) { // 只有单 Rime 运行时选项的开关,开关名即选项名,标记其启用状态
46+
Rime.getRimeOption(s.name!!).compareTo(false)
4747
} else { // 带有一系列 Rime 运行时选项的开关,找到启用的选项并标记
48-
for ((j, o) in s.options.withIndex()) {
49-
if (Rime.getRimeOption(o)) {
50-
// 将启用状态标记为此选项的索引值,方便切换时直接从选项列表中获取
51-
s.enabled = j
52-
break
53-
}
54-
}
48+
// 将启用状态标记为此选项的索引值,方便切换时直接从选项列表中获取
49+
s.options.indexOfFirst { Rime.getRimeOption(it) }
5550
}
5651
}
5752
}
@@ -62,7 +57,7 @@ object SchemaManager {
6257
val switch = visibleSwitches[index]
6358
val enabled = switch.enabled
6459
val next: Int
65-
if (switch.options.isNullOrEmpty()) {
60+
if (switch.options.isEmpty()) {
6661
next = 1 - switch.enabled
6762
Rime.setOption(switch.name!!, next == 1)
6863
} else {
@@ -80,8 +75,8 @@ object SchemaManager {
8075
return Array(visibleSwitches.size) {
8176
val switch = visibleSwitches[it]
8277
val enabled = switch.enabled
83-
val text = switch.states!![enabled]
84-
val comment = if (switch.options.isNullOrEmpty()) {
78+
val text = switch.states[enabled]
79+
val comment = if (switch.options.isEmpty()) {
8580
"${if (arrow) "" else ""}${switch.states[1 - enabled]}"
8681
} else ""
8782
CandidateListItem(comment, text)

app/src/main/java/com/osfans/trime/data/theme/Config.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.osfans.trime.core.Rime;
2828
import com.osfans.trime.data.AppPrefs;
2929
import com.osfans.trime.data.DataManager;
30+
import com.osfans.trime.data.schema.RimeSchema;
3031
import com.osfans.trime.data.schema.SchemaManager;
3132
import com.osfans.trime.data.sound.SoundThemeManager;
3233
import com.osfans.trime.ime.keyboard.Key;
@@ -303,7 +304,8 @@ public String remapKeyboardId(@NonNull String name) {
303304
if (theme.presetKeyboards.containsKey(shortSchemaId)) {
304305
return shortSchemaId;
305306
} else {
306-
final String alphabet = SchemaManager.getActiveSchema().getSpeller().getAlphabet();
307+
final RimeSchema.Speller speller = SchemaManager.getActiveSchema().getSpeller();
308+
final String alphabet = speller != null ? speller.getAlphabet() : null;
307309
final String twentySix = "qwerty";
308310
if (theme.presetKeyboards.containsKey(alphabet)) {
309311
return alphabet;

0 commit comments

Comments
 (0)