Skip to content

Commit 6946e5e

Browse files
committed
refactor: remove warning log in PanguText
1 parent ed2b089 commit 6946e5e

File tree

1 file changed

+6
-11
lines changed
  • pangutext-android/src/main/java/com/highcapable/pangutext/android/extension

1 file changed

+6
-11
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
package com.highcapable.pangutext.android.extension
2626

27-
import android.util.Log
2827
import android.view.ViewTreeObserver
2928
import android.widget.TextView
3029
import androidx.core.view.doOnAttach
@@ -34,7 +33,6 @@ import com.highcapable.pangutext.android.PanguText
3433
import com.highcapable.pangutext.android.PanguTextConfig
3534
import com.highcapable.pangutext.android.R
3635
import com.highcapable.pangutext.android.core.PanguTextWatcher
37-
import com.highcapable.pangutext.android.generated.PangutextAndroidProperties
3836

3937
/**
4038
* Create a new instance of [PanguTextConfig].
@@ -65,6 +63,8 @@ fun TextView.injectPanguText(injectHint: Boolean = true, config: PanguTextConfig
6563

6664
/**
6765
* Inject [PanguText] to the current text content in real time.
66+
*
67+
* - Note: Repeated inject will be ignored unless the current [TextView] is detached.
6868
* @see TextView.setTextWithPangu
6969
* @see TextView.setHintWithPangu
7070
* @see PanguText.format
@@ -76,15 +76,9 @@ 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-
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-
)
79+
val isRepeated = getTag<Boolean>(observerKey) == true
8680
// It will no longer be executed if it exceeds one time.
87-
if (lastSetTimes > 1) return
81+
if (isRepeated) return
8882
injectPanguText(injectHint, config)
8983
var currentHint = this.hint
9084
val textWatcher = PanguTextWatcher(base = this, config)
@@ -94,6 +88,7 @@ fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTe
9488
self.setHintWithPangu(self.hint, config)
9589
currentHint = self.hint
9690
}
91+
setTag(observerKey, true)
9792
doOnAttach {
9893
addTextChangedListener(textWatcher)
9994
// Add a global layout listener to monitor the hint text changes.
@@ -102,7 +97,7 @@ fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTe
10297
removeTextChangedListener(textWatcher)
10398
// Remove the global layout listener when the view is detached.
10499
if (injectHint) viewTreeObserver?.removeOnGlobalLayoutListener(listener)
105-
setTag(observerKey, 0)
100+
setTag(observerKey, false)
106101
}
107102
}
108103
}

0 commit comments

Comments
 (0)