Skip to content

Commit d82b278

Browse files
committed
Pull flow operators out of compose
1 parent 1597454 commit d82b278

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ class ComposePlaceholderManager(
7373
private val job = Job()
7474
override val coroutineContext: CoroutineContext
7575
get() = Dispatchers.Main + job
76-
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+
}
7780

7881
init {
7982
aztecText.setOnVisibilityChangeListener(this)
@@ -96,11 +99,7 @@ class ComposePlaceholderManager(
9699
fun Draw() {
97100
val density = LocalDensity.current
98101

99-
val values = composeViewState.map { map ->
100-
map.values.filter { it.visible }.sortedBy { it.topMargin }
101-
}.collectAsState(
102-
emptyList()
103-
).value
102+
val values = composeViewState.collectAsState(emptyList()).value
104103

105104
values.forEach { composeView ->
106105
Box(
@@ -128,7 +127,7 @@ class ComposePlaceholderManager(
128127
}
129128

130129
fun onDestroy() {
131-
composeViewState.value = emptyMap()
130+
_composeViewState.value = emptyMap()
132131
aztecText.contentChangeWatcher.unregisterObserver(this)
133132
adapters.values.forEach { it.onDestroy() }
134133
adapters.clear()
@@ -353,10 +352,10 @@ class ComposePlaceholderManager(
353352
* Call this method to reload all the placeholders
354353
*/
355354
suspend fun reloadAllPlaceholders() {
356-
val tempPositionToId = composeViewState.value
355+
val tempPositionToId = _composeViewState.value
357356
tempPositionToId.forEach { placeholder ->
358357
val isValid = positionToIdMutex.withLock {
359-
composeViewState.value.containsKey(placeholder.key)
358+
_composeViewState.value.containsKey(placeholder.key)
360359
}
361360
if (isValid) {
362361
insertContentOverSpanWithId(placeholder.value.uuid)
@@ -414,7 +413,7 @@ class ComposePlaceholderManager(
414413
parentTextViewRect.top += parentTextViewTopAndBottomOffset
415414
parentTextViewRect.bottom = parentTextViewRect.top + height
416415

417-
val box = composeViewState.value[uuid]
416+
val box = _composeViewState.value[uuid]
418417
val newWidth = adapter.calculateWidth(attrs, windowWidth) - EDITOR_INNER_PADDING
419418
val newHeight = height - EDITOR_INNER_PADDING
420419
val padding = 10
@@ -430,7 +429,7 @@ class ComposePlaceholderManager(
430429
return
431430
}
432431
}
433-
composeViewState.value = composeViewState.value.let { state ->
432+
_composeViewState.value = _composeViewState.value.let { state ->
434433
val mutableState = state.toMutableMap()
435434
mutableState[uuid] = ComposeView(
436435
uuid = uuid,
@@ -482,7 +481,7 @@ class ComposePlaceholderManager(
482481
val uuid = attrs.getValue(UUID_ATTRIBUTE)
483482
val adapter = adapters[attrs.getValue(TYPE_ATTRIBUTE)]
484483
adapter?.onPlaceholderDeleted(uuid)
485-
composeViewState.value = composeViewState.value.let { state ->
484+
_composeViewState.value = _composeViewState.value.let { state ->
486485
val mutableState = state.toMutableMap()
487486
mutableState.remove(uuid)
488487
mutableState
@@ -497,7 +496,7 @@ class ComposePlaceholderManager(
497496
override fun beforeMediaDeleted(attrs: AztecAttributes) {
498497
if (validateAttributes(attrs)) {
499498
val uuid = attrs.getValue(UUID_ATTRIBUTE)
500-
composeViewState.value = composeViewState.value.let { state ->
499+
_composeViewState.value = _composeViewState.value.let { state ->
501500
val mutableState = state.toMutableMap()
502501
mutableState.remove(uuid)
503502
mutableState
@@ -522,7 +521,7 @@ class ComposePlaceholderManager(
522521
if (opening) {
523522
val type = attributes.getValue(TYPE_ATTRIBUTE)
524523
attributes.getValue(UUID_ATTRIBUTE)?.also { uuid ->
525-
composeViewState.value = composeViewState.value.let { state ->
524+
_composeViewState.value = _composeViewState.value.let { state ->
526525
val mutableState = state.toMutableMap()
527526
mutableState.remove(uuid)
528527
mutableState
@@ -601,14 +600,14 @@ class ComposePlaceholderManager(
601600

602601
private suspend fun clearAllViews() {
603602
positionToIdMutex.withLock {
604-
composeViewState.value = emptyMap()
603+
_composeViewState.value = emptyMap()
605604
}
606605
}
607606

608607
override fun onVisibility(visibility: Int) {
609608
launch {
610609
positionToIdMutex.withLock {
611-
composeViewState.value = composeViewState.value.let { state ->
610+
_composeViewState.value = _composeViewState.value.let { state ->
612611
state.mapValues { (_, value) ->
613612
value.copy(
614613
visible = View.VISIBLE == visibility
@@ -627,7 +626,7 @@ class ComposePlaceholderManager(
627626
}
628627

629628
fun getViewInPosition(x: Float, y: Float): ComposeView? {
630-
return composeViewState.value.values.firstOrNull {
629+
return _composeViewState.value.values.firstOrNull {
631630
(it.topMargin < y && (it.topMargin + it.height) > y) && (it.leftMargin < x && (it.leftMargin + it.width) > x)
632631
}
633632
}

0 commit comments

Comments
 (0)