Skip to content

Commit 57c49f2

Browse files
committed
Make sure access to positionToId is thread safe
1 parent 89d6d0f commit 57c49f2

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import kotlinx.coroutines.Job
1818
import kotlinx.coroutines.delay
1919
import kotlinx.coroutines.launch
2020
import kotlinx.coroutines.runBlocking
21+
import kotlinx.coroutines.sync.Mutex
22+
import kotlinx.coroutines.sync.withLock
2123
import org.wordpress.aztec.AztecAttributes
2224
import org.wordpress.aztec.AztecContentChangeWatcher
2325
import 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
@@ -207,8 +210,10 @@ class PlaceholderManager(
207210
* Call this method to reload all the placeholders
208211
*/
209212
suspend fun reloadAllPlaceholders() {
210-
positionToId.forEach {
211-
insertContentOverSpanWithId(it.uuid)
213+
positionToIdMutex.withLock {
214+
positionToId.forEach {
215+
insertContentOverSpanWithId(it.uuid)
216+
}
212217
}
213218
}
214219

@@ -263,8 +268,10 @@ class PlaceholderManager(
263268
parentTextViewRect.top += parentTextViewTopAndBottomOffset
264269
parentTextViewRect.bottom = parentTextViewRect.top + height
265270

266-
positionToId.removeAll {
267-
it.uuid == uuid
271+
positionToIdMutex.withLock {
272+
positionToId.removeAll {
273+
it.uuid == uuid
274+
}
268275
}
269276

270277
var box = container.findViewWithTag<View>(uuid)
@@ -287,7 +294,9 @@ class PlaceholderManager(
287294
box.tag = uuid
288295
box.setBackgroundColor(Color.TRANSPARENT)
289296
box.setOnTouchListener(adapter)
290-
positionToId.add(Placeholder(targetPosition, uuid))
297+
positionToIdMutex.withLock {
298+
positionToId.add(Placeholder(targetPosition, uuid))
299+
}
291300
if (!exists && box.parent == null) {
292301
container.addView(box)
293302
adapter.onViewCreated(box, uuid)
@@ -327,7 +336,11 @@ class PlaceholderManager(
327336
val uuid = attrs.getValue(UUID_ATTRIBUTE)
328337
val adapter = adapters[attrs.getValue(TYPE_ATTRIBUTE)]
329338
adapter?.onPlaceholderDeleted(uuid)
330-
positionToId.removeAll { it.uuid == uuid }
339+
launch {
340+
positionToIdMutex.withLock {
341+
positionToId.removeAll { it.uuid == uuid }
342+
}
343+
}
331344
container.findViewWithTag<View>(uuid)?.let {
332345
it.visibility = View.GONE
333346
container.removeView(it)
@@ -431,19 +444,25 @@ class PlaceholderManager(
431444
}
432445
}
433446

434-
private fun clearAllViews() {
435-
for (placeholder in positionToId) {
436-
container.findViewWithTag<View>(placeholder.uuid)?.let {
437-
it.visibility = View.GONE
438-
container.removeView(it)
447+
private suspend fun clearAllViews() {
448+
positionToIdMutex.withLock {
449+
for (placeholder in positionToId) {
450+
container.findViewWithTag<View>(placeholder.uuid)?.let {
451+
it.visibility = View.GONE
452+
container.removeView(it)
453+
}
439454
}
455+
positionToId.clear()
440456
}
441-
positionToId.clear()
442457
}
443458

444459
override fun onVisibility(visibility: Int) {
445-
for (placeholder in positionToId) {
446-
container.findViewWithTag<View>(placeholder.uuid)?.visibility = visibility
460+
launch {
461+
positionToIdMutex.withLock {
462+
for (placeholder in positionToId) {
463+
container.findViewWithTag<View>(placeholder.uuid)?.visibility = visibility
464+
}
465+
}
447466
}
448467
}
449468

0 commit comments

Comments
 (0)