@@ -12,6 +12,8 @@ import android.view.View
1212import android.view.ViewTreeObserver
1313import android.widget.FrameLayout
1414import androidx.core.content.ContextCompat
15+ import kotlinx.coroutines.CoroutineScope
16+ import kotlinx.coroutines.launch
1517import org.wordpress.aztec.AztecAttributes
1618import org.wordpress.aztec.AztecContentChangeWatcher
1719import org.wordpress.aztec.AztecText
@@ -33,6 +35,7 @@ import kotlin.math.min
3335class PlaceholderManager (
3436 private val aztecText : AztecText ,
3537 private val container : FrameLayout ,
38+ private val coroutineScope : CoroutineScope ,
3639 private val htmlTag : String = DEFAULT_HTML_TAG
3740) : AztecContentChangeWatcher.AztecTextChangeObserver,
3841 IHtmlTagHandler ,
@@ -67,7 +70,7 @@ class PlaceholderManager(
6770 * @param type placeholder type
6871 * @param attributes other attributes passed to the view. For example a `src` for an image.
6972 */
70- fun insertItem (type : String , vararg attributes : Pair <String , String >) {
73+ suspend fun insertItem (type : String , vararg attributes : Pair <String , String >) {
7174 val adapter = adapters[type]
7275 ? : throw IllegalArgumentException (" Adapter for inserted type not found. Register it with `registerAdapter` method" )
7376 val attrs = getAttributesForMedia(type, attributes)
@@ -103,15 +106,15 @@ class PlaceholderManager(
103106 return drawable
104107 }
105108
106- private fun updateAllBelowSelection (selectionStart : Int ) {
109+ private suspend fun updateAllBelowSelection (selectionStart : Int ) {
107110 positionToId.filter {
108111 it.elementPosition >= selectionStart - 1
109112 }.forEach {
110113 insertContentOverSpanWithId(it.uuid, it.elementPosition)
111114 }
112115 }
113116
114- private fun insertContentOverSpanWithId (uuid : String , currentPosition : Int? = null) {
117+ private suspend fun insertContentOverSpanWithId (uuid : String , currentPosition : Int? = null) {
115118 var aztecAttributes: AztecAttributes ? = null
116119 val predicate = object : AztecText .AttributePredicate {
117120 override fun matches (attrs : Attributes ): Boolean {
@@ -127,7 +130,7 @@ class PlaceholderManager(
127130 insertInPosition(aztecAttributes ? : return , targetPosition, currentPosition)
128131 }
129132
130- private fun insertInPosition (attrs : AztecAttributes , targetPosition : Int , currentPosition : Int? = null) {
133+ private suspend fun insertInPosition (attrs : AztecAttributes , targetPosition : Int , currentPosition : Int? = null) {
131134 if (! validateAttributes(attrs)) {
132135 return
133136 }
@@ -197,7 +200,9 @@ class PlaceholderManager(
197200 * Called when the aztec text content changes.
198201 */
199202 override fun onContentChanged () {
200- updateAllBelowSelection(aztecText.selectionStart)
203+ coroutineScope.launch {
204+ updateAllBelowSelection(aztecText.selectionStart)
205+ }
201206 }
202207
203208 /* *
@@ -279,7 +284,7 @@ class PlaceholderManager(
279284 val type = it.attributes.getValue(TYPE_ATTRIBUTE )
280285 val adapter = adapters[type] ? : return
281286 updateDrawableBounds(adapter, it.attributes, it.drawable)
282- aztecText.post {
287+ coroutineScope.launch {
283288 aztecText.refreshText(false )
284289 insertInPosition(it.attributes, aztecText.editableText.getSpanStart(it))
285290 }
@@ -322,15 +327,15 @@ class PlaceholderManager(
322327 * @param placeholderUuid the placeholder UUID
323328 * @param attrs aztec attributes of the view
324329 */
325- fun createView (context : Context , placeholderUuid : String , attrs : AztecAttributes ): View
330+ suspend fun createView (context : Context , placeholderUuid : String , attrs : AztecAttributes ): View
326331
327332 /* *
328333 * Called after the view is measured. Use this method if you need the actual width and height of the view to
329334 * draw your media.
330335 * @param view the frame layout wrapping the custom view
331336 * @param placeholderUuid the placeholder ID
332337 */
333- fun onViewCreated (view : View , placeholderUuid : String ) {}
338+ suspend fun onViewCreated (view : View , placeholderUuid : String ) {}
334339
335340 /* *
336341 * Called when the placeholder is deleted by the user. Use this method if you need to clear your data when the
0 commit comments