Skip to content

Commit d651a65

Browse files
committed
refactor: merge the usages of ConfigGetter into CollectionUtils
1 parent 00c6382 commit d651a65

File tree

5 files changed

+114
-112
lines changed

5 files changed

+114
-112
lines changed

app/src/main/java/com/osfans/trime/ime/keyboard/Event.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.osfans.trime.core.RimeKeyMapping;
2626
import com.osfans.trime.data.AppPrefs;
2727
import com.osfans.trime.ime.enums.Keycode;
28-
import com.osfans.trime.util.ConfigGetter;
28+
import com.osfans.trime.util.CollectionUtils;
2929
import java.util.HashMap;
3030
import java.util.List;
3131
import java.util.Locale;
@@ -73,15 +73,15 @@ public Event(Keyboard keyboard, @NonNull String s) {
7373
if (Key.presetKeys.containsKey(s)) {
7474
// todo 把presetKeys缓存为presetKeyEvents,减少重新载入
7575
Map<String, Object> presetKey = Key.presetKeys.get(s);
76-
command = ConfigGetter.getString(presetKey, "command", "");
77-
option = ConfigGetter.getString(presetKey, "option", "");
78-
select = ConfigGetter.getString(presetKey, "select", "");
79-
toggle = ConfigGetter.getString(presetKey, "toggle", "");
80-
label = ConfigGetter.getString(presetKey, "label", "");
81-
preview = ConfigGetter.getString(presetKey, "preview", "");
82-
shiftLock = ConfigGetter.getString(presetKey, "shift_lock", "");
83-
commit = ConfigGetter.getString(presetKey, "commit", "");
84-
String send = ConfigGetter.getString(presetKey, "send", "");
76+
command = CollectionUtils.obtainString(presetKey, "command", "");
77+
option = CollectionUtils.obtainString(presetKey, "option", "");
78+
select = CollectionUtils.obtainString(presetKey, "select", "");
79+
toggle = CollectionUtils.obtainString(presetKey, "toggle", "");
80+
label = CollectionUtils.obtainString(presetKey, "label", "");
81+
preview = CollectionUtils.obtainString(presetKey, "preview", "");
82+
shiftLock = CollectionUtils.obtainString(presetKey, "shift_lock", "");
83+
commit = CollectionUtils.obtainString(presetKey, "commit", "");
84+
String send = CollectionUtils.obtainString(presetKey, "send", "");
8585
if (TextUtils.isEmpty(send) && !TextUtils.isEmpty(command))
8686
send = "function"; // command默認發function
8787
int[] sends = Keycode.parseSend(send);
@@ -91,9 +91,9 @@ public Event(Keyboard keyboard, @NonNull String s) {
9191
text = (String) presetKey.get("text");
9292
if (code < 0 && TextUtils.isEmpty(text)) text = s;
9393
if (presetKey.containsKey("states")) states = (List<String>) presetKey.get("states");
94-
sticky = ConfigGetter.getBoolean(presetKey, "sticky", false);
95-
repeatable = ConfigGetter.getBoolean(presetKey, "repeatable", false);
96-
functional = ConfigGetter.getBoolean(presetKey, "functional", true);
94+
sticky = CollectionUtils.obtainBoolean(presetKey, "sticky", false);
95+
repeatable = CollectionUtils.obtainBoolean(presetKey, "repeatable", false);
96+
functional = CollectionUtils.obtainBoolean(presetKey, "functional", true);
9797
} else if ((code = getClickCode(s)) >= 0) {
9898
parseLabel();
9999
} else {

app/src/main/java/com/osfans/trime/ime/keyboard/Key.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.osfans.trime.data.theme.Theme;
2828
import com.osfans.trime.ime.enums.KeyEventType;
2929
import com.osfans.trime.util.CollectionUtils;
30-
import com.osfans.trime.util.ConfigGetter;
30+
import com.osfans.trime.util.DimensionsKt;
3131
import java.text.MessageFormat;
3232
import java.util.Arrays;
3333
import java.util.Locale;
@@ -131,26 +131,27 @@ public Key(Keyboard parent, Map<String, Object> mk) {
131131
}
132132
if (hasComposingKey) mKeyboard.getComposingKeys().add(this);
133133

134-
label = ConfigGetter.getString(mk, "label", "");
135-
labelSymbol = ConfigGetter.getString(mk, "label_symbol", "");
136-
hint = ConfigGetter.getString(mk, "hint", "");
134+
label = CollectionUtils.obtainString(mk, "label", "");
135+
labelSymbol = CollectionUtils.obtainString(mk, "label_symbol", "");
136+
hint = CollectionUtils.obtainString(mk, "hint", "");
137137
if (mk.containsKey("send_bindings")) {
138-
send_bindings = ConfigGetter.getBoolean(mk, "send_bindings", true);
138+
send_bindings = CollectionUtils.obtainBoolean(mk, "send_bindings", true);
139139
} else if (!hasComposingKey) {
140140
send_bindings = false;
141141
}
142142
}
143143

144144
mKeyboard.setModiferKey(getCode(), this);
145-
key_text_size = ConfigGetter.getPixel(mk, "key_text_size", 0);
146-
symbol_text_size = ConfigGetter.getPixel(mk, "symbol_text_size", 0);
145+
key_text_size = (int) DimensionsKt.sp2px(CollectionUtils.obtainFloat(mk, "key_text_size", 0));
146+
symbol_text_size =
147+
(int) DimensionsKt.sp2px(CollectionUtils.obtainFloat(mk, "symbol_text_size", 0));
147148
key_text_color = theme.colors.getColor(mk, "key_text_color");
148149
hilited_key_text_color = theme.colors.getColor(mk, "hilited_key_text_color");
149150
key_back_color = theme.colors.getDrawable(mk, "key_back_color");
150151
hilited_key_back_color = theme.colors.getDrawable(mk, "hilited_key_back_color");
151152
key_symbol_color = theme.colors.getColor(mk, "key_symbol_color");
152153
hilited_key_symbol_color = theme.colors.getColor(mk, "hilited_key_symbol_color");
153-
round_corner = ConfigGetter.getFloat(mk, "round_corner", 0);
154+
round_corner = CollectionUtils.obtainFloat(mk, "round_corner", 0);
154155
}
155156

156157
public static Map<String, Map<String, Object>> getPresetKeys() {

app/src/main/java/com/osfans/trime/ime/keyboard/Keyboard.java

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import android.view.KeyEvent;
2323
import com.blankj.utilcode.util.ScreenUtils;
2424
import com.osfans.trime.data.theme.Theme;
25-
import com.osfans.trime.util.ConfigGetter;
25+
import com.osfans.trime.util.CollectionUtils;
2626
import com.osfans.trime.util.DimensionsKt;
2727
import java.util.ArrayList;
2828
import java.util.List;
@@ -186,32 +186,38 @@ public Keyboard(String name) {
186186
} else {
187187
keyboardConfig = (Map<String, Object>) theme.keyboards.getObject("default");
188188
}
189-
mLabelTransform = ConfigGetter.getString(keyboardConfig, "label_transform", "none");
190-
mAsciiMode = ConfigGetter.getInt(keyboardConfig, "ascii_mode", 1);
189+
mLabelTransform = CollectionUtils.obtainString(keyboardConfig, "label_transform", "none");
190+
mAsciiMode = CollectionUtils.obtainInt(keyboardConfig, "ascii_mode", 1);
191191
if (mAsciiMode == 0)
192-
mAsciiKeyboard = ConfigGetter.getString(keyboardConfig, "ascii_keyboard", "");
193-
resetAsciiMode = ConfigGetter.getBoolean(keyboardConfig, "reset_ascii_mode", false);
194-
mLock = ConfigGetter.getBoolean(keyboardConfig, "lock", false);
195-
int columns = ConfigGetter.getInt(keyboardConfig, "columns", 30);
192+
mAsciiKeyboard = CollectionUtils.obtainString(keyboardConfig, "ascii_keyboard", "");
193+
resetAsciiMode = CollectionUtils.obtainBoolean(keyboardConfig, "reset_ascii_mode", false);
194+
mLock = CollectionUtils.obtainBoolean(keyboardConfig, "lock", false);
195+
int columns = CollectionUtils.obtainInt(keyboardConfig, "columns", 30);
196196
int defaultWidth =
197-
(int) (ConfigGetter.getDouble(keyboardConfig, "width", 0d) * mDisplayWidth / 100);
197+
(int) (CollectionUtils.obtainFloat(keyboardConfig, "width", 0f) * mDisplayWidth / 100);
198198
if (defaultWidth == 0) defaultWidth = mDefaultWidth;
199199

200200
// 按键高度取值顺序: keys > keyboard/height > style/key_height
201201
// 考虑到key设置height_land需要对皮肤做大量修改,而当部分key设置height而部分没有设时会造成按键高度异常,故取消普通按键的height_land参数
202-
int height = ConfigGetter.getPixel(keyboardConfig, "height", 0);
202+
int height = (int) DimensionsKt.sp2px(CollectionUtils.obtainFloat(keyboardConfig, "height", 0));
203203
int defaultHeight = (height > 0) ? height : mDefaultHeight;
204204
int rowHeight = defaultHeight;
205-
autoHeightIndex = ConfigGetter.getInt(keyboardConfig, "auto_height_index", -1);
205+
autoHeightIndex = CollectionUtils.obtainInt(keyboardConfig, "auto_height_index", -1);
206206
List<Map<String, Object>> lm = (List<Map<String, Object>>) keyboardConfig.get("keys");
207207

208208
mDefaultHorizontalGap =
209-
ConfigGetter.getPixel(
210-
keyboardConfig, "horizontal_gap", theme.style.getFloat("horizontal_gap"));
209+
(int)
210+
DimensionsKt.sp2px(
211+
CollectionUtils.obtainFloat(
212+
keyboardConfig, "horizontal_gap", theme.style.getFloat("horizontal_gap")));
211213
mDefaultVerticalGap =
212-
ConfigGetter.getPixel(keyboardConfig, "vertical_gap", theme.style.getFloat("vertical_gap"));
214+
(int)
215+
DimensionsKt.sp2px(
216+
CollectionUtils.obtainFloat(
217+
keyboardConfig, "vertical_gap", theme.style.getFloat("vertical_gap")));
213218
mRoundCorner =
214-
ConfigGetter.getFloat(keyboardConfig, "round_corner", theme.style.getFloat("round_corner"));
219+
CollectionUtils.obtainFloat(
220+
keyboardConfig, "round_corner", theme.style.getFloat("round_corner"));
215221

216222
Drawable background = theme.colors.getDrawable(keyboardConfig, "keyboard_back_color");
217223
if (background != null) mBackground = background;
@@ -227,9 +233,14 @@ public Keyboard(String name) {
227233
int[] newHeight = new int[0];
228234

229235
if (keyboardHeight > 0) {
230-
int mkeyboardHeight = ConfigGetter.getPixel(keyboardConfig, "keyboard_height", 0);
236+
int mkeyboardHeight =
237+
(int)
238+
DimensionsKt.sp2px(CollectionUtils.obtainFloat(keyboardConfig, "keyboard_height", 0));
231239
if (ScreenUtils.isLandscape()) {
232-
int mkeyBoardHeightLand = ConfigGetter.getPixel(keyboardConfig, "keyboard_height_land", 0);
240+
int mkeyBoardHeightLand =
241+
(int)
242+
DimensionsKt.sp2px(
243+
CollectionUtils.obtainFloat(keyboardConfig, "keyboard_height_land", 0));
233244
if (mkeyBoardHeightLand > 0) mkeyboardHeight = mkeyBoardHeightLand;
234245
}
235246

@@ -239,7 +250,7 @@ public Keyboard(String name) {
239250
List<Integer> rawHeight = new ArrayList<>();
240251
for (Map<String, Object> mk : lm) {
241252
int gap = mDefaultHorizontalGap;
242-
int w = (int) (ConfigGetter.getDouble(mk, "width", 0) * mDisplayWidth / 100);
253+
int w = (int) (CollectionUtils.obtainFloat(mk, "width", 0) * mDisplayWidth / 100);
243254
if (w == 0 && mk.containsKey("click")) w = defaultWidth;
244255
w -= gap;
245256
if (column >= maxColumns || x + w > mDisplayWidth) {
@@ -251,7 +262,7 @@ public Keyboard(String name) {
251262
rawHeight.add(rowHeight);
252263
}
253264
if (column == 0) {
254-
int heightK = ConfigGetter.getPixel(mk, "height", 0);
265+
int heightK = (int) DimensionsKt.sp2px(CollectionUtils.obtainFloat(mk, "height", 0));
255266
rowHeight = (heightK > 0) ? heightK : defaultHeight;
256267
}
257268
if (!mk.containsKey("click")) { // 無按鍵事件
@@ -323,7 +334,7 @@ public Keyboard(String name) {
323334
try {
324335
for (Map<String, Object> mk : lm) {
325336
int gap = mDefaultHorizontalGap;
326-
int w = (int) (ConfigGetter.getDouble(mk, "width", 0) * mDisplayWidth / 100);
337+
int w = (int) (CollectionUtils.obtainFloat(mk, "width", 0) * mDisplayWidth / 100);
327338
if (w == 0 && mk.containsKey("click")) w = defaultWidth;
328339
w -= gap;
329340
if (column >= maxColumns || x + w > mDisplayWidth) {
@@ -337,7 +348,7 @@ public Keyboard(String name) {
337348
if (keyboardHeight > 0) {
338349
rowHeight = newHeight[row];
339350
} else {
340-
int heightK = ConfigGetter.getPixel(mk, "height", 0);
351+
int heightK = (int) DimensionsKt.sp2px(CollectionUtils.obtainFloat(mk, "height", 0));
341352
rowHeight = (heightK > 0) ? heightK : defaultHeight;
342353
}
343354
}
@@ -347,47 +358,85 @@ public Keyboard(String name) {
347358
}
348359

349360
final int defaultKeyTextOffsetX =
350-
ConfigGetter.getPixel(
351-
keyboardConfig, "key_text_offset_x", theme.style.getFloat("key_text_offset_x"));
361+
(int)
362+
DimensionsKt.sp2px(
363+
CollectionUtils.obtainFloat(
364+
keyboardConfig,
365+
"key_text_offset_x",
366+
theme.style.getFloat("key_text_offset_x")));
352367
final int defaultKeyTextOffsetY =
353-
ConfigGetter.getPixel(
354-
keyboardConfig, "key_text_offset_y", theme.style.getFloat("key_text_offset_y"));
368+
(int)
369+
DimensionsKt.sp2px(
370+
CollectionUtils.obtainFloat(
371+
keyboardConfig,
372+
"key_text_offset_y",
373+
theme.style.getFloat("key_text_offset_y")));
355374
final int defaultKeySymbolOffsetX =
356-
ConfigGetter.getPixel(
357-
keyboardConfig, "key_symbol_offset_x", theme.style.getFloat("key_symbol_offset_x"));
375+
(int)
376+
DimensionsKt.sp2px(
377+
CollectionUtils.obtainFloat(
378+
keyboardConfig,
379+
"key_symbol_offset_x",
380+
theme.style.getFloat("key_symbol_offset_x")));
358381
final int defaultKeySymbolOffsetY =
359-
ConfigGetter.getPixel(
360-
keyboardConfig, "key_symbol_offset_y", theme.style.getFloat("key_symbol_offset_y"));
382+
(int)
383+
DimensionsKt.sp2px(
384+
CollectionUtils.obtainFloat(
385+
keyboardConfig,
386+
"key_symbol_offset_y",
387+
theme.style.getFloat("key_symbol_offset_y")));
361388
final int defaultKeyHintOffsetX =
362-
ConfigGetter.getPixel(
363-
keyboardConfig, "key_hint_offset_x", theme.style.getFloat("key_hint_offset_x"));
389+
(int)
390+
DimensionsKt.sp2px(
391+
CollectionUtils.obtainFloat(
392+
keyboardConfig,
393+
"key_hint_offset_x",
394+
theme.style.getFloat("key_hint_offset_x")));
364395
final int defaultKeyHintOffsetY =
365-
ConfigGetter.getPixel(
366-
keyboardConfig, "key_hint_offset_y", theme.style.getFloat("key_hint_offset_y"));
396+
(int)
397+
DimensionsKt.sp2px(
398+
CollectionUtils.obtainFloat(
399+
keyboardConfig,
400+
"key_hint_offset_y",
401+
theme.style.getFloat("key_hint_offset_y")));
367402
final int defaultKeyPressOffsetX =
368-
ConfigGetter.getInt(
403+
CollectionUtils.obtainInt(
369404
keyboardConfig, "key_press_offset_x", theme.style.getInt("key_press_offset_x"));
370405
final int defaultKeyPressOffsetY =
371-
ConfigGetter.getInt(
406+
CollectionUtils.obtainInt(
372407
keyboardConfig, "key_press_offset_y", theme.style.getInt("key_press_offset_y"));
373408

374409
final Key key = new Key(this, mk);
375410
key.setKey_text_offset_x(
376-
ConfigGetter.getPixel(mk, "key_text_offset_x", defaultKeyTextOffsetX));
411+
(int)
412+
DimensionsKt.sp2px(
413+
CollectionUtils.obtainFloat(mk, "key_text_offset_x", defaultKeyTextOffsetX)));
377414
key.setKey_text_offset_y(
378-
ConfigGetter.getPixel(mk, "key_text_offset_y", defaultKeyTextOffsetY));
415+
(int)
416+
DimensionsKt.sp2px(
417+
CollectionUtils.obtainFloat(mk, "key_text_offset_y", defaultKeyTextOffsetY)));
379418
key.setKey_symbol_offset_x(
380-
ConfigGetter.getPixel(mk, "key_symbol_offset_x", defaultKeySymbolOffsetX));
419+
(int)
420+
DimensionsKt.sp2px(
421+
CollectionUtils.obtainFloat(
422+
mk, "key_symbol_offset_x", defaultKeySymbolOffsetX)));
381423
key.setKey_symbol_offset_y(
382-
ConfigGetter.getPixel(mk, "key_symbol_offset_y", defaultKeySymbolOffsetY));
424+
(int)
425+
DimensionsKt.sp2px(
426+
CollectionUtils.obtainFloat(
427+
mk, "key_symbol_offset_y", defaultKeySymbolOffsetY)));
383428
key.setKey_hint_offset_x(
384-
ConfigGetter.getPixel(mk, "key_hint_offset_x", defaultKeyHintOffsetX));
429+
(int)
430+
DimensionsKt.sp2px(
431+
CollectionUtils.obtainFloat(mk, "key_hint_offset_x", defaultKeyHintOffsetX)));
385432
key.setKey_hint_offset_y(
386-
ConfigGetter.getPixel(mk, "key_hint_offset_y", defaultKeyHintOffsetY));
433+
(int)
434+
DimensionsKt.sp2px(
435+
CollectionUtils.obtainFloat(mk, "key_hint_offset_y", defaultKeyHintOffsetY)));
387436
key.setKey_press_offset_x(
388-
ConfigGetter.getInt(mk, "key_press_offset_x", defaultKeyPressOffsetX));
437+
CollectionUtils.obtainInt(mk, "key_press_offset_x", defaultKeyPressOffsetX));
389438
key.setKey_press_offset_y(
390-
ConfigGetter.getInt(mk, "key_press_offset_y", defaultKeyPressOffsetY));
439+
CollectionUtils.obtainInt(mk, "key_press_offset_y", defaultKeyPressOffsetY));
391440

392441
key.setX(x);
393442
key.setY(y);

app/src/main/java/com/osfans/trime/ime/text/Composition.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import com.osfans.trime.ime.core.Trime;
4747
import com.osfans.trime.ime.keyboard.Event;
4848
import com.osfans.trime.util.CollectionUtils;
49-
import com.osfans.trime.util.ConfigGetter;
5049
import com.osfans.trime.util.DimensionsKt;
5150
import java.util.ArrayList;
5251
import java.util.List;
@@ -324,7 +323,7 @@ private void appendComposition(Map<String, Object> m) {
324323
ss.setSpan(new CompositionSpan(), start, end, span);
325324
ss.setSpan(new AbsoluteSizeSpan(text_size), start, end, span);
326325
if (m.containsKey("letter_spacing")) {
327-
final float size = ConfigGetter.getFloat(m, "letter_spacing", 0);
326+
final float size = CollectionUtils.obtainFloat(m, "letter_spacing", 0);
328327
if (size != 0f) ss.setSpan(new LetterSpacingSpan(size), start, end, span);
329328
}
330329
start = composition_pos[0] + r.getStart();

app/src/main/java/com/osfans/trime/util/ConfigGetter.kt

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)