Skip to content

Commit d359f55

Browse files
committed
refactor: remove deprecated dimension methods
1 parent de112cd commit d359f55

File tree

11 files changed

+96
-140
lines changed

11 files changed

+96
-140
lines changed

app/src/main/java/com/osfans/trime/data/theme/ColorManager.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.osfans.trime.data.theme
22

3+
import android.content.Context
34
import android.content.res.Configuration
45
import android.graphics.drawable.Drawable
56
import android.graphics.drawable.GradientDrawable
67
import androidx.annotation.ColorInt
7-
import androidx.core.math.MathUtils
88
import com.osfans.trime.data.AppPrefs
99
import com.osfans.trime.data.DataManager
1010
import com.osfans.trime.data.sound.SoundEffectManager
1111
import com.osfans.trime.util.ColorUtils
1212
import com.osfans.trime.util.WeakHashSet
1313
import com.osfans.trime.util.bitmapDrawable
14-
import com.osfans.trime.util.dp2px
1514
import com.osfans.trime.util.isNightMode
15+
import splitties.dimensions.dp
1616
import timber.log.Timber
1717
import java.io.File
1818
import java.lang.IllegalArgumentException
@@ -331,16 +331,17 @@ object ColorManager {
331331

332332
// 返回图片或背景的drawable,支持null参数。 Config 2.0
333333
fun getDrawable(
334-
key: String?,
335-
borderKey: String?,
336-
borderColorKey: String?,
337-
roundCornerKey: String,
338-
alphaKey: String?,
334+
context: Context,
335+
key: String,
336+
borderKey: String = "",
337+
borderColorKey: String = "",
338+
roundCornerKey: String = "",
339+
alphaKey: String = "",
339340
): Drawable? {
340341
val value = getColorValue(key)
341342
if (value is Drawable) {
342-
if (!alphaKey.isNullOrEmpty() && theme.style.getObject(alphaKey) != null) {
343-
value.alpha = MathUtils.clamp(theme.style.getInt(alphaKey), 0, 255)
343+
if (alphaKey.isNotEmpty() && theme.style.getObject(alphaKey) != null) {
344+
value.alpha = theme.style.getInt(alphaKey).coerceIn(0, 255)
344345
}
345346
return value
346347
}
@@ -350,15 +351,15 @@ object ColorManager {
350351
if (roundCornerKey.isNotEmpty()) {
351352
gradient.cornerRadius = theme.style.getFloat(roundCornerKey)
352353
}
353-
if (!borderColorKey.isNullOrEmpty() && !borderKey.isNullOrEmpty()) {
354-
val border = dp2px(theme.style.getFloat(borderKey))
354+
if (borderColorKey.isNotEmpty() && borderKey.isNotEmpty()) {
355+
val border = context.dp(theme.style.getFloat(borderKey))
355356
val stroke = getColor(borderColorKey)
356357
if (stroke != null && border > 0) {
357358
gradient.setStroke(border.toInt(), stroke)
358359
}
359360
}
360-
if (!alphaKey.isNullOrEmpty() && theme.style.getObject(alphaKey) != null) {
361-
gradient.alpha = MathUtils.clamp(theme.style.getInt(alphaKey), 0, 255)
361+
if (alphaKey.isNotEmpty() && theme.style.getObject(alphaKey) != null) {
362+
gradient.alpha = theme.style.getInt(alphaKey).coerceIn(0, 255)
362363
}
363364
return gradient
364365
}

app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ class QuickBar(context: Context, service: TrimeInputMethodService) {
5858
}
5959
background =
6060
ColorManager.getDrawable(
61+
context,
6162
"candidate_background",
6263
"candidate_border",
6364
"candidate_border_color",
6465
"candidate_border_round",
65-
null,
6666
)
6767
add(oldCandidateBar.root, lParams(matchParent, matchParent))
6868
add(oldTabBar.root, lParams(matchParent, matchParent))

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import com.osfans.trime.ime.enums.KeyEventType
3232
import com.osfans.trime.util.CollectionUtils.obtainBoolean
3333
import com.osfans.trime.util.CollectionUtils.obtainFloat
3434
import com.osfans.trime.util.CollectionUtils.obtainString
35-
import com.osfans.trime.util.sp2px
35+
import com.osfans.trime.util.appContext
36+
import com.osfans.trime.util.sp
3637
import timber.log.Timber
3738
import java.text.MessageFormat
3839

@@ -145,8 +146,8 @@ class Key(private val mKeyboard: Keyboard) {
145146
}
146147
}
147148
mKeyboard.setModiferKey(this.code, this)
148-
key_text_size = sp2px(obtainFloat(mk, "key_text_size", 0f)).toInt()
149-
symbol_text_size = sp2px(obtainFloat(mk, "symbol_text_size", 0f)).toInt()
149+
key_text_size = appContext.sp(obtainFloat(mk, "key_text_size", 0f)).toInt()
150+
symbol_text_size = appContext.sp(obtainFloat(mk, "symbol_text_size", 0f)).toInt()
150151
round_corner = obtainFloat(mk, "round_corner", 0f)
151152
}
152153

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ import com.osfans.trime.util.CollectionUtils.obtainFloat
2929
import com.osfans.trime.util.CollectionUtils.obtainInt
3030
import com.osfans.trime.util.CollectionUtils.obtainString
3131
import com.osfans.trime.util.appContext
32-
import com.osfans.trime.util.dp2px
33-
import com.osfans.trime.util.sp2px
32+
import com.osfans.trime.util.sp
3433
import splitties.dimensions.dp
3534
import timber.log.Timber
3635
import kotlin.math.abs
@@ -189,7 +188,7 @@ class Keyboard() {
189188

190189
// 按键高度取值顺序: keys > keyboard/height > style/key_height
191190
// 考虑到key设置height_land需要对皮肤做大量修改,而当部分key设置height而部分没有设时会造成按键高度异常,故取消普通按键的height_land参数
192-
val height = sp2px(obtainFloat(keyboardConfig, "height", 0f)).toInt()
191+
val height = appContext.sp(obtainFloat(keyboardConfig, "height", 0f)).toInt()
193192
val defaultHeight = if (height > 0) height else keyHeight
194193
var rowHeight = defaultHeight
195194
// 定义 新的键盘尺寸计算方式, 避免尺寸计算不恰当,导致切换键盘时键盘高度发生变化,UI闪烁的问题。同时可以快速调整整个键盘的尺寸
@@ -200,15 +199,15 @@ class Keyboard() {
200199
val autoHeightIndex = obtainInt(keyboardConfig, "auto_height_index", -1)
201200
val lm = keyboardConfig!!["keys"] as List<Map<String, Any>>
202201
horizontalGap =
203-
sp2px(
202+
appContext.sp(
204203
obtainFloat(
205204
keyboardConfig,
206205
"horizontal_gap",
207206
theme.style.getFloat("horizontal_gap"),
208207
),
209208
).toInt()
210209
verticalGap =
211-
sp2px(
210+
appContext.sp(
212211
obtainFloat(
213212
keyboardConfig,
214213
"vertical_gap",
@@ -292,7 +291,7 @@ class Keyboard() {
292291
if (keyboardHeight > 0) {
293292
height1[row]
294293
} else {
295-
val heightK = sp2px(obtainFloat(mk, "height", 0f)).toInt()
294+
val heightK = appContext.sp(obtainFloat(mk, "height", 0f)).toInt()
296295
if (heightK > 0) heightK else defaultHeight
297296
}
298297
}
@@ -301,47 +300,47 @@ class Keyboard() {
301300
continue // 縮進
302301
}
303302
val defaultKeyTextOffsetX =
304-
sp2px(
303+
appContext.sp(
305304
obtainFloat(
306305
keyboardConfig,
307306
"key_text_offset_x",
308307
theme.style.getFloat("key_text_offset_x"),
309308
),
310309
).toInt()
311310
val defaultKeyTextOffsetY =
312-
sp2px(
311+
appContext.sp(
313312
obtainFloat(
314313
keyboardConfig,
315314
"key_text_offset_y",
316315
theme.style.getFloat("key_text_offset_y"),
317316
),
318317
).toInt()
319318
val defaultKeySymbolOffsetX =
320-
sp2px(
319+
appContext.sp(
321320
obtainFloat(
322321
keyboardConfig,
323322
"key_symbol_offset_x",
324323
theme.style.getFloat("key_symbol_offset_x"),
325324
),
326325
).toInt()
327326
val defaultKeySymbolOffsetY =
328-
sp2px(
327+
appContext.sp(
329328
obtainFloat(
330329
keyboardConfig,
331330
"key_symbol_offset_y",
332331
theme.style.getFloat("key_symbol_offset_y"),
333332
),
334333
).toInt()
335334
val defaultKeyHintOffsetX =
336-
sp2px(
335+
appContext.sp(
337336
obtainFloat(
338337
keyboardConfig,
339338
"key_hint_offset_x",
340339
theme.style.getFloat("key_hint_offset_x"),
341340
),
342341
).toInt()
343342
val defaultKeyHintOffsetY =
344-
sp2px(
343+
appContext.sp(
345344
obtainFloat(
346345
keyboardConfig,
347346
"key_hint_offset_y",
@@ -362,35 +361,35 @@ class Keyboard() {
362361
)
363362
val key = Key(this, mk)
364363
key.key_text_offset_x =
365-
sp2px(
364+
appContext.sp(
366365
obtainFloat(mk, "key_text_offset_x", defaultKeyTextOffsetX.toFloat()),
367366
).toInt()
368367
key.key_text_offset_y =
369-
sp2px(
368+
appContext.sp(
370369
obtainFloat(mk, "key_text_offset_y", defaultKeyTextOffsetY.toFloat()),
371370
).toInt()
372371
key.key_symbol_offset_x =
373-
sp2px(
372+
appContext.sp(
374373
obtainFloat(
375374
mk,
376375
"key_symbol_offset_x",
377376
defaultKeySymbolOffsetX.toFloat(),
378377
),
379378
).toInt()
380379
key.key_symbol_offset_y =
381-
sp2px(
380+
appContext.sp(
382381
obtainFloat(
383382
mk,
384383
"key_symbol_offset_y",
385384
defaultKeySymbolOffsetY.toFloat(),
386385
),
387386
).toInt()
388387
key.key_hint_offset_x =
389-
sp2px(
388+
appContext.sp(
390389
obtainFloat(mk, "key_hint_offset_x", defaultKeyHintOffsetX.toFloat()),
391390
).toInt()
392391
key.key_hint_offset_y =
393-
sp2px(
392+
appContext.sp(
394393
obtainFloat(mk, "key_hint_offset_y", defaultKeyHintOffsetY.toFloat()),
395394
).toInt()
396395
key.key_press_offset_x = obtainInt(mk, "key_press_offset_x", defaultKeyPressOffsetX)
@@ -424,19 +423,21 @@ class Keyboard() {
424423
}
425424

426425
private fun getKeyboardHeightFromTheme(theme: Theme): Int {
427-
var keyboardHeight = dp2px(theme.style.getFloat("keyboard_height")).toInt()
428-
if (ScreenUtils.isLandscape()) {
429-
val keyBoardHeightLand = dp2px(theme.style.getFloat("keyboard_height_land")).toInt()
430-
if (keyBoardHeightLand > 0) keyboardHeight = keyBoardHeightLand
431-
}
432-
return keyboardHeight
426+
val keyboardHeight = theme.style.getFloat("keyboard_height")
427+
val keyboardHeightLand = theme.style.getFloat("keyboard_height_land")
428+
val value =
429+
when (appContext.resources.configuration.orientation) {
430+
Configuration.ORIENTATION_LANDSCAPE -> keyboardHeightLand.takeIf { it > 0 } ?: keyboardHeight
431+
else -> keyboardHeight
432+
}
433+
return appContext.dp(value).toInt()
433434
}
434435

435436
private fun getKeyboardHeightFromKeyboardConfig(keyboardConfig: Map<String, Any?>?): Int {
436-
var mkeyboardHeight = sp2px(obtainFloat(keyboardConfig, "keyboard_height", 0f)).toInt()
437+
var mkeyboardHeight = appContext.sp(obtainFloat(keyboardConfig, "keyboard_height", 0f)).toInt()
437438
if (ScreenUtils.isLandscape()) {
438439
val mkeyBoardHeightLand =
439-
sp2px(
440+
appContext.sp(
440441
obtainFloat(keyboardConfig, "keyboard_height_land", 0f),
441442
).toInt()
442443
if (mkeyBoardHeightLand > 0) mkeyboardHeight = mkeyBoardHeightLand
@@ -653,10 +654,10 @@ class Keyboard() {
653654
// Height of the screen
654655
// final int mDisplayHeight = dm.heightPixels;
655656
// Log.v(TAG, "keyboard's display metrics:" + dm);
656-
horizontalGap = dp2px(theme.style.getFloat("horizontal_gap")).toInt()
657-
verticalGap = dp2px(theme.style.getFloat("vertical_gap")).toInt()
657+
horizontalGap = appContext.dp(theme.style.getFloat("horizontal_gap")).toInt()
658+
verticalGap = appContext.dp(theme.style.getFloat("vertical_gap")).toInt()
658659
keyWidth = (mDisplayWidth * theme.style.getFloat("key_width") / 100).toInt()
659-
keyHeight = dp2px(theme.style.getFloat("key_height")).toInt()
660+
keyHeight = appContext.dp(theme.style.getFloat("key_height")).toInt()
660661
mProximityThreshold = (keyWidth * SEARCH_DISTANCE).toInt()
661662
mProximityThreshold *= mProximityThreshold // Square it for comparison
662663
roundCorner = theme.style.getFloat("round_corner")

app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardSizeCalculator.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.osfans.trime.ime.keyboard
22

33
import com.osfans.trime.util.CollectionUtils.obtainFloat
4-
import com.osfans.trime.util.sp2px
4+
import com.osfans.trime.util.appContext
5+
import com.osfans.trime.util.sp
56
import kotlin.math.abs
67

78
class KeyboardSizeCalculator(
@@ -58,7 +59,7 @@ class KeyboardSizeCalculator(
5859
}
5960

6061
if (column == 0) {
61-
val heightK = sp2px(obtainFloat(mk, "height", 0f)).toInt()
62+
val heightK = appContext.sp(obtainFloat(mk, "height", 0f)).toInt()
6263
rowHeight = if (heightK > 0) heightK else keyHeight
6364
}
6465
totalKeyWidth += keyWidthWeight

app/src/main/java/com/osfans/trime/ime/symbol/CandidateAdapter.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ class CandidateAdapter(theme: Theme) : RecyclerView.Adapter<CandidateAdapter.Vie
5252
private val mCommentTextSize = theme.style.getFloat("comment_text_size").coerceAtLeast(1f)
5353
private val mCommentFont = FontManager.getTypeface("comment_font")
5454
private val mCommentTextColor = ColorManager.getColor("comment_text_color")
55-
private val mBackgroud =
56-
ColorManager.getDrawable(
57-
"key_back_color",
58-
"key_border",
59-
"key_border_color",
60-
"round_corner",
61-
null,
62-
)
6355

6456
override fun onCreateViewHolder(
6557
parent: ViewGroup,
6658
viewType: Int,
6759
): ViewHolder {
6860
val binding = LiquidEntryViewBinding.inflate(LayoutInflater.from(parent.context))
61+
binding.root.background =
62+
ColorManager.getDrawable(
63+
parent.context,
64+
"key_back_color",
65+
"key_border",
66+
"key_border_color",
67+
"round_corner",
68+
)
6969
binding.candidate.apply {
7070
textSize = mCandidateTextSize
7171
typeface = mCandidateFont
@@ -124,10 +124,6 @@ class CandidateAdapter(theme: Theme) : RecyclerView.Adapter<CandidateAdapter.Vie
124124
holder.candidate.text = text
125125
holder.comment.text = comment
126126

127-
// 点击前后必须使用相同类型的背景,或者全部为背景图,或者都为背景色
128-
// 如果直接使用background,会造成滚动时部分内容的背景填充错误的问题
129-
holder.itemView.background = mBackgroud
130-
131127
// 如果设置了回调,则设置点击事件
132128
holder.itemView.setOnClickListener { listener?.invoke(position) }
133129

app/src/main/java/com/osfans/trime/ime/symbol/FlexibleAdapter.kt

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,26 @@ abstract class FlexibleAdapter(theme: Theme) : RecyclerView.Adapter<FlexibleAdap
5757
private val mKeyLongTextSize = theme.style.getFloat("key_long_text_size")
5858
private val mLabelTextSize = theme.style.getFloat("label_text_size")
5959

60-
// 这里不用 get() 会导致快速滑动时背景填充错误
61-
private val mBackground
62-
get() =
60+
override fun onCreateViewHolder(
61+
parent: ViewGroup,
62+
viewType: Int,
63+
): ViewHolder {
64+
val binding = SimpleKeyItemBinding.inflate(LayoutInflater.from(parent.context))
65+
binding.root.background =
6366
ColorManager.getDrawable(
67+
parent.context,
6468
"long_text_back_color",
6569
"key_border",
6670
"key_long_text_border",
6771
"round_corner",
68-
null,
6972
)
70-
71-
override fun onCreateViewHolder(
72-
parent: ViewGroup,
73-
viewType: Int,
74-
): ViewHolder {
75-
val binding = SimpleKeyItemBinding.inflate(LayoutInflater.from(parent.context))
76-
val holder = ViewHolder(binding)
77-
holder.simpleKeyText.apply {
73+
binding.simpleKey.apply {
7874
typeface = mTypeface
7975
(mLongTextColor ?: mKeyTextColor)?.let { setTextColor(it) }
8076
(mKeyLongTextSize.takeIf { it > 0f } ?: mLabelTextSize.takeIf { it > 0f })
8177
?.let { textSize = it }
8278
}
83-
return holder
79+
return ViewHolder(binding)
8480
}
8581

8682
inner class ViewHolder(binding: SimpleKeyItemBinding) : RecyclerView.ViewHolder(binding.root) {
@@ -96,7 +92,6 @@ abstract class FlexibleAdapter(theme: Theme) : RecyclerView.Adapter<FlexibleAdap
9692
val bean = mBeans[position]
9793
simpleKeyText.text = bean.text
9894
simpleKeyPin.visibility = if (bean.pinned) View.VISIBLE else View.INVISIBLE
99-
itemView.background = mBackground
10095
itemView.setOnClickListener {
10196
onPaste(bean)
10297
}

0 commit comments

Comments
 (0)