@@ -25,6 +25,7 @@ import kotlinx.coroutines.Dispatchers
2525import kotlinx.coroutines.Job
2626import kotlinx.coroutines.delay
2727import kotlinx.coroutines.flow.MutableStateFlow
28+ import kotlinx.coroutines.flow.map
2829import kotlinx.coroutines.launch
2930import kotlinx.coroutines.runBlocking
3031import kotlinx.coroutines.sync.Mutex
@@ -72,7 +73,10 @@ class ComposePlaceholderManager(
7273 private val job = Job ()
7374 override val coroutineContext: CoroutineContext
7475 get() = Dispatchers .Main + job
75- private val composeViewState = MutableStateFlow (emptyMap<String , ComposeView >())
76+ private val _composeViewState = MutableStateFlow (emptyMap<String , ComposeView >())
77+ private val composeViewState = _composeViewState .map { map ->
78+ map.values.filter { it.visible }.sortedBy { it.topMargin }
79+ }
7680
7781 init {
7882 aztecText.setOnVisibilityChangeListener(this )
@@ -95,7 +99,7 @@ class ComposePlaceholderManager(
9599 fun Draw () {
96100 val density = LocalDensity .current
97101
98- val values = composeViewState.collectAsState() .value.values.filter { it.visible }.sortedBy { it.topMargin }
102+ val values = composeViewState.collectAsState(emptyList()) .value
99103
100104 values.forEach { composeView ->
101105 Box (
@@ -123,7 +127,7 @@ class ComposePlaceholderManager(
123127 }
124128
125129 fun onDestroy () {
126- composeViewState .value = emptyMap()
130+ _composeViewState .value = emptyMap()
127131 aztecText.contentChangeWatcher.unregisterObserver(this )
128132 adapters.values.forEach { it.onDestroy() }
129133 adapters.clear()
@@ -348,10 +352,10 @@ class ComposePlaceholderManager(
348352 * Call this method to reload all the placeholders
349353 */
350354 suspend fun reloadAllPlaceholders () {
351- val tempPositionToId = composeViewState .value
355+ val tempPositionToId = _composeViewState .value
352356 tempPositionToId.forEach { placeholder ->
353357 val isValid = positionToIdMutex.withLock {
354- composeViewState .value.containsKey(placeholder.key)
358+ _composeViewState .value.containsKey(placeholder.key)
355359 }
356360 if (isValid) {
357361 insertContentOverSpanWithId(placeholder.value.uuid)
@@ -409,7 +413,7 @@ class ComposePlaceholderManager(
409413 parentTextViewRect.top + = parentTextViewTopAndBottomOffset
410414 parentTextViewRect.bottom = parentTextViewRect.top + height
411415
412- val box = composeViewState .value[uuid]
416+ val box = _composeViewState .value[uuid]
413417 val newWidth = adapter.calculateWidth(attrs, windowWidth) - EDITOR_INNER_PADDING
414418 val newHeight = height - EDITOR_INNER_PADDING
415419 val padding = 10
@@ -420,12 +424,12 @@ class ComposePlaceholderManager(
420424 val heightSame = existingView.height == newHeight
421425 val topMarginSame = existingView.topMargin == newTopPadding
422426 val leftMarginSame = existingView.leftMargin == newLeftPadding
423- if (widthSame && heightSame && topMarginSame && leftMarginSame) {
427+ val attrsSame = existingView.attrs == attrs
428+ if (widthSame && heightSame && topMarginSame && leftMarginSame && attrsSame) {
424429 return
425430 }
426431 }
427-
428- composeViewState.value = composeViewState.value.let { state ->
432+ _composeViewState .value = _composeViewState .value.let { state ->
429433 val mutableState = state.toMutableMap()
430434 mutableState[uuid] = ComposeView (
431435 uuid = uuid,
@@ -477,7 +481,7 @@ class ComposePlaceholderManager(
477481 val uuid = attrs.getValue(UUID_ATTRIBUTE )
478482 val adapter = adapters[attrs.getValue(TYPE_ATTRIBUTE )]
479483 adapter?.onPlaceholderDeleted(uuid)
480- composeViewState .value = composeViewState .value.let { state ->
484+ _composeViewState .value = _composeViewState .value.let { state ->
481485 val mutableState = state.toMutableMap()
482486 mutableState.remove(uuid)
483487 mutableState
@@ -492,7 +496,7 @@ class ComposePlaceholderManager(
492496 override fun beforeMediaDeleted (attrs : AztecAttributes ) {
493497 if (validateAttributes(attrs)) {
494498 val uuid = attrs.getValue(UUID_ATTRIBUTE )
495- composeViewState .value = composeViewState .value.let { state ->
499+ _composeViewState .value = _composeViewState .value.let { state ->
496500 val mutableState = state.toMutableMap()
497501 mutableState.remove(uuid)
498502 mutableState
@@ -517,7 +521,7 @@ class ComposePlaceholderManager(
517521 if (opening) {
518522 val type = attributes.getValue(TYPE_ATTRIBUTE )
519523 attributes.getValue(UUID_ATTRIBUTE )?.also { uuid ->
520- composeViewState .value = composeViewState .value.let { state ->
524+ _composeViewState .value = _composeViewState .value.let { state ->
521525 val mutableState = state.toMutableMap()
522526 mutableState.remove(uuid)
523527 mutableState
@@ -596,17 +600,19 @@ class ComposePlaceholderManager(
596600
597601 private suspend fun clearAllViews () {
598602 positionToIdMutex.withLock {
599- composeViewState .value = emptyMap()
603+ _composeViewState .value = emptyMap()
600604 }
601605 }
602606
603607 override fun onVisibility (visibility : Int ) {
604608 launch {
605609 positionToIdMutex.withLock {
606- composeViewState.value = composeViewState.value.let { state ->
607- state.mapValues { (_, value) -> value.copy(
608- visible = View .VISIBLE == visibility
609- ) }
610+ _composeViewState .value = _composeViewState .value.let { state ->
611+ state.mapValues { (_, value) ->
612+ value.copy(
613+ visible = View .VISIBLE == visibility
614+ )
615+ }
610616 }
611617 }
612618 }
@@ -620,7 +626,7 @@ class ComposePlaceholderManager(
620626 }
621627
622628 fun getViewInPosition (x : Float , y : Float ): ComposeView ? {
623- return composeViewState .value.values.firstOrNull {
629+ return _composeViewState .value.values.firstOrNull {
624630 (it.topMargin < y && (it.topMargin + it.height) > y) && (it.leftMargin < x && (it.leftMargin + it.width) > x)
625631 }
626632 }
0 commit comments