Skip to content

Commit

Permalink
feat: excerpt the text of clipboard, collection or draft entry ...
Browse files Browse the repository at this point in the history
... to reduce the render time of very long text
  • Loading branch information
WhiredPlanck committed Apr 30, 2024
1 parent 3c852a8 commit 7df876a
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/symbol/FlexibleAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<FlexibleAdapter.ViewHolder>() {
private val mBeans = mutableListOf<DatabaseBean>()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7df876a

Please sign in to comment.