@@ -32,6 +32,10 @@ import org.wordpress.aztec.AztecContentChangeWatcher
3232import org.wordpress.aztec.AztecText
3333import org.wordpress.aztec.Constants
3434import org.wordpress.aztec.Html
35+ import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.DEFAULT_HTML_TAG
36+ import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.EDITOR_INNER_PADDING
37+ import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.TYPE_ATTRIBUTE
38+ import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.UUID_ATTRIBUTE
3539import org.wordpress.aztec.plugins.html2visual.IHtmlPreprocessor
3640import org.wordpress.aztec.plugins.html2visual.IHtmlTagHandler
3741import org.wordpress.aztec.spans.AztecMediaClickableSpan
@@ -63,7 +67,6 @@ class ComposePlaceholderManager(
6367 IHtmlPreprocessor {
6468 private val adapters = mutableMapOf<String , ComposePlaceholderAdapter >()
6569 private val positionToIdMutex = Mutex ()
66- private val positionToId = mutableSetOf<Placeholder >()
6770 private val job = Job ()
6871 override val coroutineContext: CoroutineContext
6972 get() = Dispatchers .Main + job
@@ -111,23 +114,14 @@ class ComposePlaceholderManager(
111114 adapters[composeView.adapterKey]?.Placeholder (
112115 composeView.uuid,
113116 composeView.attrs,
114- composeView.width,
115- composeView.height,
116117 )
117118 }
118119 }
119120 }
120121 }
121122
122123 fun onDestroy () {
123- positionToId.forEach {
124- composeViewState.value = composeViewState.value.let { state ->
125- val mutableState = state.toMutableMap()
126- mutableState.remove(it.uuid)
127- mutableState
128- }
129- }
130- positionToId.clear()
124+ composeViewState.value = emptyMap()
131125 aztecText.contentChangeWatcher.unregisterObserver(this )
132126 adapters.values.forEach { it.onDestroy() }
133127 adapters.clear()
@@ -352,13 +346,13 @@ class ComposePlaceholderManager(
352346 * Call this method to reload all the placeholders
353347 */
354348 suspend fun reloadAllPlaceholders () {
355- val tempPositionToId = positionToId.toList()
349+ val tempPositionToId = composeViewState.value
356350 tempPositionToId.forEach { placeholder ->
357351 val isValid = positionToIdMutex.withLock {
358- positionToId.contains (placeholder)
352+ composeViewState.value.containsKey (placeholder.key )
359353 }
360354 if (isValid) {
361- insertContentOverSpanWithId(placeholder.uuid)
355+ insertContentOverSpanWithId(placeholder.value. uuid)
362356 }
363357 }
364358 }
@@ -427,11 +421,6 @@ class ComposePlaceholderManager(
427421 if (widthSame && heightSame && topMarginSame && leftMarginSame) {
428422 return
429423 }
430- positionToIdMutex.withLock {
431- positionToId.removeAll {
432- it.uuid == uuid
433- }
434- }
435424 }
436425
437426 composeViewState.value = composeViewState.value.let { state ->
@@ -448,10 +437,6 @@ class ComposePlaceholderManager(
448437 )
449438 mutableState
450439 }
451-
452- positionToIdMutex.withLock {
453- positionToId.add(Placeholder (targetPosition, uuid))
454- }
455440 }
456441
457442 private fun validateAttributes (attributes : AztecAttributes ): Boolean {
@@ -490,11 +475,6 @@ class ComposePlaceholderManager(
490475 val uuid = attrs.getValue(UUID_ATTRIBUTE )
491476 val adapter = adapters[attrs.getValue(TYPE_ATTRIBUTE )]
492477 adapter?.onPlaceholderDeleted(uuid)
493- launch {
494- positionToIdMutex.withLock {
495- positionToId.removeAll { it.uuid == uuid }
496- }
497- }
498478 composeViewState.value = composeViewState.value.let { state ->
499479 val mutableState = state.toMutableMap()
500480 mutableState.remove(uuid)
@@ -614,44 +594,22 @@ class ComposePlaceholderManager(
614594
615595 private suspend fun clearAllViews () {
616596 positionToIdMutex.withLock {
617- for (placeholder in positionToId) {
618- composeViewState.value = composeViewState.value.let { state ->
619- val mutableState = state.toMutableMap()
620- mutableState.remove(placeholder.uuid)
621- mutableState
622- }
623- }
624- positionToId.clear()
597+ composeViewState.value = emptyMap()
625598 }
626599 }
627600
628601 override fun onVisibility (visibility : Int ) {
629602 launch {
630603 positionToIdMutex.withLock {
631- for (placeholder in positionToId) {
632- composeViewState.value = composeViewState.value.let { state ->
633- val mutableState = state.toMutableMap()
634- mutableState[placeholder.uuid]?.copy(
635- visible = View .VISIBLE == visibility
636- )?.let {
637- mutableState[placeholder.uuid] = it
638- }
639- mutableState
640- }
604+ composeViewState.value = composeViewState.value.let { state ->
605+ state.mapValues { (_, value) -> value.copy(
606+ visible = View .VISIBLE == visibility
607+ ) }
641608 }
642609 }
643610 }
644611 }
645612
646- data class Placeholder (val elementPosition : Int , val uuid : String )
647-
648- companion object {
649- private const val DEFAULT_HTML_TAG = " placeholder"
650- private const val UUID_ATTRIBUTE = " uuid"
651- private const val TYPE_ATTRIBUTE = " type"
652- private const val EDITOR_INNER_PADDING = 20
653- }
654-
655613 override fun beforeHtmlProcessed (source : String ): String {
656614 runBlocking {
657615 clearAllViews()
0 commit comments