@@ -12,7 +12,9 @@ import android.view.View
1212import android.view.ViewTreeObserver
1313import android.widget.FrameLayout
1414import androidx.core.content.ContextCompat
15+ import kotlinx.coroutines.CoroutineDispatcher
1516import kotlinx.coroutines.CoroutineScope
17+ import kotlinx.coroutines.Job
1618import kotlinx.coroutines.launch
1719import kotlinx.coroutines.runBlocking
1820import org.wordpress.aztec.AztecAttributes
@@ -24,6 +26,7 @@ import org.wordpress.aztec.plugins.html2visual.IHtmlTagHandler
2426import org.wordpress.aztec.spans.AztecMediaClickableSpan
2527import org.xml.sax.Attributes
2628import java.util.UUID
29+ import kotlin.coroutines.CoroutineContext
2730import kotlin.math.min
2831
2932/* *
@@ -36,15 +39,19 @@ import kotlin.math.min
3639class 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