Skip to content

Commit 944c964

Browse files
committed
Code review
1 parent 1de74b1 commit 944c964

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/SpaceOrderTest.kt

+14-23
Original file line numberDiff line numberDiff line change
@@ -235,35 +235,26 @@ class SpaceOrderTest {
235235
Assert.assertEquals("roomId6", reOrdered[5].first)
236236
}
237237

238+
@Test
239+
fun testComparator() {
240+
listOf(
241+
"roomId2" to "a",
242+
"roomId1" to "b",
243+
"roomId3" to null,
244+
"roomId4" to null,
245+
).assertSpaceOrdered()
246+
}
247+
238248
private fun reOrderWithCommands(orderedSpaces: List<Pair<String, String?>>, orderCommand: List<SpaceOrderUtils.SpaceReOrderCommand>) =
239249
orderedSpaces.map { orderInfo ->
240250
orderInfo.first to (orderCommand.find { it.spaceId == orderInfo.first }?.order ?: orderInfo.second)
241251
}
242-
.sortedWith(TestSpaceComparator())
252+
.sortedWith(testSpaceComparator)
243253

244-
private fun List<Pair<String, String?>>.assertSpaceOrdered() : List<Pair<String, String?>> {
245-
assertEquals(this, this.sortedWith(TestSpaceComparator()))
254+
private fun List<Pair<String, String?>>.assertSpaceOrdered(): List<Pair<String, String?>> {
255+
assertEquals(this, this.sortedWith(testSpaceComparator))
246256
return this
247257
}
248258

249-
class TestSpaceComparator : Comparator<Pair<String, String?>> {
250-
251-
override fun compare(left: Pair<String, String?>?, right: Pair<String, String?>?): Int {
252-
val leftOrder = left?.second
253-
val rightOrder = right?.second
254-
return if (leftOrder != null && rightOrder != null) {
255-
leftOrder.compareTo(rightOrder)
256-
} else {
257-
if (leftOrder == null) {
258-
if (rightOrder == null) {
259-
compareValues(left?.first, right?.first)
260-
} else {
261-
1
262-
}
263-
} else {
264-
-1
265-
}
266-
}
267-
}
268-
}
259+
private val testSpaceComparator = compareBy<Pair<String, String?>, String?>(nullsLast()) { it.second }.thenBy { it.first }
269260
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/space/SpaceOrderUtils.kt

+18-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,26 @@ package org.matrix.android.sdk.api.session.space
1818

1919
import org.matrix.android.sdk.api.util.StringOrderUtils
2020

21+
/**
22+
* Adds some utilities to compute correct string orders when ordering spaces.
23+
* After moving a space (e.g via DnD), client should limit the number of room account data update.
24+
* For example if the space is moved between two other spaces with orders, just update the moved space order by computing
25+
* a mid point between the surrounding orders.
26+
* If the space is moved after a space with no order, all the previous spaces should be then ordered,
27+
* and the computed orders should be chosen so that there is enough gaps in between them to facilitate future re-order.
28+
* Re numbering (i.e change all spaces m.space.order account data) should be avoided as much as possible,
29+
* as the updates might not be atomic for other clients and would makes spaces jump around.
30+
*/
2131
object SpaceOrderUtils {
2232

2333
data class SpaceReOrderCommand(
2434
val spaceId: String,
2535
val order: String
2636
)
2737

38+
/**
39+
* Returns a minimal list of order change in order to re order the space list as per given move.
40+
*/
2841
fun orderCommandsForMove(orderedSpacesToOrderMap: List<Pair<String, String?>>, movedSpaceId: String, delta: Int): List<SpaceReOrderCommand> {
2942
val movedIndex = orderedSpacesToOrderMap.indexOfFirst { it.first == movedSpaceId }
3043
if (movedIndex == -1) return emptyList()
@@ -34,8 +47,6 @@ object SpaceOrderUtils {
3447

3548
val nodesToReNumber = mutableListOf<String>()
3649
var lowerBondOrder: String? = null
37-
var afterSpace: Pair<String, String?>? = null
38-
// if (delta > 0) {
3950
var index = targetIndex
4051
while (index >= 0 && lowerBondOrder == null) {
4152
val node = orderedSpacesToOrderMap.getOrNull(index)
@@ -51,7 +62,9 @@ object SpaceOrderUtils {
5162
index--
5263
}
5364
nodesToReNumber.add(movedSpaceId)
54-
afterSpace = if (orderedSpacesToOrderMap.indices.contains(targetIndex + 1)) orderedSpacesToOrderMap[targetIndex + 1] else null
65+
val afterSpace: Pair<String, String?>? = if (orderedSpacesToOrderMap.indices.contains(targetIndex + 1)) {
66+
orderedSpacesToOrderMap[targetIndex + 1]
67+
} else null
5568

5669
val defaultMaxOrder = CharArray(4) { StringOrderUtils.DEFAULT_ALPHABET.last() }
5770
.joinToString("")
@@ -81,10 +94,10 @@ object SpaceOrderUtils {
8194
}
8295
} ?: emptyList()
8396
} else {
84-
return nodesToReNumber.mapIndexed { index, s ->
97+
return nodesToReNumber.mapIndexed { i, s ->
8598
SpaceReOrderCommand(
8699
s,
87-
newOrder[index]
100+
newOrder[i]
88101
)
89102
}
90103
}

vector/build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,6 @@ dependencies {
366366
implementation "com.jakewharton.rxbinding3:rxbinding-appcompat:$rxbinding_version"
367367
implementation "com.jakewharton.rxbinding3:rxbinding-material:$rxbinding_version"
368368

369-
// implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
370369
implementation("com.airbnb.android:epoxy:$epoxy_version")
371370
implementation "com.airbnb.android:epoxy-glide-preloading:$epoxy_version"
372371
kapt "com.airbnb.android:epoxy-processor:$epoxy_version"

0 commit comments

Comments
 (0)