@@ -3,6 +3,8 @@ package org.wordpress.aztec.placeholders
33import android.graphics.Color
44import android.graphics.Rect
55import android.graphics.drawable.Drawable
6+ import android.os.Handler
7+ import android.os.Looper
68import android.text.Editable
79import android.text.Layout
810import android.text.Spanned
@@ -33,6 +35,7 @@ import org.wordpress.aztec.spans.AztecMediaClickableSpan
3335import org.xml.sax.Attributes
3436import java.lang.ref.WeakReference
3537import java.util.UUID
38+ import java.util.concurrent.CountDownLatch
3639import kotlin.coroutines.CoroutineContext
3740
3841/* *
@@ -547,9 +550,20 @@ class ViewPlaceholderManager(
547550 }
548551
549552 override fun beforeHtmlProcessed (source : String ): String {
550- runBlocking {
551- clearAllViews()
553+ // If on main thread, execute directly
554+ if (Looper .myLooper() == Looper .getMainLooper()) {
555+ runBlocking { clearAllViews() }
556+ return source
557+ }
558+
559+ // Otherwise, post to main thread and wait
560+ val latch = CountDownLatch (1 )
561+ Handler (Looper .getMainLooper()).post {
562+ runBlocking { clearAllViews() }
563+ latch.countDown()
552564 }
565+ latch.await()
566+
553567 return source
554568 }
555569}
0 commit comments