Skip to content

Commit 4d9440c

Browse files
nopdanWhiredPlanck
authored andcommitted
refactor: get color/drawable
1 parent c998c88 commit 4d9440c

File tree

1 file changed

+69
-81
lines changed
  • app/src/main/java/com/osfans/trime/data/theme

1 file changed

+69
-81
lines changed

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

+69-81
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ class Theme private constructor(isDarkMode: Boolean) {
7878

7979
private val defaultThemeName = "trime"
8080

81-
fun isImageString(str: String?): Boolean {
82-
return str?.contains(Regex(".(png|jpg)")) == true
81+
fun isFileString(str: String?): Boolean {
82+
return str?.contains(Regex("""\.[a-z]{3,4}$""")) == true
8383
}
8484
}
8585

@@ -179,8 +179,10 @@ class Theme private constructor(isDarkMode: Boolean) {
179179
m: Map<String?, Any?>,
180180
key: String?,
181181
): Int? {
182-
if (m[key] == null) return null
183-
return ColorUtils.parseColor(m[key] as String?) ?: getColor(m[key] as String?)
182+
var value = theme.getColorValue(m[key] as String?)
183+
// 回退到配色
184+
if (value == null) value = theme.currentColors[key]
185+
return if (value is Int) value else null
184186
}
185187

186188
// 返回drawable。 Config 2.0
@@ -200,29 +202,15 @@ class Theme private constructor(isDarkMode: Boolean) {
200202
m: Map<String?, Any?>,
201203
key: String,
202204
): Drawable? {
203-
m[key] ?: return null
204-
val value = m[key] as String?
205-
val override = ColorUtils.parseColor(value)
206-
if (override != null) {
207-
return GradientDrawable().apply { setColor(override) }
208-
}
209-
210-
// value maybe a color label or a image path
211-
return if (isImageString(value)) {
212-
val path = theme.getImagePath(value!!)
213-
if (path.isNotEmpty()) {
214-
bitmapDrawable(path)
215-
} else {
216-
// fallback if image not found
217-
getDrawable(key)
218-
}
219-
} else if (theme.currentColors.containsKey(value)) {
220-
// use custom color label
221-
getDrawable(value!!)
222-
} else {
223-
// fallback color
224-
getDrawable(key)
205+
var value = theme.getColorValue(m[key] as String?)
206+
// 回退到配色
207+
if (value == null) value = theme.currentColors[key]
208+
if (value is Int) {
209+
return GradientDrawable().apply { setColor(value) }
210+
} else if (value is String) {
211+
return bitmapDrawable(value)
225212
}
213+
return null
226214
}
227215

228216
// 返回图片或背景的drawable,支持null参数。 Config 2.0
@@ -233,26 +221,19 @@ class Theme private constructor(isDarkMode: Boolean) {
233221
roundCornerKey: String,
234222
alphaKey: String?,
235223
): Drawable? {
236-
if (key == null) return null
237-
val o = theme.currentColors[key]
238-
var color = o
239-
if (o is String) {
240-
if (isImageString(o)) {
241-
val bitmap = bitmapDrawable(o)
242-
if (bitmap != null) {
243-
if (!alphaKey.isNullOrEmpty() && theme.style.getObject(alphaKey) != null) {
244-
bitmap.alpha = MathUtils.clamp(theme.style.getInt(alphaKey), 0, 255)
245-
}
246-
return bitmap
224+
val value = theme.getColorValue(key)
225+
if (value is String) {
226+
val bitmap = bitmapDrawable(value)
227+
if (bitmap != null) {
228+
if (!alphaKey.isNullOrEmpty() && theme.style.getObject(alphaKey) != null) {
229+
bitmap.alpha = MathUtils.clamp(theme.style.getInt(alphaKey), 0, 255)
247230
}
248-
} else {
249-
// it is html hex color string (e.g. #ff0000)
250-
color = ColorUtils.parseColor(o)
231+
return bitmap
251232
}
252233
}
253234

254-
if (color is Int) {
255-
val gradient = GradientDrawable().apply { setColor(color) }
235+
if (value is Int) {
236+
val gradient = GradientDrawable().apply { setColor(value) }
256237
if (roundCornerKey.isNotEmpty()) {
257238
gradient.cornerRadius = theme.style.getFloat(roundCornerKey)
258239
}
@@ -374,20 +355,6 @@ class Theme private constructor(isDarkMode: Boolean) {
374355
return keyboardPadding
375356
}
376357

377-
// 获取当前配色方案的key的value,或者从fallback获取值。
378-
private fun getColorValue(key: String?): Any? {
379-
val map = presetColorSchemes!![currentColorSchemeId] ?: return null
380-
var value: Any?
381-
var newKey = key
382-
val limit = fallbackColors!!.size * 2
383-
for (i in 0 until limit) {
384-
value = map[newKey]
385-
if (value != null || !fallbackColors!!.containsKey(newKey)) return value
386-
newKey = fallbackColors!![newKey]
387-
}
388-
return null
389-
}
390-
391358
var hasDarkLight = false
392359
private set
393360

@@ -442,54 +409,75 @@ class Theme private constructor(isDarkMode: Boolean) {
442409
currentColorSchemeId,
443410
darkMode,
444411
)
445-
cacheColorValues()
412+
refreshColorValues()
446413
}
447414

448-
private fun cacheColorValues() {
415+
private fun refreshColorValues() {
449416
currentColors.clear()
450417
val colorMap = presetColorSchemes!![currentColorSchemeId]
451418
if (colorMap == null) {
452419
Timber.w("Color scheme id not found: %s", currentColorSchemeId)
453420
return
454421
}
455422

456-
for ((key, value1) in colorMap) {
423+
for ((key, value) in colorMap) {
457424
if (key == "name" || key == "author" || key == "light_scheme" || key == "dark_scheme") continue
458-
val value = parseColorValue(value1)
459-
if (value != null) currentColors[key] = value
425+
currentColors[key] = value
460426
}
461-
for ((key) in fallbackColors!!) {
427+
for ((key, value) in fallbackColors!!) {
462428
if (!currentColors.containsKey(key)) {
463-
val value = parseColorValue(getColorValue(key))
464-
if (value != null) currentColors[key] = value
429+
currentColors[key] = value
430+
}
431+
}
432+
433+
for ((key, value) in currentColors) {
434+
val parsedValue = getColorValue(value)
435+
if (parsedValue != null) {
436+
currentColors[key] = parsedValue
437+
} else {
438+
Timber.w("Cannot parse color key: %s, value: %s", key, value)
465439
}
466440
}
467441
}
468442

443+
// 处理 value 值,转为颜色(Int)或图片path string 或者 fallback
444+
// 处理失败返回 null
445+
private fun getColorValue(value: Any?): Any? {
446+
val parsedValue = parseColorValue(value)
447+
if (parsedValue != null) {
448+
return parsedValue
449+
}
450+
var newKey = value
451+
var newValue: Any?
452+
val limit = currentColors.size
453+
for (i in 0 until limit) {
454+
newValue = currentColors[newKey]
455+
if (newValue == null) return null
456+
if (newValue is Int) return newValue
457+
if (isFileString(newValue as String)) return newValue
458+
459+
val parsedNewValue = parseColorValue(newValue)
460+
if (parsedNewValue != null) {
461+
return parsedNewValue
462+
}
463+
newKey = newValue
464+
}
465+
return null
466+
}
467+
469468
// 获取参数的真实value,Config 2.0
470469
// 如果是色彩返回int,如果是背景图返回path string,如果处理失败返回null
471470
private fun parseColorValue(value: Any?): Any? {
472-
value ?: return null
473471
if (value is String) {
474-
if (value.matches(".*[.\\\\/].*".toRegex())) {
475-
return getImagePath(value)
476-
} else {
477-
runCatching {
478-
return ColorUtils.parseColor(value)
479-
}.getOrElse {
480-
Timber.e("Unknown color value $value: ${it.message}")
472+
if (isFileString(value)) {
473+
// 获取图片的真实地址
474+
val fullPath = joinToFullImagePath(value)
475+
if (File(fullPath).exists()) {
476+
return fullPath
481477
}
482478
}
479+
return ColorUtils.parseColor(value)
483480
}
484481
return null
485482
}
486-
487-
private fun getImagePath(value: String): String {
488-
return if (value.matches(".*[.\\\\/].*".toRegex())) {
489-
val fullPath = joinToFullImagePath(value)
490-
if (File(fullPath).exists()) fullPath else ""
491-
} else {
492-
""
493-
}
494-
}
495483
}

0 commit comments

Comments
 (0)