Skip to content

Commit

Permalink
refactor(data, ime, util): migrate more parameters from Config ...
Browse files Browse the repository at this point in the history
to the related components for detail parsing to reduce it complexity.
  • Loading branch information
WhiredPlanck committed Dec 13, 2022
1 parent 0789403 commit 756bc78
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 100 deletions.
60 changes: 4 additions & 56 deletions app/src/main/java/com/osfans/trime/data/theme/Config.java
Expand Up @@ -33,10 +33,8 @@
import com.osfans.trime.core.Rime;
import com.osfans.trime.data.AppPrefs;
import com.osfans.trime.data.DataManager;
import com.osfans.trime.ime.enums.SymbolKeyboardType;
import com.osfans.trime.ime.keyboard.Key;
import com.osfans.trime.ime.keyboard.Sound;
import com.osfans.trime.ime.symbol.TabManager;
import com.osfans.trime.util.DimensionsKt;
import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -168,11 +166,7 @@ public void init() {
keyboards = new Keyboards(this);
long end = System.currentTimeMillis();
Timber.d("Setting up all theme config map takes %s ms", end - start);
initLiquidKeyboard();
Timber.d("init() initLiquidKeyboard done");
Timber.d("init() reset done");
initCurrentColors();
initEnterLabels();
Timber.i("The theme is initialized");
long initEnd = System.currentTimeMillis();
Timber.d("Initializing cache takes %s ms", initEnd - end);
Expand Down Expand Up @@ -284,6 +278,10 @@ public Liquid(@NonNull final Config theme) {
this.theme = theme;
}

public Object getObject(@NonNull String key) {
return obtainValue(theme.liquidKeyboard, key);
}

public int getInt(@NonNull String key) {
return obtainInt(theme.liquidKeyboard, key);
}
Expand Down Expand Up @@ -349,32 +347,6 @@ public boolean hasKey(String s) {
return style.getObject(s) != null;
}

public void initLiquidKeyboard() {
TabManager.clear();
if (liquidKeyboard == null) return;
Timber.d("Initializing LiquidKeyboard ...");
final List<String> names = (List<String>) liquidKeyboard.get("keyboards");
if (names == null) return;
for (String s : names) {
String name = s;
if (liquidKeyboard.containsKey(name)) {
Map<String, Object> keyboard = (Map<String, Object>) liquidKeyboard.get(name);
if (keyboard != null) {
if (keyboard.containsKey("name")) {
name = (String) keyboard.get("name");
}
if (keyboard.containsKey("type")) {
TabManager.get()
.addTab(
name,
SymbolKeyboardType.Companion.fromObject(keyboard.get("type")),
keyboard.get("keys"));
}
}
}
}
}

public void destroy() {
if (style != null) style = null;
if (liquid != null) liquid = null;
Expand Down Expand Up @@ -580,30 +552,6 @@ public List<Pair<String, String>> getPresetColorSchemes() {
entry -> new Pair<>(entry.getKey(), Objects.requireNonNull(entry.getValue().get("name"))));
}

private Map<String, String> mEnterLabels;

public Map<String, String> getmEnterLabels() {
return mEnterLabels;
}

public void initEnterLabels() {
if ((mEnterLabels = (Map<String, String>) style.getObject("enter_labels")) == null) {
mEnterLabels = new HashMap<>();
}

String defaultEnterLabel = "Enter";
if (mEnterLabels.containsKey("default")) defaultEnterLabel = mEnterLabels.get("default");
else mEnterLabels.put("default", defaultEnterLabel);

if (!mEnterLabels.containsKey("done")) mEnterLabels.put("done", defaultEnterLabel);
if (!mEnterLabels.containsKey("go")) mEnterLabels.put("go", defaultEnterLabel);
if (!mEnterLabels.containsKey("next")) mEnterLabels.put("next", defaultEnterLabel);
if (!mEnterLabels.containsKey("none")) mEnterLabels.put("none", defaultEnterLabel);
if (!mEnterLabels.containsKey("pre")) mEnterLabels.put("pre", defaultEnterLabel);
if (!mEnterLabels.containsKey("search")) mEnterLabels.put("search", defaultEnterLabel);
if (!mEnterLabels.containsKey("send")) mEnterLabels.put("send", defaultEnterLabel);
}

// 返回drawable。参数可以是颜色或者图片。如果参数缺失,返回null
private Drawable drawableObject(Object o) {
if (o == null) return null;
Expand Down
26 changes: 8 additions & 18 deletions app/src/main/java/com/osfans/trime/ime/enums/SymbolKeyboardType.kt
Expand Up @@ -52,32 +52,22 @@ enum class SymbolKeyboardType {
PAIR;

companion object {
private val convertMap: HashMap<String, SymbolKeyboardType> = hashMapOf()

init {
for (type in values()) {
convertMap[type.toString()] = type
}
}

fun fromString(code: String): SymbolKeyboardType {
val type = convertMap[code.uppercase(Locale.getDefault())]
return type ?: SINGLE
}

fun fromObject(code: Any?): SymbolKeyboardType {
if (code == null)
return SINGLE
val type = convertMap[code.toString().uppercase(Locale.getDefault())]
return type ?: SINGLE
@JvmStatic
fun fromString(code: String?): SymbolKeyboardType {
code ?: return SINGLE
return runCatching {
valueOf(code.uppercase())
}.getOrDefault(SINGLE)
}

// 是否在liquidKeyboard键盘区域展示按键
@JvmStatic
fun hasKeys(type: SymbolKeyboardType): Boolean {
return type > HISTORY
}

// 是否呈现在liquidKeyboard键盘区域的tabs列表中
@JvmStatic
fun hasKey(type: SymbolKeyboardType): Boolean {
return type >= CLIPBOARD
}
Expand Down
Expand Up @@ -245,6 +245,27 @@ public void resetEnterLabel() {
labelEnter = mEnterLabels.get("default");
}

private void handleEnterLabel(@NonNull Config theme) {
if ((mEnterLabels = (Map<String, String>) theme.style.getObject("enter_labels")) == null) {
mEnterLabels = new HashMap<>();
}

final String defaultEnterLabel;
if (mEnterLabels.containsKey("default")) {
defaultEnterLabel = mEnterLabels.get("default");
} else {
defaultEnterLabel = "Enter";
mEnterLabels.put("default", defaultEnterLabel);
}

for (final String label :
new String[] {"done", "go", "next", "none", "pre", "search", "send"}) {
if (!mEnterLabels.containsKey(label)) {
mEnterLabels.put(label, defaultEnterLabel);
}
}
}

public void setEnterLabel(int action, CharSequence actionLabel) {
// enter_label_mode 取值:
// 0不使用,1只使用actionlabel,2优先使用,3当其他方式没有获得label时才读取actionlabel
Expand Down Expand Up @@ -400,7 +421,7 @@ public void reset() {
mPaintSymbol.setTextSize(mSymbolSize);
mPreviewText.setTypeface(FontManager.getTypeface(config.style.getString("preview_font")));

mEnterLabels = config.getmEnterLabels();
handleEnterLabel(config);
enterLabelMode = config.style.getInt("enter_label_mode");
invalidateAllKeys();
}
Expand Down
15 changes: 8 additions & 7 deletions app/src/main/java/com/osfans/trime/ime/symbol/LiquidKeyboard.kt
Expand Up @@ -30,6 +30,7 @@ import kotlin.math.ceil

class LiquidKeyboard(private val context: Context) {
private val theme: Config = Config.get()
private val tabManager: TabManager = TabManager.get()
private val service: Trime = Trime.getService()
private var rootView: View? = null
private lateinit var keyboardView: RecyclerView
Expand Down Expand Up @@ -71,18 +72,18 @@ class LiquidKeyboard(private val context: Context) {
SymbolKeyboardType.CLIPBOARD,
SymbolKeyboardType.COLLECTION,
SymbolKeyboardType.DRAFT -> {
TabManager.get().select(i)
tabManager.select(i)
initDbData(tag.type)
}
SymbolKeyboardType.CANDIDATE -> {
TabManager.get().select(i)
tabManager.select(i)
initCandidates()
}
SymbolKeyboardType.VAR_LENGTH -> {
initVarLengthKeys(TabManager.get().select(i))
initVarLengthKeys(tabManager.select(i))
}
SymbolKeyboardType.SYMBOL, SymbolKeyboardType.HISTORY, SymbolKeyboardType.TABS -> {
TabManager.get().select(i)
tabManager.select(i)
initFixData(i)
}
else -> initFixData(i)
Expand Down Expand Up @@ -179,7 +180,7 @@ class LiquidKeyboard(private val context: Context) {
KeyCommandType.DEL_LEFT, KeyCommandType.DEL_RIGHT, KeyCommandType.REDO, KeyCommandType.UNDO -> {}
else -> {}
}
} else if (TabManager.get().isAfterTabSwitch(position)) {
} else if (tabManager.isAfterTabSwitch(position)) {
// tab的位置在“更多”的右侧,不滚动tab,焦点仍然在”更多“上
select(position)
} else {
Expand Down Expand Up @@ -208,9 +209,9 @@ class LiquidKeyboard(private val context: Context) {
SymbolKeyboardType.HISTORY ->
simpleAdapter.updateBeans(historyBeans!!)
SymbolKeyboardType.TABS ->
simpleAdapter.updateBeans(TabManager.get().tabSwitchData)
simpleAdapter.updateBeans(tabManager.tabSwitchData)
else ->
simpleAdapter.updateBeans(TabManager.get().select(i))
simpleAdapter.updateBeans(tabManager.select(i))
}
Timber.d("Tab #%s with bean size %s", i, simpleAdapter.itemCount)
}
Expand Down
44 changes: 26 additions & 18 deletions app/src/main/java/com/osfans/trime/ime/symbol/TabManager.java
Expand Up @@ -2,19 +2,21 @@

import androidx.annotation.NonNull;
import com.osfans.trime.core.Rime;
import com.osfans.trime.data.theme.Config;
import com.osfans.trime.ime.enums.KeyCommandType;
import com.osfans.trime.ime.enums.SymbolKeyboardType;
import com.osfans.trime.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TabManager {
private int selected;
private final List<SimpleKeyBean> keyboardData;
private final List<SimpleKeyBean> tabSwitchData;
private final ArrayList<TabTag> tabTags;
private int selected = 0;
private final List<SimpleKeyBean> keyboardData = new ArrayList<>();
private final List<SimpleKeyBean> tabSwitchData = new ArrayList<>();
private final ArrayList<TabTag> tabTags = new ArrayList<>();
private int tabSwitchPosition = 0;
private final List<List<SimpleKeyBean>> keyboards;
private final List<List<SimpleKeyBean>> keyboards = new ArrayList<>();
private static TabManager self;
private final List<SimpleKeyBean> notKeyboard = new ArrayList<>();
private final TabTag tagExit = new TabTag("返回", SymbolKeyboardType.NO_KEY, KeyCommandType.EXIT);
Expand All @@ -28,17 +30,12 @@ public List<SimpleKeyBean> getTabSwitchData() {
if (tabSwitchData.size() > 0) return tabSwitchData;

for (TabTag tag : tabTags) {
if (SymbolKeyboardType.Companion.hasKey(tag.type))
tabSwitchData.add(new SimpleKeyBean(tag.text));
if (SymbolKeyboardType.hasKey(tag.type)) tabSwitchData.add(new SimpleKeyBean(tag.text));
else tabSwitchData.add(new SimpleKeyBean(""));
}
return tabSwitchData;
}

public static void clear() {
self = new TabManager();
}

public static TabTag getTag(int i) {
return self.tabTags.get(i);
}
Expand Down Expand Up @@ -66,17 +63,28 @@ public static int getTagIndex(SymbolKeyboardType type) {
}

private TabManager() {
selected = 0;
tabTags = new ArrayList<>();
keyboards = new ArrayList<>();
keyboardData = new ArrayList<>();
tabSwitchData = new ArrayList<>();
final Config theme = Config.get();
final List<String> availables = (List<String>) theme.liquid.getObject("keyboards");
if (availables != null) {
for (final String id : availables) {
final Map<String, Object> keyboard;
if ((keyboard = (Map<String, Object>) theme.liquid.getObject(id)) != null) {
final String name = (String) CollectionUtils.getOrDefault(keyboard, "name", id);
if (keyboard.containsKey("type")) {
addTab(
name,
SymbolKeyboardType.fromString((String) keyboard.get("type")),
keyboard.get("keys"));
}
}
}
}
}

public void addTab(@NonNull String name, SymbolKeyboardType type, List<SimpleKeyBean> keyBeans) {
if (name.trim().length() < 1) return;
if (name.trim().isEmpty()) return;

if (SymbolKeyboardType.Companion.hasKeys(type)) {
if (SymbolKeyboardType.hasKeys(type)) {
for (int i = 0; i < tabTags.size(); i++) {
TabTag tag = tabTags.get(i);
if (tag.text.equals(name)) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/osfans/trime/util/CollectionUtils.kt
@@ -0,0 +1,6 @@
package com.osfans.trime.util

object CollectionUtils {
@JvmStatic
fun <K, V> getOrDefault(map: Map<K, V>, key: K, defaultValue: V): V = map[key] ?: defaultValue
}

0 comments on commit 756bc78

Please sign in to comment.