Skip to content

Commit 1597454

Browse files
committed
Do not iterate in while when history cursor is outside of list range
1 parent 6b18e51 commit 1597454

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
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: 13 additions & 6 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
@@ -95,7 +96,11 @@ class ComposePlaceholderManager(
9596
fun Draw() {
9697
val density = LocalDensity.current
9798

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

100105
values.forEach { composeView ->
101106
Box(
@@ -420,11 +425,11 @@ class ComposePlaceholderManager(
420425
val heightSame = existingView.height == newHeight
421426
val topMarginSame = existingView.topMargin == newTopPadding
422427
val leftMarginSame = existingView.leftMargin == newLeftPadding
423-
if (widthSame && heightSame && topMarginSame && leftMarginSame) {
428+
val attrsSame = existingView.attrs == attrs
429+
if (widthSame && heightSame && topMarginSame && leftMarginSame && attrsSame) {
424430
return
425431
}
426432
}
427-
428433
composeViewState.value = composeViewState.value.let { state ->
429434
val mutableState = state.toMutableMap()
430435
mutableState[uuid] = ComposeView(
@@ -604,9 +609,11 @@ class ComposePlaceholderManager(
604609
launch {
605610
positionToIdMutex.withLock {
606611
composeViewState.value = composeViewState.value.let { state ->
607-
state.mapValues { (_, value) -> value.copy(
608-
visible = View.VISIBLE == visibility
609-
) }
612+
state.mapValues { (_, value) ->
613+
value.copy(
614+
visible = View.VISIBLE == visibility
615+
)
616+
}
610617
}
611618
}
612619
}

0 commit comments

Comments
 (0)