Skip to content

Commit 6ce6c91

Browse files
committed
Fix media removal and improve coroutine handling
1 parent 2ff653a commit 6ce6c91

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,24 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
20172017
lineBlockFormatter.insertMediaSpan(shouldAddMediaInline, span)
20182018
}
20192019

2020+
/**
2021+
* Use this method to remove a media span
2022+
*/
2023+
fun removeMediaSpan(span: AztecMediaSpan) {
2024+
history.beforeTextChanged(this@AztecText)
2025+
disableTextChangedListener()
2026+
val startIndex = editableText.getSpanStart(span)
2027+
val endIndex = editableText.getSpanEnd(span)
2028+
editableText.getSpans(startIndex, endIndex, AztecMediaClickableSpan::class.java).forEach {
2029+
editableText.removeSpan(it)
2030+
}
2031+
editableText.removeSpan(span)
2032+
editableText.delete(startIndex, endIndex)
2033+
onMediaDeletedListener?.onMediaDeleted(span.attributes)
2034+
enableTextChangedListener()
2035+
contentChangeWatcher.notifyContentChanged()
2036+
}
2037+
20202038
fun insertVideo(drawable: Drawable?, attributes: Attributes) {
20212039
lineBlockFormatter.insertVideo(shouldAddMediaInline, drawable, attributes, onVideoTappedListener, onMediaDeletedListener)
20222040
}

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import android.view.View
1212
import android.view.ViewTreeObserver
1313
import android.widget.FrameLayout
1414
import androidx.core.content.ContextCompat
15+
import kotlinx.coroutines.CoroutineDispatcher
1516
import kotlinx.coroutines.CoroutineScope
17+
import kotlinx.coroutines.Job
1618
import kotlinx.coroutines.launch
1719
import kotlinx.coroutines.runBlocking
1820
import org.wordpress.aztec.AztecAttributes
@@ -24,6 +26,7 @@ import org.wordpress.aztec.plugins.html2visual.IHtmlTagHandler
2426
import org.wordpress.aztec.spans.AztecMediaClickableSpan
2527
import org.xml.sax.Attributes
2628
import java.util.UUID
29+
import kotlin.coroutines.CoroutineContext
2730
import kotlin.math.min
2831

2932
/**
@@ -36,15 +39,19 @@ import kotlin.math.min
3639
class PlaceholderManager(
3740
private val aztecText: AztecText,
3841
private val container: FrameLayout,
39-
private val coroutineScope: CoroutineScope,
42+
private val mainThreadDispatcher: CoroutineDispatcher,
4043
private val htmlTag: String = DEFAULT_HTML_TAG
4144
) : AztecContentChangeWatcher.AztecTextChangeObserver,
4245
IHtmlTagHandler,
4346
Html.MediaCallback,
4447
AztecText.OnMediaDeletedListener,
45-
AztecText.OnVisibilityChangeListener {
48+
AztecText.OnVisibilityChangeListener,
49+
CoroutineScope {
4650
private val adapters = mutableMapOf<String, PlaceholderAdapter>()
4751
private val positionToId = mutableSetOf<Placeholder>()
52+
private val job = Job()
53+
override val coroutineContext: CoroutineContext
54+
get() = mainThreadDispatcher + job
4855

4956
init {
5057
aztecText.setOnVisibilityChangeListener(this)
@@ -56,6 +63,7 @@ class PlaceholderManager(
5663
aztecText.contentChangeWatcher.unregisterObserver(this)
5764
adapters.values.forEach { it.onDestroy() }
5865
adapters.clear()
66+
job.cancel()
5967
}
6068

6169
/**
@@ -89,16 +97,8 @@ class PlaceholderManager(
8997
aztecText.editableText.getSpans(0, aztecText.length(), AztecPlaceholderSpan::class.java).filter {
9098
predicate(it.attributes)
9199
}.forEach { placeholderSpan ->
92-
val startIndex = aztecText.editableText.getSpanStart(placeholderSpan)
93-
val endIndex = aztecText.editableText.getSpanEnd(placeholderSpan)
94-
aztecText.editableText.getSpans(startIndex, endIndex, AztecMediaClickableSpan::class.java).forEach {
95-
aztecText.editableText.removeSpan(it)
96-
}
97-
aztecText.editableText.removeSpan(placeholderSpan)
98-
aztecText.editableText.delete(startIndex, endIndex)
99-
onMediaDeleted(placeholderSpan.attributes)
100+
aztecText.removeMediaSpan(placeholderSpan)
100101
}
101-
aztecText.refreshText(false)
102102
}
103103

104104
private suspend fun buildPlaceholderDrawable(adapter: PlaceholderAdapter, attrs: AztecAttributes): Drawable {
@@ -201,7 +201,7 @@ class PlaceholderManager(
201201
* Called when the aztec text content changes.
202202
*/
203203
override fun onContentChanged() {
204-
coroutineScope.launch {
204+
launch {
205205
updateAllBelowSelection(aztecText.selectionStart)
206206
}
207207
}
@@ -284,7 +284,7 @@ class PlaceholderManager(
284284
spans.forEach {
285285
val type = it.attributes.getValue(TYPE_ATTRIBUTE)
286286
val adapter = adapters[type] ?: return
287-
coroutineScope.launch {
287+
launch {
288288
updateDrawableBounds(adapter, it.attributes, it.drawable)
289289
aztecText.refreshText(false)
290290
insertInPosition(it.attributes, aztecText.editableText.getSpanStart(it))

0 commit comments

Comments
 (0)