Skip to content

Commit 5b37469

Browse files
committed
refactor(data, ime): reform the getters of the keyboard style parameters
- slightly reform the basic getters - create Style class to organize the keyboard style parameter getters
1 parent 119b4ff commit 5b37469

14 files changed

Lines changed: 160 additions & 216 deletions

File tree

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

Lines changed: 58 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public static Config get() {
6767
return self;
6868
}
6969

70-
private Map<String, Object> defaultKeyboardStyle;
7170
private String currentThemeName, soundPackageName, currentSound;
7271
private static final String defaultThemeName = "trime";
7372
private String currentSchemaId, currentColorSchemeId;
@@ -77,33 +76,31 @@ public static Config get() {
7776
private Map<String, Map<String, Object>> presetKeyboards;
7877
private Map<String, Object> liquidKeyboard;
7978

79+
public Style style;
80+
8081
public Config() {
8182
this(false);
8283
}
8384

8485
public Config(boolean skipDeploy) {
85-
String methodName =
86-
"\t<TrimeInit>\t" + Thread.currentThread().getStackTrace()[2].getMethodName() + "\t";
87-
Timber.d(methodName);
8886
self = this;
8987
currentThemeName = appPrefs.getThemeAndColor().getSelectedTheme();
9088
soundPackageName = appPrefs.getKeyboard().getSoundPackage();
9189

92-
Timber.d(methodName + "sync");
90+
Timber.d("Syncing asset data ...");
9391
DataManager.sync();
9492
Rime.get(!DataManager.INSTANCE.getSharedDataDir().exists());
9593

9694
// 正常逻辑不应该部署全部主题,init()方法已经做过当前主题的部署
9795
// Timber.d(methodName + "deployTheme");
9896
// deployTheme();
9997

100-
Timber.d(methodName + "init");
10198
init(true);
10299

103-
Timber.d(methodName + "setSoundFromColor");
100+
Timber.d("Setting sound from color ...");
104101
setSoundFromColor();
105102

106-
Timber.d(methodName + "finish");
103+
Timber.d("Initialization finished");
107104
}
108105

109106
public String getTheme() {
@@ -147,7 +144,7 @@ private void applySoundPackage(File file, String name) {
147144
}
148145
in.close();
149146
out.close();
150-
Timber.i("applySoundPackage = " + name);
147+
Timber.i("applySoundPackage=%s", name);
151148
} catch (Exception e) {
152149
e.printStackTrace();
153150
}
@@ -198,9 +195,9 @@ public void init(boolean skipDeployment) {
198195
Objects.requireNonNull(fullThemeConfigMap, "The theme file cannot be empty!");
199196
Timber.d("Fetching done");
200197

201-
defaultKeyboardStyle = (Map<String, Object>) fullThemeConfigMap.get("style");
198+
style = new Style((Map<String, Object>) fullThemeConfigMap.get("style"));
202199
fallbackColors = (Map<String, String>) fullThemeConfigMap.get("fallback_colors");
203-
Key.presetKeys = (Map<String, Map<String, String>>) fullThemeConfigMap.get("preset_keys");
200+
Key.presetKeys = (Map<String, Map<String, Object>>) fullThemeConfigMap.get("preset_keys");
204201
presetColorSchemes =
205202
(Map<String, Map<String, String>>) fullThemeConfigMap.get("preset_color_schemes");
206203
presetKeyboards =
@@ -250,18 +247,19 @@ public static Object obtainValue(Map<String, Object> map, @NonNull String vararg
250247
}
251248
}
252249

253-
public static String obtainString(Map<String, Object> map, @NonNull String key, @NonNull String defValue) {
254-
if (map == null || map.isEmpty() || key.isEmpty() || !map.containsKey(key)) return defValue;
255-
final String v;
256-
return ((v = (String) obtainValue(map, key)) != null) ? v : defValue;
250+
public static String obtainString(
251+
Map<String, Object> map, @NonNull String key, @NonNull String defValue) {
252+
if (map == null || map.isEmpty() || key.isEmpty()) return defValue;
253+
final Object v;
254+
return ((v = obtainValue(map, key)) != null) ? v.toString() : defValue;
257255
}
258256

259257
public static String obtainString(Map<String, Object> map, @NonNull String key) {
260258
return obtainString(map, key, "");
261259
}
262260

263261
public static int obtainInt(Map<String, Object> map, @NonNull String key, int defValue) {
264-
if (map == null || map.isEmpty() || key.isEmpty() || !map.containsKey(key)) return defValue;
262+
if (map == null || map.isEmpty() || key.isEmpty()) return defValue;
265263
final String nm;
266264
try {
267265
return (!(nm = obtainString(map, key)).isEmpty()) ? Long.decode(nm).intValue() : defValue;
@@ -275,7 +273,7 @@ public static int obtainInt(Map<String, Object> map, @NonNull String key) {
275273
}
276274

277275
public static float obtainFloat(Map<String, Object> map, @NonNull String key, float defValue) {
278-
if (map == null || map.isEmpty() || key.isEmpty() || !map.containsKey(key)) return defValue;
276+
if (map == null || map.isEmpty() || key.isEmpty()) return defValue;
279277
final String s;
280278
try {
281279
return (!(s = obtainString(map, key)).isEmpty()) ? Float.parseFloat(s) : defValue;
@@ -288,61 +286,46 @@ public static float obtainFloat(Map<String, Object> map, @NonNull String key) {
288286
return obtainFloat(map, key, 0f);
289287
}
290288

291-
public static boolean obtainBoolean(Map<String, Object> map, @NonNull String key, boolean defValue) {
292-
if (map == null || map.isEmpty() || key.isEmpty() || !map.containsKey(key)) return defValue;
289+
public static boolean obtainBoolean(
290+
Map<String, Object> map, @NonNull String key, boolean defValue) {
291+
if (map == null || map.isEmpty() || key.isEmpty()) return defValue;
293292
return Boolean.parseBoolean(obtainString(map, key));
294293
}
295294

296295
public static boolean obtainBoolean(Map<String, Object> map, @NonNull String key) {
297296
return obtainBoolean(map, key, false);
298297
}
299298

300-
@Nullable
301-
private Object _getValue(String k1, String k2) {
302-
if (defaultKeyboardStyle != null && defaultKeyboardStyle.containsKey(k1)) {
303-
final Map<String, Object> m = (Map<String, Object>) defaultKeyboardStyle.get(k1);
304-
if (m != null && m.containsKey(k2)) return m.get(k2);
299+
public static class Style {
300+
private final Map<String, Object> styleConfigMap;
301+
302+
public Style(final Map<String, Object> styleConfigMap) {
303+
this.styleConfigMap = styleConfigMap;
305304
}
306-
return null;
307-
}
308305

309-
private Object _getValue(String k1, String k2, Object defaultValue) {
310-
if (defaultKeyboardStyle != null && defaultKeyboardStyle.containsKey(k1)) {
311-
final Map<String, Object> m = (Map<String, Object>) defaultKeyboardStyle.get(k1);
312-
if (m != null && m.containsKey(k2)) return m.get(k2);
306+
public String getString(@NonNull String key) {
307+
return obtainString(styleConfigMap, key);
313308
}
314-
return defaultValue;
315-
}
316309

317-
@Nullable
318-
private Object _getValue(String k1) {
319-
if (defaultKeyboardStyle != null && defaultKeyboardStyle.containsKey(k1))
320-
return defaultKeyboardStyle.get(k1);
321-
return null;
322-
}
310+
public int getInt(@NonNull String key) {
311+
return obtainInt(styleConfigMap, key);
312+
}
323313

324-
private Object _getValue(String k1, Object defaultValue) {
325-
if (defaultKeyboardStyle != null && defaultKeyboardStyle.containsKey(k1))
326-
return defaultKeyboardStyle.get(k1);
327-
return defaultValue;
328-
}
314+
public float getFloat(@NonNull String key) {
315+
return obtainFloat(styleConfigMap, key);
316+
}
329317

330-
public Object getValue(@NonNull String s) {
331-
final String[] ss = s.split("/");
332-
if (ss.length == 1) return _getValue(ss[0]);
333-
else if (ss.length == 2) return _getValue(ss[0], ss[1]);
334-
return null;
335-
}
318+
public boolean getBoolean(@NonNull String key) {
319+
return obtainBoolean(styleConfigMap, key);
320+
}
336321

337-
public Object getValue(@NonNull String s, Object defaultValue) {
338-
final String[] ss = s.split("/");
339-
if (ss.length == 1) return _getValue(ss[0], defaultValue);
340-
else if (ss.length == 2) return _getValue(ss[0], ss[1], defaultValue);
341-
return null;
322+
public Object getObject(@NonNull String key) {
323+
return obtainValue(styleConfigMap, key);
324+
}
342325
}
343326

344327
public boolean hasKey(String s) {
345-
return getValue(s) != null;
328+
return style.getObject(s) != null;
346329
}
347330

348331
private String getKeyboardName(@NonNull String name) {
@@ -374,7 +357,7 @@ private String getKeyboardName(@NonNull String name) {
374357
}
375358

376359
public List<String> getKeyboardNames() {
377-
final List<String> names = (List<String>) getValue("keyboards");
360+
final List<String> names = (List<String>) style.getObject("keyboards");
378361
final List<String> keyboards = new ArrayList<>();
379362
for (String s : names) {
380363
s = getKeyboardName(s);
@@ -419,7 +402,7 @@ public Map<String, Object> getLiquidKeyboard() {
419402
}
420403

421404
public void destroy() {
422-
if (defaultKeyboardStyle != null) defaultKeyboardStyle.clear();
405+
if (style != null) style = null;
423406
self = null;
424407
}
425408

@@ -479,12 +462,12 @@ private static int getPixel(Float f) {
479462
}
480463

481464
public int getPixel(String key) {
482-
return getPixel(getFloat(key));
465+
return getPixel(style.getFloat(key));
483466
}
484467

485468
public int getPixel(String key, int defaultValue) {
486-
float v = getFloat(key, Float.MAX_VALUE);
487-
if (v == Float.MAX_VALUE) return defaultValue;
469+
float v = style.getFloat(key);
470+
if (v == 0f) return defaultValue;
488471
return getPixel(v);
489472
}
490473

@@ -546,61 +529,13 @@ public static Object getValue(@NonNull Map<?, ?> m, String k, Object o) {
546529
return m.containsKey(k) ? m.get(k) : o;
547530
}
548531

549-
@NonNull
550-
public static String getString(Map<?, ?> m, String k, Object s) {
551-
final Object o = getValue(m, k, s);
552-
if (o == null) return "";
553-
return o.toString();
554-
}
555-
556-
@NonNull
557-
public static String getString(Map<?, ?> m, String k) {
558-
return getString(m, k, "");
559-
}
560-
561-
public boolean getBoolean(String key) {
562-
final Object o = getValue(key);
563-
if (o == null) return true;
564-
return Boolean.parseBoolean(o.toString());
565-
}
566-
567-
public double getDouble(String key) {
568-
final Object o = getValue(key);
569-
if (o == null) return 0d;
570-
return Double.parseDouble(o.toString());
571-
}
572-
573-
public float getFloat(String key) {
574-
final Object o = getValue(key);
575-
if (o == null) return 0f;
576-
return Float.parseFloat(o.toString());
577-
}
578-
579532
public float getLiquidFloat(String key) {
580533
if (liquidKeyboard != null) {
581534
if (liquidKeyboard.containsKey(key)) {
582-
return ConfigGetter.getFloat(liquidKeyboard, key, 0);
535+
return obtainFloat(liquidKeyboard, key, 0);
583536
}
584537
}
585-
return getFloat(key);
586-
}
587-
588-
public float getFloat(String key, float defaultValue) {
589-
final Object o = getValue(key, defaultValue);
590-
if (o == null) return defaultValue;
591-
return Float.parseFloat(o.toString());
592-
}
593-
594-
public int getInt(String key) {
595-
final Object o = getValue(key);
596-
if (o == null) return 0;
597-
return Long.decode(o.toString()).intValue();
598-
}
599-
600-
public String getString(String key) {
601-
final Object o = getValue(key);
602-
if (o == null) return "";
603-
return o.toString();
538+
return style.getFloat(key);
604539
}
605540

606541
// 获取当前配色方案的key的value,或者从fallback获取值。
@@ -627,7 +562,8 @@ private String getColorValue(String key) {
627562
*/
628563
private String getColorSchemeName() {
629564
String schemeId = appPrefs.getThemeAndColor().getSelectedColor();
630-
if (!presetColorSchemes.containsKey(schemeId)) schemeId = getString("color_scheme"); // 主題中指定的配色
565+
if (!presetColorSchemes.containsKey(schemeId))
566+
schemeId = style.getString("color_scheme"); // 主題中指定的配色
631567
if (!presetColorSchemes.containsKey(schemeId)) schemeId = "default"; // 主題中的default配色
632568
Map<String, String> colorMap = presetColorSchemes.get(schemeId);
633569
if (colorMap.containsKey("dark_scheme") || colorMap.containsKey("light_scheme"))
@@ -649,7 +585,8 @@ public boolean hasDarkLight() {
649585
*/
650586
private String getColorSchemeName(boolean darkMode) {
651587
String scheme = appPrefs.getThemeAndColor().getSelectedColor();
652-
if (!presetColorSchemes.containsKey(scheme)) scheme = getString("color_scheme"); // 主題中指定的配色
588+
if (!presetColorSchemes.containsKey(scheme))
589+
scheme = style.getString("color_scheme"); // 主題中指定的配色
653590
if (!presetColorSchemes.containsKey(scheme)) scheme = "default"; // 主題中的default配色
654591
Map<String, String> colorMap = presetColorSchemes.get(scheme);
655592
if (darkMode) {
@@ -724,9 +661,9 @@ public Map<String, String> getmEnterLabels() {
724661
}
725662

726663
public void initEnterLabels() {
727-
Object enter_labels = getValue("enter_labels");
728-
if (enter_labels == null) mEnterLabels = new HashMap<>();
729-
else mEnterLabels = (Map<String, String>) enter_labels;
664+
if ((mEnterLabels = (Map<String, String>) style.getObject("enter_labels")) == null) {
665+
mEnterLabels = new HashMap<>();
666+
}
730667

731668
String defaultEnterLabel = "Enter";
732669
if (mEnterLabels.containsKey("default")) defaultEnterLabel = mEnterLabels.get("default");
@@ -742,7 +679,7 @@ public void initEnterLabels() {
742679
}
743680

744681
public Typeface getFont(String key) {
745-
final String name = getString(key);
682+
final String name = style.getString(key);
746683
if (name != null) {
747684
final File f = new File(DataManager.getDataDir("fonts"), name);
748685
if (f.exists()) return Typeface.createFromFile(f);
@@ -865,7 +802,7 @@ public Drawable getDrawable(
865802
if (drawable != null) {
866803
if (alphaKey != null) {
867804
if (hasKey(alphaKey)) {
868-
int alpha = getInt("layout/alpha");
805+
int alpha = style.getInt("layout/alpha");
869806
if (alpha <= 0) alpha = 0;
870807
else if (alpha >= 255) alpha = 255;
871808
drawable.setAlpha(alpha);
@@ -879,7 +816,7 @@ public Drawable getDrawable(
879816
if (!(o instanceof Integer)) return null;
880817
gd.setColor((int) o);
881818

882-
if (roundCornerKey != null) gd.setCornerRadius(getFloat(roundCornerKey));
819+
if (roundCornerKey != null) gd.setCornerRadius(style.getFloat(roundCornerKey));
883820

884821
if (borderColorKey != null && borderKey != null) {
885822
int border = getPixel(borderKey);
@@ -891,7 +828,7 @@ public Drawable getDrawable(
891828

892829
if (alphaKey != null) {
893830
if (hasKey(alphaKey)) {
894-
int alpha = getInt("layout/alpha");
831+
int alpha = style.getInt("layout/alpha");
895832
if (alpha <= 0) alpha = 0;
896833
else if (alpha >= 255) alpha = 255;
897834
gd.setAlpha(alpha);
@@ -927,7 +864,7 @@ public Drawable getDrawableBitmap_(String key) {
927864
public void initCurrentColors() {
928865
curcentColors.clear();
929866
currentColorSchemeId = getColorSchemeName();
930-
backgroundFolder = getString("background_folder");
867+
backgroundFolder = style.getString("background_folder");
931868
Timber.d("Initializing currentColors ...");
932869
Timber.d(
933870
"currentColorSchemeId = %s, currentThemeName = %s, currentSchemaId = %s",
@@ -974,7 +911,7 @@ public void initCurrentColors() {
974911
public void initCurrentColors(boolean darkMode) {
975912
curcentColors.clear();
976913
currentColorSchemeId = getColorSchemeName(darkMode);
977-
backgroundFolder = getString("background_folder");
914+
backgroundFolder = style.getString("background_folder");
978915
Timber.d("Initializing currentColors ...");
979916
Timber.d(
980917
"currentColorSchemeId = %s, currentThemeName = %s, currentSchemaId = %s, isDarkMode = %s",

app/src/main/java/com/osfans/trime/ime/core/Speech.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void onResults(Bundle results) {
109109
if (trime != null) {
110110
final ArrayList<String> matches =
111111
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
112-
final String openccConfig = Config.get().getString("speech_opencc_config");
112+
final String openccConfig = Config.get().style.getString("speech_opencc_config");
113113
for (String result : matches) trime.commitText(Rime.openccConvert(result, openccConfig));
114114
}
115115
}

0 commit comments

Comments
 (0)