refactor: migrate to BetterAndroid new usage

This commit is contained in:
2025-12-17 10:25:59 +08:00
parent a0c6f77b5b
commit bb11b6ecc3
7 changed files with 19 additions and 16 deletions

View File

@@ -32,6 +32,7 @@ import android.text.style.CharacterStyle
import android.widget.TextView
import androidx.annotation.Px
import com.highcapable.kavaref.extension.classOf
import com.highcapable.pangutext.android.PanguText.PH
import com.highcapable.pangutext.android.core.PanguMarginSpan
import com.highcapable.pangutext.android.core.PanguPatterns
import com.highcapable.pangutext.android.extension.injectPanguText
@@ -155,7 +156,7 @@ object PanguText {
// Find the [PanguMarginSpan.Placeholder] subscript in [builder] and use [PanguMarginSpan] to set it to [original].
val builderSpans = builder.getSpans(0, builder.length, classOf<PanguMarginSpan.Placeholder>())
val spannable = if (this !is Spannable) SpannableString(this) else this
val spannable = this as? Spannable ?: SpannableString(this)
// Add new [PanguMarginSpan].
builderSpans.forEach {

View File

@@ -39,7 +39,7 @@ import kotlin.math.round
* Pangu span with margin.
* @param margin the margin size (px).
*/
internal class PanguMarginSpan(@param:Px val margin: Int) : ReplacementSpan() {
internal class PanguMarginSpan(@field:Px val margin: Int) : ReplacementSpan() {
internal companion object {
@@ -71,8 +71,8 @@ internal class PanguMarginSpan(@param:Px val margin: Int) : ReplacementSpan() {
override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
if (text is Spanned) text.getSpans<Any>(start, end).forEach { span ->
when {
span is BackgroundColorSpan -> {
when (span) {
is BackgroundColorSpan -> {
// Get background color.
val color = span.backgroundColor
val originalColor = paint.color
@@ -89,7 +89,7 @@ internal class PanguMarginSpan(@param:Px val margin: Int) : ReplacementSpan() {
// Restore original color.
paint.color = originalColor
}
span is CharacterStyle && paint is TextPaint -> span.updateDrawState(paint)
is CharacterStyle if paint is TextPaint -> span.updateDrawState(paint)
}
}

View File

@@ -19,7 +19,7 @@
*
* This file is created by fankes on 2025/1/20.
*/
@file:Suppress("RegExpRedundantEscape", "RegExpSimplifiable")
@file:Suppress("RegExpRedundantEscape", "RegExpSimplifiable", "CanConvertToMultiDollarString", "CanUnescapeDollarLiteral")
package com.highcapable.pangutext.android.core

View File

@@ -25,7 +25,7 @@ import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView
import com.highcapable.betterandroid.system.extension.tool.AndroidVersion
import com.highcapable.betterandroid.system.extension.utils.AndroidVersion
import com.highcapable.pangutext.android.PanguText
import com.highcapable.pangutext.android.PanguTextConfig
import com.highcapable.pangutext.android.core.TextViewDelegate.Companion.delegate

View File

@@ -94,6 +94,8 @@ fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTe
val self = this@injectRealTimePanguText
if (self.hint != currentHint)
self.setHintWithPangu(self.hint, config)
@Suppress("AssignedValueIsNeverRead")
currentHint = self.hint
}

View File

@@ -44,10 +44,10 @@ internal fun CharSequence.replaceAndPreserveSpans(regex: Regex, replacement: Str
val matcher = regex.toPattern().matcher(this)
val excludeMatchers = excludePatterns.map { it.toPattern().matcher(this) }
val excludeIndexs = mutableSetOf<Pair<Int, Int>>()
val excludeIndexes = mutableSetOf<Pair<Int, Int>>()
excludeMatchers.forEach {
while (it.find()) excludeIndexs.add(it.start() to it.end())
while (it.find()) excludeIndexes.add(it.start() to it.end())
}
var offset = 0
@@ -59,7 +59,7 @@ internal fun CharSequence.replaceAndPreserveSpans(regex: Regex, replacement: Str
// Skip the replacement if the matched range is excluded.
// The character range offset is adjusted by 1 to avoid the exclusion of the matched range.
if (excludeIndexs.any { it.first <= start + 1 && it.second >= end - 1 }) continue
if (excludeIndexes.any { it.first <= start + 1 && it.second >= end - 1 }) continue
// Perform the replacement.
val replacementText = matcher.buildReplacementText(replacement)

View File

@@ -49,7 +49,7 @@ import com.highcapable.pangutext.android.generated.PangutextAndroidProperties
internal object PanguWidget {
/** The text regex split symbol. */
private const val TEXT_REGEX_SPLITE_SYMBOL = "|@|"
private const val TEXT_REGEX_SPLIT_SYMBOL = "|@|"
/**
* Process the widget by the given name.
@@ -118,7 +118,7 @@ internal object PanguWidget {
val cjkSpacingRatio = getFloatOrNull(R.styleable.PanguTextHelper_panguText_cjkSpacingRatio)
val excludePatterns = getStringOrNull(R.styleable.PanguTextHelper_panguText_excludePatterns)
?.split(TEXT_REGEX_SPLITE_SYMBOL)?.mapNotNull { regex ->
?.split(TEXT_REGEX_SPLIT_SYMBOL)?.mapNotNull { regex ->
runCatching { regex.toRegex() }.onFailure { th ->
Log.e(PangutextAndroidProperties.PROJECT_NAME, "Invalid exclude pattern of $instance: $regex", th)
}.getOrNull()