mirror of
https://github.com/BetterAndroid/PanguText.git
synced 2025-09-04 01:35:37 +08:00
refactor: only show once warning log for duplicate injection in PanguText
This commit is contained in:
@@ -76,10 +76,15 @@ fun TextView.injectPanguText(injectHint: Boolean = true, config: PanguTextConfig
|
|||||||
fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTextConfig = PanguText.globalConfig) {
|
fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTextConfig = PanguText.globalConfig) {
|
||||||
if (!config.isEnabled) return
|
if (!config.isEnabled) return
|
||||||
val observerKey = R.id.tag_inject_real_time_pangu_text
|
val observerKey = R.id.tag_inject_real_time_pangu_text
|
||||||
if (getTag<Boolean>(observerKey) == true) return run {
|
var lastSetTimes = getTag<Int>(observerKey) ?: 0
|
||||||
Log.w(PangutextAndroidProperties.PROJECT_NAME, "Duplicate injection of real-time PanguText ($this).")
|
setTag(observerKey, ++lastSetTimes)
|
||||||
}
|
// Only print warning log once after one time.
|
||||||
setTag(observerKey, injectHint)
|
if (lastSetTimes == 2) Log.w(
|
||||||
|
PangutextAndroidProperties.PROJECT_NAME,
|
||||||
|
"Duplicate injection of real-time PanguText ($this), subsequent operations will be ignored."
|
||||||
|
)
|
||||||
|
// It will no longer be executed if it exceeds one time.
|
||||||
|
if (lastSetTimes > 1) return
|
||||||
injectPanguText(injectHint, config)
|
injectPanguText(injectHint, config)
|
||||||
var currentHint = this.hint
|
var currentHint = this.hint
|
||||||
val textWatcher = PanguTextWatcher(base = this, config)
|
val textWatcher = PanguTextWatcher(base = this, config)
|
||||||
@@ -97,7 +102,7 @@ fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTe
|
|||||||
removeTextChangedListener(textWatcher)
|
removeTextChangedListener(textWatcher)
|
||||||
// Remove the global layout listener when the view is detached.
|
// Remove the global layout listener when the view is detached.
|
||||||
if (injectHint) viewTreeObserver?.removeOnGlobalLayoutListener(listener)
|
if (injectHint) viewTreeObserver?.removeOnGlobalLayoutListener(listener)
|
||||||
setTag(observerKey, false)
|
setTag(observerKey, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user