Skip to content

Commit

Permalink
fix(Rime): switches on the bar toggle to the opposite states as they …
Browse files Browse the repository at this point in the history
…show

The second parameter of `Rime.setOption` at the original line 245 (now line 241) should have been set to `nextOrReserved == 1`, while `nextOrReserved` is the correct index indicated the next state.

Also makes other minor refactors.
  • Loading branch information
WhiredPlanck committed Dec 14, 2022
1 parent 4b4c272 commit 272cbdc
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions app/src/main/java/com/osfans/trime/core/Rime.java
Expand Up @@ -37,6 +37,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import kotlin.collections.CollectionsKt;
import kotlin.collections.IndexedValue;
import kotlinx.coroutines.channels.BufferOverflow;
import kotlinx.coroutines.flow.FlowKt;
import kotlinx.coroutines.flow.MutableSharedFlow;
Expand Down Expand Up @@ -149,17 +151,13 @@ public static class RimeStatus {
}

/** Rime方案 */
@SuppressWarnings("unchecked")
public static class RimeSchema {
private Map<String, Object> schema;
private List<Map<String, Object>> switches;
public Map<String, Object> symbolMap;

public RimeSchema(@NonNull String schemaId) {
Timber.d("RimeSchema <init>, schemaId=%s", schemaId);
if ((schema = (Map<String, Object>) getRimeSchemaValue(schemaId, "schema")) == null) {
Timber.w("Failed to parse schema meta info, fallback to empty collection");
schema = new HashMap<>();
}
if ((switches = (List<Map<String, Object>>) getRimeSchemaValue(schemaId, "switches"))
== null) {
Timber.w("Failed to parse schema status switches, fallback to empty collection");
Expand Down Expand Up @@ -208,24 +206,22 @@ public RimeCandidate[] getStatusSwitches() {

public void updateSwitchOptions() {
if (switches.isEmpty()) return; // 無方案
int i = 0;
for (final Map<String, Object> s : switches) {
for (final IndexedValue<Map<String, Object>> is : CollectionsKt.withIndex(switches)) {
final Map<String, Object> s = is.getValue();
if (s.containsKey("options")) { // 带有一系列 Rime 运行时选项的开关,找到启用的选项并标记
final List<String> options = (List<String>) s.get("options");
assert options != null;
int j = 0;
for (final String option : options) {
if (Rime.get_option(option)) {
for (final IndexedValue<String> io : CollectionsKt.withIndex(options)) {
if (Rime.get_option(io.getValue())) {
// 将启用状态标记为此选项的索引值,方便切换时直接从选项列表中获取
s.put("enabled", j);
s.put("enabled", io.getIndex());
break;
}
j++;
}
} else { // 只有单 Rime 运行时选项的开关,开关名即选项名,标记其启用状态
s.put("enabled", Rime.get_option(s.get("name").toString()) ? 1 : 0);
s.put("enabled", Rime.get_option((String) s.get("name")) ? 1 : 0);
}
switches.set(i++, s);
switches.set(is.getIndex(), s);
}
}

Expand All @@ -242,7 +238,7 @@ public void toggleSwitchOption(int index) {
Rime.setOption(options.get(nextOrReserved), true);
} else {
nextOrReserved = 1 - enabled;
Rime.setOption(s.get("name").toString(), enabled == 1);
Rime.setOption((String) s.get("name"), nextOrReserved == 1);
}
s.put("enabled", nextOrReserved);
switches.set(index, s);
Expand Down

0 comments on commit 272cbdc

Please sign in to comment.