@@ -13,6 +13,8 @@ import android.view.ViewTreeObserver
1313import android.widget.FrameLayout
1414import androidx.core.content.ContextCompat
1515import kotlinx.coroutines.CoroutineScope
16+ import kotlinx.coroutines.Dispatchers
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,18 @@ import kotlin.math.min
3639class PlaceholderManager (
3740 private val aztecText : AztecText ,
3841 private val container : FrameLayout ,
39- private val coroutineScope : CoroutineScope ,
4042 private val htmlTag : String = DEFAULT_HTML_TAG
4143) : AztecContentChangeWatcher.AztecTextChangeObserver,
4244 IHtmlTagHandler ,
4345 Html .MediaCallback ,
4446 AztecText .OnMediaDeletedListener ,
45- AztecText .OnVisibilityChangeListener {
47+ AztecText .OnVisibilityChangeListener ,
48+ CoroutineScope {
4649 private val adapters = mutableMapOf<String , PlaceholderAdapter >()
4750 private val positionToId = mutableSetOf<Placeholder >()
51+ private val job = Job ()
52+ override val coroutineContext: CoroutineContext
53+ get() = Dispatchers .Main + job
4854
4955 init {
5056 aztecText.setOnVisibilityChangeListener(this )
@@ -56,6 +62,7 @@ class PlaceholderManager(
5662 aztecText.contentChangeWatcher.unregisterObserver(this )
5763 adapters.values.forEach { it.onDestroy() }
5864 adapters.clear()
65+ job.cancel()
5966 }
6067
6168 /* *
@@ -89,16 +96,8 @@ class PlaceholderManager(
8996 aztecText.editableText.getSpans(0 , aztecText.length(), AztecPlaceholderSpan ::class .java).filter {
9097 predicate(it.attributes)
9198 }.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)
99+ aztecText.removeMediaSpan(placeholderSpan)
100100 }
101- aztecText.refreshText(false )
102101 }
103102
104103 private suspend fun buildPlaceholderDrawable (adapter : PlaceholderAdapter , attrs : AztecAttributes ): Drawable {
@@ -201,7 +200,7 @@ class PlaceholderManager(
201200 * Called when the aztec text content changes.
202201 */
203202 override fun onContentChanged () {
204- coroutineScope. launch {
203+ launch {
205204 updateAllBelowSelection(aztecText.selectionStart)
206205 }
207206 }
@@ -284,7 +283,7 @@ class PlaceholderManager(
284283 spans.forEach {
285284 val type = it.attributes.getValue(TYPE_ATTRIBUTE )
286285 val adapter = adapters[type] ? : return
287- coroutineScope. launch {
286+ launch {
288287 updateDrawableBounds(adapter, it.attributes, it.drawable)
289288 aztecText.refreshText(false )
290289 insertInPosition(it.attributes, aztecText.editableText.getSpanStart(it))
0 commit comments