@@ -25,6 +25,7 @@ import kotlinx.coroutines.Dispatchers
25
25
import kotlinx.coroutines.Job
26
26
import kotlinx.coroutines.delay
27
27
import kotlinx.coroutines.flow.MutableStateFlow
28
+ import kotlinx.coroutines.flow.map
28
29
import kotlinx.coroutines.launch
29
30
import kotlinx.coroutines.runBlocking
30
31
import kotlinx.coroutines.sync.Mutex
@@ -72,7 +73,10 @@ class ComposePlaceholderManager(
72
73
private val job = Job ()
73
74
override val coroutineContext: CoroutineContext
74
75
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
+ }
76
80
77
81
init {
78
82
aztecText.setOnVisibilityChangeListener(this )
@@ -95,7 +99,7 @@ class ComposePlaceholderManager(
95
99
fun Draw () {
96
100
val density = LocalDensity .current
97
101
98
- val values = composeViewState.collectAsState() .value.values.filter { it.visible }.sortedBy { it.topMargin }
102
+ val values = composeViewState.collectAsState(emptyList()) .value
99
103
100
104
values.forEach { composeView ->
101
105
Box (
@@ -123,7 +127,7 @@ class ComposePlaceholderManager(
123
127
}
124
128
125
129
fun onDestroy () {
126
- composeViewState .value = emptyMap()
130
+ _composeViewState .value = emptyMap()
127
131
aztecText.contentChangeWatcher.unregisterObserver(this )
128
132
adapters.values.forEach { it.onDestroy() }
129
133
adapters.clear()
@@ -348,10 +352,10 @@ class ComposePlaceholderManager(
348
352
* Call this method to reload all the placeholders
349
353
*/
350
354
suspend fun reloadAllPlaceholders () {
351
- val tempPositionToId = composeViewState .value
355
+ val tempPositionToId = _composeViewState .value
352
356
tempPositionToId.forEach { placeholder ->
353
357
val isValid = positionToIdMutex.withLock {
354
- composeViewState .value.containsKey(placeholder.key)
358
+ _composeViewState .value.containsKey(placeholder.key)
355
359
}
356
360
if (isValid) {
357
361
insertContentOverSpanWithId(placeholder.value.uuid)
@@ -409,7 +413,7 @@ class ComposePlaceholderManager(
409
413
parentTextViewRect.top + = parentTextViewTopAndBottomOffset
410
414
parentTextViewRect.bottom = parentTextViewRect.top + height
411
415
412
- val box = composeViewState .value[uuid]
416
+ val box = _composeViewState .value[uuid]
413
417
val newWidth = adapter.calculateWidth(attrs, windowWidth) - EDITOR_INNER_PADDING
414
418
val newHeight = height - EDITOR_INNER_PADDING
415
419
val padding = 10
@@ -420,12 +424,12 @@ class ComposePlaceholderManager(
420
424
val heightSame = existingView.height == newHeight
421
425
val topMarginSame = existingView.topMargin == newTopPadding
422
426
val leftMarginSame = existingView.leftMargin == newLeftPadding
423
- if (widthSame && heightSame && topMarginSame && leftMarginSame) {
427
+ val attrsSame = existingView.attrs == attrs
428
+ if (widthSame && heightSame && topMarginSame && leftMarginSame && attrsSame) {
424
429
return
425
430
}
426
431
}
427
-
428
- composeViewState.value = composeViewState.value.let { state ->
432
+ _composeViewState .value = _composeViewState .value.let { state ->
429
433
val mutableState = state.toMutableMap()
430
434
mutableState[uuid] = ComposeView (
431
435
uuid = uuid,
@@ -477,7 +481,7 @@ class ComposePlaceholderManager(
477
481
val uuid = attrs.getValue(UUID_ATTRIBUTE )
478
482
val adapter = adapters[attrs.getValue(TYPE_ATTRIBUTE )]
479
483
adapter?.onPlaceholderDeleted(uuid)
480
- composeViewState .value = composeViewState .value.let { state ->
484
+ _composeViewState .value = _composeViewState .value.let { state ->
481
485
val mutableState = state.toMutableMap()
482
486
mutableState.remove(uuid)
483
487
mutableState
@@ -492,7 +496,7 @@ class ComposePlaceholderManager(
492
496
override fun beforeMediaDeleted (attrs : AztecAttributes ) {
493
497
if (validateAttributes(attrs)) {
494
498
val uuid = attrs.getValue(UUID_ATTRIBUTE )
495
- composeViewState .value = composeViewState .value.let { state ->
499
+ _composeViewState .value = _composeViewState .value.let { state ->
496
500
val mutableState = state.toMutableMap()
497
501
mutableState.remove(uuid)
498
502
mutableState
@@ -517,7 +521,7 @@ class ComposePlaceholderManager(
517
521
if (opening) {
518
522
val type = attributes.getValue(TYPE_ATTRIBUTE )
519
523
attributes.getValue(UUID_ATTRIBUTE )?.also { uuid ->
520
- composeViewState .value = composeViewState .value.let { state ->
524
+ _composeViewState .value = _composeViewState .value.let { state ->
521
525
val mutableState = state.toMutableMap()
522
526
mutableState.remove(uuid)
523
527
mutableState
@@ -596,17 +600,19 @@ class ComposePlaceholderManager(
596
600
597
601
private suspend fun clearAllViews () {
598
602
positionToIdMutex.withLock {
599
- composeViewState .value = emptyMap()
603
+ _composeViewState .value = emptyMap()
600
604
}
601
605
}
602
606
603
607
override fun onVisibility (visibility : Int ) {
604
608
launch {
605
609
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
+ }
610
616
}
611
617
}
612
618
}
@@ -620,7 +626,7 @@ class ComposePlaceholderManager(
620
626
}
621
627
622
628
fun getViewInPosition (x : Float , y : Float ): ComposeView ? {
623
- return composeViewState .value.values.firstOrNull {
629
+ return _composeViewState .value.values.firstOrNull {
624
630
(it.topMargin < y && (it.topMargin + it.height) > y) && (it.leftMargin < x && (it.leftMargin + it.width) > x)
625
631
}
626
632
}
0 commit comments