Skip to content

Commit a27546b

Browse files
authored
Merge pull request #989 from wordpress-mobile/fix/media-removal-and-coroutines
Fix media removal and improve coroutine handling
2 parents 2ff653a + bef2d8e commit a27546b

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-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: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import android.view.ViewTreeObserver
1313
import android.widget.FrameLayout
1414
import androidx.core.content.ContextCompat
1515
import kotlinx.coroutines.CoroutineScope
16+
import kotlinx.coroutines.Dispatchers
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,18 @@ import kotlin.math.min
3639
class 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

Comments
 (0)