Skip to content

Commit bb11b6e

Browse files
committed
refactor: migrate to BetterAndroid new usage
1 parent a0c6f77 commit bb11b6e

File tree

7 files changed

+19
-16
lines changed

7 files changed

+19
-16
lines changed

pangutext-android/src/main/java/com/highcapable/pangutext/android/PanguText.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import android.text.style.CharacterStyle
3232
import android.widget.TextView
3333
import androidx.annotation.Px
3434
import com.highcapable.kavaref.extension.classOf
35+
import com.highcapable.pangutext.android.PanguText.PH
3536
import com.highcapable.pangutext.android.core.PanguMarginSpan
3637
import com.highcapable.pangutext.android.core.PanguPatterns
3738
import com.highcapable.pangutext.android.extension.injectPanguText
@@ -155,7 +156,7 @@ object PanguText {
155156

156157
// Find the [PanguMarginSpan.Placeholder] subscript in [builder] and use [PanguMarginSpan] to set it to [original].
157158
val builderSpans = builder.getSpans(0, builder.length, classOf<PanguMarginSpan.Placeholder>())
158-
val spannable = if (this !is Spannable) SpannableString(this) else this
159+
val spannable = this as? Spannable ?: SpannableString(this)
159160

160161
// Add new [PanguMarginSpan].
161162
builderSpans.forEach {

pangutext-android/src/main/java/com/highcapable/pangutext/android/core/PanguMarginSpan.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import kotlin.math.round
3939
* Pangu span with margin.
4040
* @param margin the margin size (px).
4141
*/
42-
internal class PanguMarginSpan(@param:Px val margin: Int) : ReplacementSpan() {
42+
internal class PanguMarginSpan(@field:Px val margin: Int) : ReplacementSpan() {
4343

4444
internal companion object {
4545

@@ -71,25 +71,25 @@ internal class PanguMarginSpan(@param:Px val margin: Int) : ReplacementSpan() {
7171

7272
override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
7373
if (text is Spanned) text.getSpans<Any>(start, end).forEach { span ->
74-
when {
75-
span is BackgroundColorSpan -> {
74+
when (span) {
75+
is BackgroundColorSpan -> {
7676
// Get background color.
7777
val color = span.backgroundColor
7878
val originalColor = paint.color
79-
79+
8080
// Save the current [paint] color.
8181
paint.color = color
82-
82+
8383
// Get the width of the text.
8484
val textWidth = paint.measureText(text, start, end)
85-
85+
8686
// Draw background rectangle.
8787
canvas.drawRect(x, top.toFloat(), x + textWidth + margin, bottom.toFloat(), paint)
88-
88+
8989
// Restore original color.
9090
paint.color = originalColor
9191
}
92-
span is CharacterStyle && paint is TextPaint -> span.updateDrawState(paint)
92+
is CharacterStyle if paint is TextPaint -> span.updateDrawState(paint)
9393
}
9494
}
9595

pangutext-android/src/main/java/com/highcapable/pangutext/android/core/PanguPatterns.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* This file is created by fankes on 2025/1/20.
2121
*/
22-
@file:Suppress("RegExpRedundantEscape", "RegExpSimplifiable")
22+
@file:Suppress("RegExpRedundantEscape", "RegExpSimplifiable", "CanConvertToMultiDollarString", "CanUnescapeDollarLiteral")
2323

2424
package com.highcapable.pangutext.android.core
2525

pangutext-android/src/main/java/com/highcapable/pangutext/android/core/PanguTextWatcher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import android.text.Editable
2525
import android.text.TextWatcher
2626
import android.widget.EditText
2727
import android.widget.TextView
28-
import com.highcapable.betterandroid.system.extension.tool.AndroidVersion
28+
import com.highcapable.betterandroid.system.extension.utils.AndroidVersion
2929
import com.highcapable.pangutext.android.PanguText
3030
import com.highcapable.pangutext.android.PanguTextConfig
3131
import com.highcapable.pangutext.android.core.TextViewDelegate.Companion.delegate

pangutext-android/src/main/java/com/highcapable/pangutext/android/extension/PanguText.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTe
9494
val self = this@injectRealTimePanguText
9595
if (self.hint != currentHint)
9696
self.setHintWithPangu(self.hint, config)
97+
98+
@Suppress("AssignedValueIsNeverRead")
9799
currentHint = self.hint
98100
}
99101

pangutext-android/src/main/java/com/highcapable/pangutext/android/extension/Replacement.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ internal fun CharSequence.replaceAndPreserveSpans(regex: Regex, replacement: Str
4444

4545
val matcher = regex.toPattern().matcher(this)
4646
val excludeMatchers = excludePatterns.map { it.toPattern().matcher(this) }
47-
val excludeIndexs = mutableSetOf<Pair<Int, Int>>()
47+
val excludeIndexes = mutableSetOf<Pair<Int, Int>>()
4848

4949
excludeMatchers.forEach {
50-
while (it.find()) excludeIndexs.add(it.start() to it.end())
50+
while (it.find()) excludeIndexes.add(it.start() to it.end())
5151
}
5252

5353
var offset = 0
@@ -59,7 +59,7 @@ internal fun CharSequence.replaceAndPreserveSpans(regex: Regex, replacement: Str
5959

6060
// Skip the replacement if the matched range is excluded.
6161
// The character range offset is adjusted by 1 to avoid the exclusion of the matched range.
62-
if (excludeIndexs.any { it.first <= start + 1 && it.second >= end - 1 }) continue
62+
if (excludeIndexes.any { it.first <= start + 1 && it.second >= end - 1 }) continue
6363

6464
// Perform the replacement.
6565
val replacementText = matcher.buildReplacementText(replacement)

pangutext-android/src/main/java/com/highcapable/pangutext/android/factory/PanguWidget.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import com.highcapable.pangutext.android.generated.PangutextAndroidProperties
4949
internal object PanguWidget {
5050

5151
/** The text regex split symbol. */
52-
private const val TEXT_REGEX_SPLITE_SYMBOL = "|@|"
52+
private const val TEXT_REGEX_SPLIT_SYMBOL = "|@|"
5353

5454
/**
5555
* Process the widget by the given name.
@@ -118,7 +118,7 @@ internal object PanguWidget {
118118
val cjkSpacingRatio = getFloatOrNull(R.styleable.PanguTextHelper_panguText_cjkSpacingRatio)
119119

120120
val excludePatterns = getStringOrNull(R.styleable.PanguTextHelper_panguText_excludePatterns)
121-
?.split(TEXT_REGEX_SPLITE_SYMBOL)?.mapNotNull { regex ->
121+
?.split(TEXT_REGEX_SPLIT_SYMBOL)?.mapNotNull { regex ->
122122
runCatching { regex.toRegex() }.onFailure { th ->
123123
Log.e(PangutextAndroidProperties.PROJECT_NAME, "Invalid exclude pattern of $instance: $regex", th)
124124
}.getOrNull()

0 commit comments

Comments
 (0)