@@ -18,6 +18,8 @@ import kotlinx.coroutines.Job
1818import kotlinx.coroutines.delay
1919import kotlinx.coroutines.launch
2020import kotlinx.coroutines.runBlocking
21+ import kotlinx.coroutines.sync.Mutex
22+ import kotlinx.coroutines.sync.withLock
2123import org.wordpress.aztec.AztecAttributes
2224import org.wordpress.aztec.AztecContentChangeWatcher
2325import org.wordpress.aztec.AztecText
@@ -52,6 +54,7 @@ class PlaceholderManager(
5254 AztecText .OnVisibilityChangeListener ,
5355 CoroutineScope {
5456 private val adapters = mutableMapOf<String , PlaceholderAdapter >()
57+ private val positionToIdMutex = Mutex ()
5558 private val positionToId = mutableSetOf<Placeholder >()
5659 private val job = Job ()
5760 override val coroutineContext: CoroutineContext
@@ -163,8 +166,10 @@ class PlaceholderManager(
163166 * Call this method to reload all the placeholders
164167 */
165168 suspend fun reloadAllPlaceholders () {
166- positionToId.forEach {
167- insertContentOverSpanWithId(it.uuid)
169+ positionToIdMutex.withLock {
170+ positionToId.forEach {
171+ insertContentOverSpanWithId(it.uuid)
172+ }
168173 }
169174 }
170175
@@ -219,8 +224,10 @@ class PlaceholderManager(
219224 parentTextViewRect.top + = parentTextViewTopAndBottomOffset
220225 parentTextViewRect.bottom = parentTextViewRect.top + height
221226
222- positionToId.removeAll {
223- it.uuid == uuid
227+ positionToIdMutex.withLock {
228+ positionToId.removeAll {
229+ it.uuid == uuid
230+ }
224231 }
225232
226233 var box = container.findViewWithTag<View >(uuid)
@@ -243,7 +250,9 @@ class PlaceholderManager(
243250 box.tag = uuid
244251 box.setBackgroundColor(Color .TRANSPARENT )
245252 box.setOnTouchListener(adapter)
246- positionToId.add(Placeholder (targetPosition, uuid))
253+ positionToIdMutex.withLock {
254+ positionToId.add(Placeholder (targetPosition, uuid))
255+ }
247256 if (! exists && box.parent == null ) {
248257 container.addView(box)
249258 adapter.onViewCreated(box, uuid)
@@ -283,7 +292,11 @@ class PlaceholderManager(
283292 val uuid = attrs.getValue(UUID_ATTRIBUTE )
284293 val adapter = adapters[attrs.getValue(TYPE_ATTRIBUTE )]
285294 adapter?.onPlaceholderDeleted(uuid)
286- positionToId.removeAll { it.uuid == uuid }
295+ launch {
296+ positionToIdMutex.withLock {
297+ positionToId.removeAll { it.uuid == uuid }
298+ }
299+ }
287300 container.findViewWithTag<View >(uuid)?.let {
288301 it.visibility = View .GONE
289302 container.removeView(it)
@@ -387,19 +400,25 @@ class PlaceholderManager(
387400 }
388401 }
389402
390- private fun clearAllViews () {
391- for (placeholder in positionToId) {
392- container.findViewWithTag<View >(placeholder.uuid)?.let {
393- it.visibility = View .GONE
394- container.removeView(it)
403+ private suspend fun clearAllViews () {
404+ positionToIdMutex.withLock {
405+ for (placeholder in positionToId) {
406+ container.findViewWithTag<View >(placeholder.uuid)?.let {
407+ it.visibility = View .GONE
408+ container.removeView(it)
409+ }
395410 }
411+ positionToId.clear()
396412 }
397- positionToId.clear()
398413 }
399414
400415 override fun onVisibility (visibility : Int ) {
401- for (placeholder in positionToId) {
402- container.findViewWithTag<View >(placeholder.uuid)?.visibility = visibility
416+ launch {
417+ positionToIdMutex.withLock {
418+ for (placeholder in positionToId) {
419+ container.findViewWithTag<View >(placeholder.uuid)?.visibility = visibility
420+ }
421+ }
403422 }
404423 }
405424
0 commit comments