refactor: migration to KavaRef

This commit is contained in:
2025-06-26 12:55:28 +08:00
parent abc82dd9d4
commit 260fab2c1f
7 changed files with 40 additions and 42 deletions

View File

@@ -13,7 +13,7 @@ repositories:
plugins:
com.android.application:
alias: android-application
version: 8.9.0
version: 8.9.3
com.android.library:
alias: android-library
version-ref: com.android.application
@@ -26,7 +26,7 @@ plugins:
auto-update: false
com.vanniktech.maven.publish:
alias: maven-publish
version: 0.31.0
version: 0.33.0
libraries:
com.highcapable.betterandroid:
@@ -36,17 +36,19 @@ libraries:
version: 1.0.6
system-extension:
version: 1.0.2
com.highcapable.yukireflection:
api:
version: 1.0.3
com.highcapable.kavaref:
kavaref-core:
version: 1.0.0
kavaref-extension:
version: 1.0.0
androidx.core:
core:
version: 1.15.0
version: 1.16.0
core-ktx:
version-ref: <this>::core
androidx.appcompat:
appcompat:
version: 1.7.0
version: 1.7.1
com.google.android.material:
material:
# Workaround for a bug in version 1.12.0

View File

@@ -38,7 +38,8 @@ android {
}
dependencies {
implementation(com.highcapable.yukireflection.api)
implementation(com.highcapable.kavaref.kavaref.core)
implementation(com.highcapable.kavaref.kavaref.extension)
implementation(com.highcapable.betterandroid.ui.extension)
implementation(com.highcapable.betterandroid.system.extension)
implementation(androidx.core.core.ktx)

View File

@@ -31,13 +31,13 @@ import android.text.Spanned
import android.text.style.CharacterStyle
import android.widget.TextView
import androidx.annotation.Px
import com.highcapable.kavaref.extension.classOf
import com.highcapable.pangutext.android.core.PanguMarginSpan
import com.highcapable.pangutext.android.core.PanguPatterns
import com.highcapable.pangutext.android.extension.injectPanguText
import com.highcapable.pangutext.android.extension.injectRealTimePanguText
import com.highcapable.pangutext.android.extension.setHintWithPangu
import com.highcapable.pangutext.android.extension.setTextWithPangu
import com.highcapable.yukireflection.factory.classOf
/**
* The library core of Pangu text processor.
@@ -52,7 +52,7 @@ object PanguText {
* with no actual meaning.
*/
private const val PH = '\u001C'
/**
* The global configuration of [PanguText].
*/

View File

@@ -26,10 +26,10 @@ import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView
import com.highcapable.betterandroid.system.extension.tool.SystemVersion
import com.highcapable.kavaref.KavaRef.Companion.resolve
import com.highcapable.pangutext.android.PanguText
import com.highcapable.pangutext.android.PanguTextConfig
import com.highcapable.pangutext.android.extension.injectRealTimePanguText
import com.highcapable.yukireflection.factory.current
/**
* A [TextWatcher] that automatically applies [PanguText] to the text content.
@@ -45,10 +45,10 @@ class PanguTextWatcher internal constructor(private val base: TextView, private
* @return [ArrayList]<[TextWatcher]>.
*/
private val textWatchers
get() = base.current(ignored = true).field {
get() = base.resolve().optional(silent = true).firstFieldOrNull {
name = "mListeners"
superClass()
}.cast<ArrayList<TextWatcher>>()
superclass()
}?.getQuietly<ArrayList<TextWatcher>>()
/**
* Whether to automatically re-measure the text width after processing.

View File

@@ -25,9 +25,8 @@ package com.highcapable.pangutext.android.extension
import android.text.SpannableStringBuilder
import android.util.Log
import com.highcapable.kavaref.KavaRef.Companion.resolve
import com.highcapable.pangutext.android.generated.PangutextAndroidProperties
import com.highcapable.yukireflection.factory.classOf
import com.highcapable.yukireflection.factory.field
import java.util.regex.Matcher
/**
@@ -101,10 +100,10 @@ private fun Matcher.buildReplacementText(replacement: String): String {
* @return [Int]
*/
private fun Matcher.getNamedGroupIndex(groupName: String): Int {
val namedGroups = classOf<Matcher>()
.field { name = "namedGroups" }
.ignored()
.get(this)
.cast<Map<String, Int>>()
val namedGroups = Matcher::class.resolve()
.optional(silent = true)
.firstFieldOrNull {
name = "namedGroups"
}?.of(this)?.getQuietly<Map<String, Int>>()
return namedGroups?.get(groupName) ?: -1
}

View File

@@ -29,9 +29,8 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import com.highcapable.betterandroid.ui.extension.view.layoutInflater
import com.highcapable.kavaref.KavaRef.Companion.resolve
import com.highcapable.pangutext.android.generated.PangutextAndroidProperties
import com.highcapable.yukireflection.factory.field
import com.highcapable.yukireflection.type.android.LayoutInflaterClass
/**
* Pangu text factory 2 for [LayoutInflater.Factory2].
@@ -110,11 +109,10 @@ class PanguTextFactory2 private constructor(private val base: LayoutInflater.Fac
}
val replacement = PanguTextFactory2(original)
if (original != null)
LayoutInflaterClass.field {
inflater.resolve().optional(silent = true).firstFieldOrNull {
name = "mFactory2"
}.ignored().onNoSuchField {
Log.e(PangutextAndroidProperties.PROJECT_NAME, "LayoutInflater.mFactory2 not found.", it)
}.get(inflater).set(replacement)
superclass()
}?.setQuietly(replacement) ?: Log.e(PangutextAndroidProperties.PROJECT_NAME, "LayoutInflater.mFactory2 not found.")
else inflater.factory2 = replacement
}
}

View File

@@ -31,6 +31,10 @@ import com.highcapable.betterandroid.ui.extension.component.base.getBooleanOrNul
import com.highcapable.betterandroid.ui.extension.component.base.getFloatOrNull
import com.highcapable.betterandroid.ui.extension.component.base.getStringOrNull
import com.highcapable.betterandroid.ui.extension.component.base.obtainStyledAttributes
import com.highcapable.kavaref.KavaRef.Companion.resolve
import com.highcapable.kavaref.extension.classOf
import com.highcapable.kavaref.extension.isNotSubclassOf
import com.highcapable.kavaref.extension.toClassOrNull
import com.highcapable.pangutext.android.PanguText
import com.highcapable.pangutext.android.PanguTextConfig
import com.highcapable.pangutext.android.R
@@ -38,12 +42,6 @@ import com.highcapable.pangutext.android.core.PanguTextView
import com.highcapable.pangutext.android.extension.injectPanguText
import com.highcapable.pangutext.android.extension.injectRealTimePanguText
import com.highcapable.pangutext.android.generated.PangutextAndroidProperties
import com.highcapable.yukireflection.factory.classOf
import com.highcapable.yukireflection.factory.constructor
import com.highcapable.yukireflection.factory.notExtends
import com.highcapable.yukireflection.factory.toClassOrNull
import com.highcapable.yukireflection.type.android.AttributeSetClass
import com.highcapable.yukireflection.type.android.ContextClass
/**
* A widgets processor that automatically applies [PanguText] to the text content.
@@ -67,19 +65,19 @@ internal object PanguWidget {
if (!it.contains(".")) "android.widget.$it" else it
}.toClassOrNull()?.let { viewClass ->
// Avoid creating unnecessary components for waste.
if (viewClass notExtends classOf<TextView>()) return null
val twoParams = viewClass.constructor {
param(ContextClass, AttributeSetClass)
}.ignored().get()
val onceParam = viewClass.constructor {
param(ContextClass)
}.ignored().get()
if (viewClass isNotSubclassOf classOf<TextView>()) return null
val twoParams = viewClass.resolve()
.optional(silent = true)
.firstConstructorOrNull { parameters(Context::class, AttributeSet::class) }
val onceParam = viewClass.resolve()
.optional(silent = true)
.firstConstructorOrNull { parameters(Context::class) }
// Catching when the attrs value initialization failed.
runCatching { twoParams.newInstance<View>(context, attrs) }.onFailure {
runCatching { twoParams?.create(context, attrs) }.onFailure {
Log.w(PangutextAndroidProperties.PROJECT_NAME, "Failed to create instance of $viewClass using (Context, AttributeSet).", it)
}.getOrNull()
// Try to initialize with the default constructor again, otherwise return null.
?: runCatching { onceParam.newInstance<View>(context) }.onFailure {
?: runCatching { onceParam?.create(context) }.onFailure {
Log.w(PangutextAndroidProperties.PROJECT_NAME, "Failed to create instance of $viewClass, this process will be ignored.", it)
}.getOrNull()
}