Fix urltest color

This commit is contained in:
世界 2023-08-05 12:15:18 +08:00
parent dd9db2599b
commit 90da262add
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 37 additions and 11 deletions

View file

@ -2,9 +2,12 @@ package io.nekohasekai.sfa.ktx
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.util.TypedValue
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import com.google.android.material.color.MaterialColors
@ColorInt
@ -18,14 +21,27 @@ fun Context.getAttrColor(
}
@ColorInt
fun colorForURLTestDelay(urlTestDelay: Int): Int {
return if (urlTestDelay <= 0) {
Color.GRAY
} else if (urlTestDelay <= 800) {
Color.GREEN
} else if (urlTestDelay <= 1500) {
Color.YELLOW
} else {
Color.RED
fun colorForURLTestDelay(context: Context, urlTestDelay: Int): Int {
if (urlTestDelay <= 0) {
return Color.GRAY
}
val colorRes =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && context.resources.configuration.isNightModeActive) {
if (urlTestDelay <= 800) {
android.R.color.holo_green_dark
} else if (urlTestDelay <= 1500) {
android.R.color.holo_orange_dark
} else {
android.R.color.holo_red_dark
}
} else {
if (urlTestDelay <= 800) {
android.R.color.holo_green_light
} else if (urlTestDelay <= 1500) {
android.R.color.holo_orange_light
} else {
android.R.color.holo_red_light
}
}
return MaterialColors.harmonizeWithPrimary(context, ContextCompat.getColor(context, colorRes))
}

View file

@ -212,7 +212,12 @@ class GroupsFragment : Fragment(), CommandClientHandler {
items.forEach {
builder.append("")
builder.setSpan(
ForegroundColorSpan(colorForURLTestDelay(it.urlTestDelay)),
ForegroundColorSpan(
colorForURLTestDelay(
binding.root.context,
it.urlTestDelay
)
),
builder.length - 1,
builder.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
@ -288,7 +293,12 @@ class GroupsFragment : Fragment(), CommandClientHandler {
binding.itemStatus.isVisible = item.urlTestTime > 0
if (item.urlTestTime > 0) {
binding.itemStatus.text = "${item.urlTestDelay}ms"
binding.itemStatus.setTextColor(colorForURLTestDelay(item.urlTestDelay))
binding.itemStatus.setTextColor(
colorForURLTestDelay(
binding.root.context,
item.urlTestDelay
)
)
}
}
}