@@ -3,6 +3,8 @@ package org.wordpress.aztec.placeholders
3
3
import android.graphics.Color
4
4
import android.graphics.Rect
5
5
import android.graphics.drawable.Drawable
6
+ import android.os.Handler
7
+ import android.os.Looper
6
8
import android.text.Editable
7
9
import android.text.Layout
8
10
import android.text.Spanned
@@ -33,6 +35,7 @@ import org.wordpress.aztec.spans.AztecMediaClickableSpan
33
35
import org.xml.sax.Attributes
34
36
import java.lang.ref.WeakReference
35
37
import java.util.UUID
38
+ import java.util.concurrent.CountDownLatch
36
39
import kotlin.coroutines.CoroutineContext
37
40
38
41
/* *
@@ -547,9 +550,20 @@ class ViewPlaceholderManager(
547
550
}
548
551
549
552
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()
552
564
}
565
+ latch.await()
566
+
553
567
return source
554
568
}
555
569
}
0 commit comments