Keep list positions unique and tidy, notify through the provider (#136)
Also carry the order over when copying provider lists into the own store.
This commit is contained in:
1 parent
426378a106
commit
8eede92d84
5 files changed
+64
-22
No files matched your search
+13
@@ -52,4 +52,17 @@ class ExternalProviderListOrderTest {
|
||||
assertThat(AndroidTasksDataSource(context, resolver).taskLists().map { it.id }.filter { it in created })
|
||||
.containsExactly(c, a, b).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aDeletedListLeavesNoPositionBehind() {
|
||||
val (a, b, c) = created
|
||||
source.reorderLists(listOf(c, a, b))
|
||||
|
||||
source.deleteList(c)
|
||||
created -= c
|
||||
|
||||
val stored = context.getSharedPreferences("provider_list_order", Context.MODE_PRIVATE)
|
||||
.getString(resolver.resolve()!!.authority, null)
|
||||
assertThat(ListOrder.decode(stored).keys).doesNotContain(c)
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,6 @@ import de.jeanlucmakiola.agendula.domain.recurrence.ExpansionWindow
|
||||
import de.jeanlucmakiola.agendula.domain.recurrence.RecurrenceExpander
|
||||
import de.jeanlucmakiola.agendula.domain.recurrence.RecurrenceSpec
|
||||
import java.time.ZoneId
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlin.time.Instant
|
||||
@@ -58,22 +57,30 @@ class AndroidTasksDataSource @Inject constructor(
|
||||
val reader = CursorColumnReader(c)
|
||||
buildList { while (c.moveToNext()) add(TaskMapper.taskList(reader)) }
|
||||
} ?: emptyList()
|
||||
return ListOrder.sorted(lists, ListOrder.decode(listOrder.getString(authority, null)))
|
||||
val order = ListOrder.decode(listOrder.getString(authority, null))
|
||||
val known = lists.mapTo(HashSet()) { it.id }
|
||||
// A list gone from the provider takes its position along; its id may be reused.
|
||||
if (order.keys.any { it !in known }) storeListOrder(authority, order.filterKeys { it in known })
|
||||
return ListOrder.sorted(lists, order)
|
||||
}
|
||||
|
||||
private val listOrder get() = context.getSharedPreferences(LIST_ORDER, Context.MODE_PRIVATE)
|
||||
|
||||
/** Notified on writes the provider never hears about, such as the list order. */
|
||||
private val localObservers = CopyOnWriteArrayList<() -> Unit>()
|
||||
private fun storeListOrder(authority: String, order: Map<Long, Int>) {
|
||||
listOrder.edit().putString(authority, ListOrder.encode(order)).commit()
|
||||
}
|
||||
|
||||
/** Kept by Agendula, as the provider's lists table has no order of its own. */
|
||||
override fun reorderLists(listIds: List<Long>) {
|
||||
val authority = authority()
|
||||
val order = ListOrder.reordered(ListOrder.decode(listOrder.getString(authority, null)), listIds)
|
||||
listOrder.edit().putString(authority, ListOrder.encode(order)).apply()
|
||||
localObservers.forEach { it() }
|
||||
synchronized(listOrderLock) {
|
||||
storeListOrder(authority, ListOrder.reordered(taskLists().map { it.id }, listIds))
|
||||
}
|
||||
resolver.notifyChange(TasksContract.listsUri(authority), null)
|
||||
}
|
||||
|
||||
private val listOrderLock = Any()
|
||||
|
||||
override fun tasks(query: TaskQuery): List<Task> {
|
||||
val clauses = mutableListOf<String>()
|
||||
val args = mutableListOf<String>()
|
||||
@@ -559,6 +566,11 @@ class AndroidTasksDataSource @Inject constructor(
|
||||
override fun deleteList(listId: Long) {
|
||||
val rows = resolver.delete(listSyncUri(listId), null, null)
|
||||
if (rows == 0) throw TaskWriteFailedException("delete list $listId")
|
||||
val authority = authority()
|
||||
synchronized(listOrderLock) {
|
||||
val order = ListOrder.decode(listOrder.getString(authority, null))
|
||||
if (listId in order) storeListOrder(authority, order - listId)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -591,11 +603,7 @@ class AndroidTasksDataSource @Inject constructor(
|
||||
runCatching { resolver.unregisterContentObserver(observer) }
|
||||
throw e
|
||||
}
|
||||
localObservers += onChange
|
||||
return AutoCloseable {
|
||||
localObservers -= onChange
|
||||
runCatching { resolver.unregisterContentObserver(observer) }
|
||||
}
|
||||
return AutoCloseable { runCatching { resolver.unregisterContentObserver(observer) } }
|
||||
}
|
||||
|
||||
private fun Map<String, Any?>.toContentValues(): ContentValues {
|
||||
|
||||
@@ -15,10 +15,21 @@ object ListOrder {
|
||||
|
||||
fun encode(order: Map<Long, Int>): String = order.entries.joinToString(",") { "${it.key}=${it.value}" }
|
||||
|
||||
/** [order] with [listIds] — one account's lists, as the user arranged them — in that order. */
|
||||
fun reordered(order: Map<Long, Int>, listIds: List<Long>): Map<Long, Int> =
|
||||
order + listIds.withIndex().associate { it.value to it.index }
|
||||
/**
|
||||
* Positions for every list in [shown] — the lists in their current order —
|
||||
* after one account's [listIds] were rearranged: those take each other's
|
||||
* slots in their new order, and every other list keeps its own.
|
||||
*/
|
||||
fun reordered(shown: List<Long>, listIds: List<Long>): Map<Long, Int> {
|
||||
val moved = listIds.iterator()
|
||||
val moving = listIds.toSet()
|
||||
val result = shown.map { if (it in moving && moved.hasNext()) moved.next() else it }.toMutableList()
|
||||
moved.forEachRemaining { if (it !in result) result += it }
|
||||
return result.withIndex().associate { it.value to it.index }
|
||||
}
|
||||
|
||||
fun sorted(lists: List<TaskList>, order: Map<Long, Int>): List<TaskList> =
|
||||
lists.sortedWith(compareBy({ it.accountName }, { order[it.id] ?: Int.MAX_VALUE }, { it.name }))
|
||||
fun sorted(lists: List<TaskList>, order: Map<Long, Int>): List<TaskList> = lists.sortedWith(
|
||||
compareBy<TaskList>({ it.accountName.lowercase() }, { order[it.id] ?: Int.MAX_VALUE }, { it.name.lowercase() })
|
||||
.thenBy { it.name },
|
||||
)
|
||||
}
|
||||
@@ -159,7 +159,7 @@ class ExternalImport @Inject constructor(
|
||||
val taskDao = database.tasks()
|
||||
val alarmDao = database.alarms()
|
||||
|
||||
val listIds = snapshot.lists.associate { list ->
|
||||
val listIds = snapshot.lists.withIndex().associate { (position, list) ->
|
||||
list.id to listDao.insert(
|
||||
TaskListEntity(
|
||||
name = list.name,
|
||||
@@ -168,6 +168,7 @@ class ExternalImport @Inject constructor(
|
||||
isVisible = list.isVisible,
|
||||
isSynced = false,
|
||||
owner = list.owner,
|
||||
sortOrder = position,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -26,15 +26,24 @@ class ListOrderTest {
|
||||
@Test
|
||||
fun `lists follow the stored order within their account (#136)`() {
|
||||
val lists = listOf(list(1, "Groceries"), list(2, "Home"), list(3, "Work"), list(4, "Inbox", account = "Local"))
|
||||
val order = ListOrder.reordered(emptyMap(), listOf(3, 1, 2))
|
||||
val order = ListOrder.reordered(listOf(4, 1, 2, 3), listOf(3, 1, 2))
|
||||
|
||||
assertThat(ListOrder.sorted(lists, order).map { it.id }).containsExactly(4L, 3L, 1L, 2L).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `reordering one account keeps the other's order`() {
|
||||
val order = ListOrder.reordered(mapOf(7L to 0, 8L to 1), listOf(2, 1))
|
||||
assertThat(order).isEqualTo(mapOf(7L to 0, 8L to 1, 2L to 0, 1L to 1))
|
||||
fun `reordering one account leaves every other list in its slot`() {
|
||||
// 7 and 8 are another account; 5 joined this one after the screen was drawn.
|
||||
val order = ListOrder.reordered(listOf(7, 8, 1, 5, 2), listOf(2, 1))
|
||||
|
||||
assertThat(order).isEqualTo(mapOf(7L to 0, 8L to 1, 2L to 2, 5L to 3, 1L to 4))
|
||||
assertThat(order.values.toSet()).hasSize(order.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `names sort without regard to case`() {
|
||||
val lists = listOf(list(1, "Zeta"), list(2, "apple"))
|
||||
assertThat(ListOrder.sorted(lists, emptyMap()).map { it.id }).containsExactly(2L, 1L).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user