@@ -18,13 +18,26 @@ package org.matrix.android.sdk.api.session.space
18
18
19
19
import org.matrix.android.sdk.api.util.StringOrderUtils
20
20
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
+ */
21
31
object SpaceOrderUtils {
22
32
23
33
data class SpaceReOrderCommand (
24
34
val spaceId : String ,
25
35
val order : String
26
36
)
27
37
38
+ /* *
39
+ * Returns a minimal list of order change in order to re order the space list as per given move.
40
+ */
28
41
fun orderCommandsForMove (orderedSpacesToOrderMap : List <Pair <String , String ?>>, movedSpaceId : String , delta : Int ): List <SpaceReOrderCommand > {
29
42
val movedIndex = orderedSpacesToOrderMap.indexOfFirst { it.first == movedSpaceId }
30
43
if (movedIndex == - 1 ) return emptyList()
@@ -34,8 +47,6 @@ object SpaceOrderUtils {
34
47
35
48
val nodesToReNumber = mutableListOf<String >()
36
49
var lowerBondOrder: String? = null
37
- var afterSpace: Pair <String , String ?>? = null
38
- // if (delta > 0) {
39
50
var index = targetIndex
40
51
while (index >= 0 && lowerBondOrder == null ) {
41
52
val node = orderedSpacesToOrderMap.getOrNull(index)
@@ -51,7 +62,9 @@ object SpaceOrderUtils {
51
62
index--
52
63
}
53
64
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
55
68
56
69
val defaultMaxOrder = CharArray (4 ) { StringOrderUtils .DEFAULT_ALPHABET .last() }
57
70
.joinToString(" " )
@@ -81,10 +94,10 @@ object SpaceOrderUtils {
81
94
}
82
95
} ? : emptyList()
83
96
} else {
84
- return nodesToReNumber.mapIndexed { index , s ->
97
+ return nodesToReNumber.mapIndexed { i , s ->
85
98
SpaceReOrderCommand (
86
99
s,
87
- newOrder[index ]
100
+ newOrder[i ]
88
101
)
89
102
}
90
103
}
0 commit comments