Skip to content

Commit 3a9bc15

Browse files
committed
refactor: only show once warning log for duplicate injection in PanguText
1 parent b85cd19 commit 3a9bc15

File tree

1 file changed

+10
-5
lines changed
  • pangutext-android/src/main/java/com/highcapable/pangutext/android/extension

1 file changed

+10
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,15 @@ fun TextView.injectPanguText(injectHint: Boolean = true, config: PanguTextConfig
7676
fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTextConfig = PanguText.globalConfig) {
7777
if (!config.isEnabled) return
7878
val observerKey = R.id.tag_inject_real_time_pangu_text
79-
if (getTag<Boolean>(observerKey) == true) return run {
80-
Log.w(PangutextAndroidProperties.PROJECT_NAME, "Duplicate injection of real-time PanguText ($this).")
81-
}
82-
setTag(observerKey, injectHint)
79+
var lastSetTimes = getTag<Int>(observerKey) ?: 0
80+
setTag(observerKey, ++lastSetTimes)
81+
// Only print warning log once after one time.
82+
if (lastSetTimes == 2) Log.w(
83+
PangutextAndroidProperties.PROJECT_NAME,
84+
"Duplicate injection of real-time PanguText ($this), subsequent operations will be ignored."
85+
)
86+
// It will no longer be executed if it exceeds one time.
87+
if (lastSetTimes > 1) return
8388
injectPanguText(injectHint, config)
8489
var currentHint = this.hint
8590
val textWatcher = PanguTextWatcher(base = this, config)
@@ -97,7 +102,7 @@ fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTe
97102
removeTextChangedListener(textWatcher)
98103
// Remove the global layout listener when the view is detached.
99104
if (injectHint) viewTreeObserver?.removeOnGlobalLayoutListener(listener)
100-
setTag(observerKey, false)
105+
setTag(observerKey, 0)
101106
}
102107
}
103108
}

0 commit comments

Comments
 (0)