From eb9f683762c9c7f545667df9b5369a28f235005c Mon Sep 17 00:00:00 2001 From: Gerardo Date: Mon, 27 May 2024 16:24:31 +0200 Subject: [PATCH] Add uninstall method to EndOfBufferMarkerAdder --- .../kotlin/org/wordpress/aztec/AztecText.kt | 7 ++++- .../aztec/watchers/EndOfBufferMarkerAdder.kt | 28 ++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index e23e2c3f7..f6b7bde98 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -310,6 +310,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown var observationQueue: ObservationQueue = ObservationQueue(this) var textWatcherEventBuilder: TextWatcherEvent.Builder = TextWatcherEvent.Builder() + var endOfBufferMarkerAdderWatcher: EndOfBufferMarkerAdder? = null private var accessibilityDelegate = AztecTextAccessibilityDelegate(this) @@ -640,6 +641,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown } } + fun removeEndOfBufferMarkerAdder() { + endOfBufferMarkerAdderWatcher?.uninstallEndOfBuffer(this) + } + private fun selectionHasExactlyOneMarker(start: Int, end: Int, type: Class): Boolean { val spanFound: Array = editableText.getSpans( start, @@ -891,7 +896,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown FullWidthImageElementWatcher.install(this) - EndOfBufferMarkerAdder.install(this) + endOfBufferMarkerAdderWatcher = EndOfBufferMarkerAdder.install(this) ZeroIndexContentWatcher.install(this) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt index 8e95eeb84..76e95da62 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt @@ -18,8 +18,7 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - // count < before to take into account auto-corrected words - deletedText = before > 0 && count < before + deletedText = before > 0 } override fun afterTextChanged(text: Editable) { @@ -32,9 +31,26 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { // by the way, the cursor will be adjusted "automatically" by RichTextEditText's onSelectionChanged to before the marker } + fun uninstallEndOfBuffer(aztecText: AztecText) { + uninstall(aztecText) + } + companion object { - fun install(editText: AztecText) { - editText.addTextChangedListener(EndOfBufferMarkerAdder(editText.text)) + private val watchers = mutableMapOf() + + fun install(editText: AztecText): EndOfBufferMarkerAdder { + var watcher = EndOfBufferMarkerAdder(editText.text) + editText.addTextChangedListener(watcher) + watchers[editText] = watcher + return watcher + } + + fun uninstall(editText: AztecText) { + val watcher = watchers[editText] + if (watcher != null) { + editText.removeTextChangedListener(watcher) + watchers.remove(editText) + } } fun ensureEndOfTextMarker(text: Editable, deletedText: Boolean = false): Editable { @@ -45,9 +61,7 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { if (text.isEmpty()) { if (text.getSpans(0, 0, IAztecBlockSpan::class.java).isNotEmpty()) { // need to add a end-of-text marker so a block element can render in the empty text. - if (!deletedText) { - text.append(Constants.END_OF_BUFFER_MARKER) - } + text.append(Constants.END_OF_BUFFER_MARKER) } return text } else if (text.length == 1 && text[0] == Constants.END_OF_BUFFER_MARKER && deletedText) {