Skip to content

Commit

Permalink
fix(data): make all properties optional when deserializing a schema c…
Browse files Browse the repository at this point in the history
…onfig

A schema config (*.schema.yaml) can only require `schema/schema_id` and without any other property.
  • Loading branch information
WhiredPlanck committed Jan 11, 2023
1 parent 0f86f10 commit 5f33a55
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
26 changes: 13 additions & 13 deletions app/src/main/java/com/osfans/trime/data/schema/RimeSchema.kt
Expand Up @@ -7,45 +7,45 @@ import kotlinx.serialization.Transient
@Serializable
data class RimeSchema(
@SerialName("__build_info")
val buildInfo: BuildInfo, // 构建信息
val buildInfo: BuildInfo? = null, // 构建信息
val schema: Schema, // 方案信息
val switches: List<Switch>?, // 选项开关
val punctuator: Punctuator, // 标点
val speller: Speller // 拼写器
val switches: List<Switch> = listOf(), // 选项开关
val punctuator: Punctuator? = null, // 标点
val speller: Speller? = null // 拼写器
) {
@Serializable
data class BuildInfo(
@SerialName("rime_version")
val rimeVersion: String,
val timestamps: Map<String, Long>
val timestamps: Map<String, Long> = mapOf()
)

@Serializable
data class Schema(
val author: List<String>,
val author: List<String> = listOf(),
@SerialName("schema_id")
val schemaId: String,
val version: String,
val dependencies: List<String>,
val description: String
val version: String? = null,
val dependencies: List<String> = listOf(),
val description: String? = null
)

@Serializable
data class Switch(
val name: String? = null,
val options: List<String>? = null,
val options: List<String> = listOf(),
val reset: Int? = null,
val states: List<String>? = null,
val states: List<String> = listOf(),
@Transient var enabled: Int = 0,
)

@Serializable
data class Punctuator(
val symbols: Map<String, List<String>>?
val symbols: Map<String, List<String>> = mapOf()
)

@Serializable
data class Speller(
val alphabet: String?
val alphabet: String? = null
)
}
25 changes: 10 additions & 15 deletions app/src/main/java/com/osfans/trime/data/schema/SchemaManager.kt
Expand Up @@ -30,8 +30,8 @@ object SchemaManager {
RimeSchema.serializer(),
raw
)
visibleSwitches = currentSchema.switches ?: listOf<RimeSchema.Switch>()
.filter { !it.states.isNullOrEmpty() } // 剔除没有 states 条目项的值,它们不作为开关使用
visibleSwitches = currentSchema.switches
.filter { it.states.isNotEmpty() } // 剔除没有 states 条目项的值,它们不作为开关使用
updateSwitchOptions()
}

Expand All @@ -41,17 +41,12 @@ object SchemaManager {
@JvmStatic
fun updateSwitchOptions() {
if (!this::visibleSwitches.isInitialized || visibleSwitches.isEmpty()) return // 無方案
for (s in visibleSwitches) {
if (s.options.isNullOrEmpty()) { // 只有单 Rime 运行时选项的开关,开关名即选项名,标记其启用状态
s.enabled = if (Rime.getRimeOption(s.name!!)) 1 else 0
visibleSwitches.forEach { s ->
s.enabled = if (s.options.isEmpty()) { // 只有单 Rime 运行时选项的开关,开关名即选项名,标记其启用状态
Rime.getRimeOption(s.name!!).compareTo(false)
} else { // 带有一系列 Rime 运行时选项的开关,找到启用的选项并标记
for ((j, o) in s.options.withIndex()) {
if (Rime.getRimeOption(o)) {
// 将启用状态标记为此选项的索引值,方便切换时直接从选项列表中获取
s.enabled = j
break
}
}
// 将启用状态标记为此选项的索引值,方便切换时直接从选项列表中获取
s.options.indexOfFirst { Rime.getRimeOption(it) }
}
}
}
Expand All @@ -62,7 +57,7 @@ object SchemaManager {
val switch = visibleSwitches[index]
val enabled = switch.enabled
val next: Int
if (switch.options.isNullOrEmpty()) {
if (switch.options.isEmpty()) {
next = 1 - switch.enabled
Rime.setOption(switch.name!!, next == 1)
} else {
Expand All @@ -80,8 +75,8 @@ object SchemaManager {
return Array(visibleSwitches.size) {
val switch = visibleSwitches[it]
val enabled = switch.enabled
val text = switch.states!![enabled]
val comment = if (switch.options.isNullOrEmpty()) {
val text = switch.states[enabled]
val comment = if (switch.options.isEmpty()) {
"${if (arrow) "" else ""}${switch.states[1 - enabled]}"
} else ""
CandidateListItem(comment, text)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/osfans/trime/data/theme/Config.java
Expand Up @@ -27,6 +27,7 @@
import com.osfans.trime.core.Rime;
import com.osfans.trime.data.AppPrefs;
import com.osfans.trime.data.DataManager;
import com.osfans.trime.data.schema.RimeSchema;
import com.osfans.trime.data.schema.SchemaManager;
import com.osfans.trime.data.sound.SoundThemeManager;
import com.osfans.trime.ime.keyboard.Key;
Expand Down Expand Up @@ -303,7 +304,8 @@ public String remapKeyboardId(@NonNull String name) {
if (theme.presetKeyboards.containsKey(shortSchemaId)) {
return shortSchemaId;
} else {
final String alphabet = SchemaManager.getActiveSchema().getSpeller().getAlphabet();
final RimeSchema.Speller speller = SchemaManager.getActiveSchema().getSpeller();
final String alphabet = speller != null ? speller.getAlphabet() : null;
final String twentySix = "qwerty";
if (theme.presetKeyboards.containsKey(alphabet)) {
return alphabet;
Expand Down

0 comments on commit 5f33a55

Please sign in to comment.