Skip to content

Commit 7d70d92

Browse files
committed
Make sure placeholders are not drawn twice when activity is destroyed
1 parent ce42c26 commit 7d70d92

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,27 +287,42 @@ class PlaceholderManager(
287287
}
288288

289289
override fun mediaLoadingStarted() {
290-
clearAllViews()
291290
val spans = aztecText.editableText.getSpans(0, aztecText.editableText.length, AztecPlaceholderSpan::class.java)
292291

293292
if (spans == null || spans.isEmpty()) {
294293
return
295294
}
295+
aztecText.viewTreeObserver.removeOnGlobalLayoutListener(globalLayoutListener)
296+
aztecText.viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener)
297+
}
298+
299+
private val globalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener {
300+
private var job: Job? = null
301+
override fun onGlobalLayout() {
302+
if (job?.isActive == true) {
303+
return
304+
}
305+
aztecText.viewTreeObserver.removeOnGlobalLayoutListener(this)
306+
val spans = aztecText.editableText.getSpans(
307+
0,
308+
aztecText.editableText.length,
309+
AztecPlaceholderSpan::class.java
310+
)
296311

297-
aztecText.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
298-
override fun onGlobalLayout() {
299-
aztecText.viewTreeObserver.removeOnGlobalLayoutListener(this)
312+
if (spans == null || spans.isEmpty()) {
313+
return
314+
}
315+
job = launch {
316+
clearAllViews()
300317
spans.forEach {
301318
val type = it.attributes.getValue(TYPE_ATTRIBUTE)
302-
val adapter = adapters[type] ?: return
303-
launch {
304-
updateDrawableBounds(adapter, it.attributes, it.drawable)
305-
aztecText.refreshText(false)
306-
insertInPosition(it.attributes, aztecText.editableText.getSpanStart(it))
307-
}
319+
val adapter = adapters[type] ?: return@forEach
320+
updateDrawableBounds(adapter, it.attributes, it.drawable)
321+
aztecText.refreshText(false)
322+
insertInPosition(it.attributes, aztecText.editableText.getSpanStart(it))
308323
}
309324
}
310-
})
325+
}
311326
}
312327

313328
private suspend fun updateDrawableBounds(adapter: PlaceholderAdapter, attrs: AztecAttributes, drawable: Drawable?) {

0 commit comments

Comments
 (0)