@@ -18,13 +18,26 @@ package org.matrix.android.sdk.api.session.space
1818
1919import 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+ */
2131object 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 }
0 commit comments