From d6c0d1eeb25dbdc8fb8442ed415a5e8e6f05e0bf Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Sun, 19 Jun 2022 23:00:06 +0800 Subject: [PATCH] refactor(components): new LogView A component that show logcat in different color. Ref: https://github.com/fcitx5-android/fcitx5-android/blob/e44c1c7/app/src/main/java/org/fcitx/fcitx5/android/ui/common/LogView.kt --- .../trime/settings/components/LogView.kt | 102 ++++++++++++++++++ app/src/main/res/values/colors.xml | 8 ++ 2 files changed, 110 insertions(+) create mode 100644 app/src/main/java/com/osfans/trime/settings/components/LogView.kt diff --git a/app/src/main/java/com/osfans/trime/settings/components/LogView.kt b/app/src/main/java/com/osfans/trime/settings/components/LogView.kt new file mode 100644 index 0000000000..e9cf8dc2f1 --- /dev/null +++ b/app/src/main/java/com/osfans/trime/settings/components/LogView.kt @@ -0,0 +1,102 @@ +/** + * Adapted from [fcitx5-android/Logview.kt](https://github.com/fcitx5-android/fcitx5-android/blob/e44c1c7/app/src/main/java/org/fcitx/fcitx5/android/ui/common/LogView.kt) + */ +package com.osfans.trime.settings.components + +import android.content.Context +import android.graphics.Typeface +import android.os.Build +import android.text.Spannable +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.util.AttributeSet +import android.view.ViewGroup +import android.view.textclassifier.TextClassifier +import android.widget.HorizontalScrollView +import android.widget.TextView +import androidx.core.view.setPadding +import androidx.core.widget.NestedScrollView +import androidx.lifecycle.findViewTreeLifecycleOwner +import androidx.lifecycle.lifecycleScope +import com.blankj.utilcode.util.ColorUtils +import com.blankj.utilcode.util.SizeUtils +import com.osfans.trime.R +import com.osfans.trime.util.Logcat +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach + +class LogView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null) : + NestedScrollView(context, attributeSet) { + + private var logcat: Logcat? = null + + private val textView = TextView(context, attributeSet).apply { + setPadding(SizeUtils.dp2px(4F)) + textSize = 12f + typeface = Typeface.MONOSPACE + setTextIsSelectable(true) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + setTextClassifier(object : TextClassifier {}) + } + } + + private val scrollView = HorizontalScrollView(context, attributeSet).apply { + addView( + textView, + LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT + ) + ) + } + + init { + addView( + scrollView, + LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT + ) + ) + } + + override fun onDetachedFromWindow() { + logcat?.shutdownLogFlow() + super.onDetachedFromWindow() + } + + fun setLogcat(logcat: Logcat) { + this.logcat = logcat + logcat.initLogFlow() + logcat.logFlow.onEach { + val color = ColorUtils.getColor( + when (it.first()) { + 'V' -> R.color.grey_700 + 'D' -> R.color.grey_700 + 'I' -> R.color.blue_500 + 'W' -> R.color.yellow_800 + 'E' -> R.color.red_400 + 'F' -> R.color.red_A700 + else -> R.color.colorPrimary + } + ) + val colored = SpannableString(it).apply { + setSpan( + ForegroundColorSpan(color), + 0, + it.length, + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE + ) + } + textView.append(colored) + textView.append("\n") + }.launchIn(findViewTreeLifecycleOwner()!!.lifecycleScope) + } + + val currentLog: CharSequence + get() = textView.text ?: "" + + fun clear() { + textView.text = "" + } +} diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 5cb302d8ee..3deb40d445 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,5 +1,13 @@ + #D50000 + #EF5350 + #FFEB3B + #F9A825 + #2196F3 + #BDBDBD + #616161 + #3F4144 #37393D