Skip to content

Commit dc5dff3

Browse files
authored
Merge pull request #1103 from wordpress-mobile/issue/improve-behaviour-of-compose-placeholder-manager
Improve behaviour of Compose Placeholder manager
2 parents 6b18e51 + d82b278 commit dc5dff3

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecAttributes.kt

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ class AztecAttributes(attributes: Attributes = AttributesImpl()) : AttributesImp
1414
addAttribute("", key, key, "string", value)
1515
} catch (e: ArrayIndexOutOfBoundsException) {
1616
// https://github.com/wordpress-mobile/AztecEditor-Android/issues/705
17-
AppLog.e(AppLog.T.EDITOR, "Error adding attribute with name: $key and value: $value")
17+
AppLog.e(
18+
AppLog.T.EDITOR,
19+
"Error adding attribute with name: $key and value: $value"
20+
)
1821
logInternalState()
1922
throw e
2023
}
@@ -64,21 +67,35 @@ class AztecAttributes(attributes: Attributes = AttributesImpl()) : AttributesImp
6467

6568
@Synchronized
6669
override fun toString(): String {
67-
val sb = StringBuilder()
70+
return toMap().map { (key, value) ->
71+
"$key=\"$value\""
72+
}.joinToString(" ")
73+
}
74+
75+
override fun hashCode(): Int {
76+
return toMap().hashCode()
77+
}
78+
79+
private fun toMap(): Map<String, String> {
80+
val map = mutableMapOf<String, String>()
6881
try {
69-
for (i in 0..this.length - 1) {
70-
sb.append(this.getLocalName(i))
71-
sb.append("=\"")
72-
sb.append(this.getValue(i))
73-
sb.append("\" ")
82+
for (i in 0..<this.length) {
83+
map[this.getLocalName(i)] = this.getValue(i)
7484
}
7585
} catch (e: ArrayIndexOutOfBoundsException) {
7686
// https://github.com/wordpress-mobile/AztecEditor-Android/issues/705
77-
AppLog.e(AppLog.T.EDITOR, "IOOB occurred in toString. Dumping partial state:")
78-
AppLog.e(AppLog.T.EDITOR, sb.trimEnd().toString())
87+
AppLog.e(AppLog.T.EDITOR, "IOOB occurred in toMap. Dumping partial state:")
88+
AppLog.e(AppLog.T.EDITOR, map.toString())
7989
throw e
8090
}
91+
return map
92+
}
8193

82-
return sb.trimEnd().toString()
94+
override fun equals(other: Any?): Boolean {
95+
if (other is AztecAttributes) {
96+
return this.toMap() == other.toMap()
97+
} else {
98+
return super.equals(other)
99+
}
83100
}
84101
}

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import kotlinx.coroutines.Dispatchers
2525
import kotlinx.coroutines.Job
2626
import kotlinx.coroutines.delay
2727
import kotlinx.coroutines.flow.MutableStateFlow
28+
import kotlinx.coroutines.flow.map
2829
import kotlinx.coroutines.launch
2930
import kotlinx.coroutines.runBlocking
3031
import 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

Comments
 (0)