Skip to content

Commit

Permalink
perf: make parseColor no longer check the parameter is null
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan authored and WhiredPlanck committed May 9, 2024
1 parent 9373ea6 commit cc8f7db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
22 changes: 14 additions & 8 deletions app/src/main/java/com/osfans/trime/data/theme/ColorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,11 @@ object ColorManager {
m: Map<String, Any?>,
key: String?,
): Int? {
val value = getColorValue(m[key].toString())
return if (value is Int) value else null
m[key].let {
if (it == null) return null
val value = getColorValue(it.toString())
return if (value is Int) value else null
}
}

// 返回drawable。 Config 2.0
Expand All @@ -335,13 +338,16 @@ object ColorManager {
m: Map<String, Any?>,
key: String,
): Drawable? {
val value = getColorValue(m[key].toString())
if (value is Int) {
return GradientDrawable().apply { setColor(value) }
} else if (value is Drawable) {
return value
m[key].let {
if (it == null) return null
val value = getColorValue(it.toString())
if (value is Int) {
return GradientDrawable().apply { setColor(value) }
} else if (value is Drawable) {
return value
}
return null
}
return null
}

// 返回图片或背景的drawable,支持null参数。 Config 2.0
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/osfans/trime/util/ColorUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import timber.log.Timber

object ColorUtils {
@JvmStatic
fun parseColor(s: String?): Int? {
if (s == null) return null
fun parseColor(s: String): Int? {
val hex: String = if (s.startsWith("#")) s.replace("#", "0x") else s
return try {
val completed =
Expand Down

0 comments on commit cc8f7db

Please sign in to comment.