From 7df876a21ab0e5de309dadbb987ff21f1d5f1074 Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Tue, 30 Apr 2024 03:40:12 +0800 Subject: [PATCH] feat: excerpt the text of clipboard, collection or draft entry ... ... to reduce the render time of very long text --- .../trime/ime/symbol/FlexibleAdapter.kt | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/osfans/trime/ime/symbol/FlexibleAdapter.kt b/app/src/main/java/com/osfans/trime/ime/symbol/FlexibleAdapter.kt index 123a087820..c32e28caea 100644 --- a/app/src/main/java/com/osfans/trime/ime/symbol/FlexibleAdapter.kt +++ b/app/src/main/java/com/osfans/trime/ime/symbol/FlexibleAdapter.kt @@ -20,6 +20,7 @@ import com.osfans.trime.data.theme.Theme import com.osfans.trime.databinding.SimpleKeyItemBinding import splitties.resources.drawable import splitties.resources.styledColor +import kotlin.math.min abstract class FlexibleAdapter(private val theme: Theme) : RecyclerView.Adapter() { private val mBeans = mutableListOf() @@ -53,6 +54,30 @@ abstract class FlexibleAdapter(private val theme: Theme) : RecyclerView.Adapter< } } + private fun excerptText( + str: String, + lines: Int = 4, + chars: Int = 128, + ): String = + buildString { + val length = str.length + var lineBreak = -1 + for (i in 1..lines) { + val start = lineBreak + 1 // skip previous '\n' + val excerptEnd = min(start + chars, length) + lineBreak = str.indexOf('\n', start) + if (lineBreak < 0) { + // no line breaks remaining, substring to end of text + append(str.substring(start, excerptEnd)) + break + } else { + val end = min(excerptEnd, lineBreak) + // append one line exactly + appendLine(str.substring(start, end)) + } + } + } + override fun getItemCount(): Int = mBeans.size private val mTypeface = FontManager.getTypeface("long_text_font") @@ -94,7 +119,7 @@ abstract class FlexibleAdapter(private val theme: Theme) : RecyclerView.Adapter< ) { with(viewHolder) { val bean = mBeans[position] - simpleKeyText.text = bean.text + simpleKeyText.text = bean.text?.let { excerptText(it) } simpleKeyPin.visibility = if (bean.pinned) View.VISIBLE else View.INVISIBLE itemView.setOnClickListener { onPaste(bean)