Track each add from search on its own (#95)
This commit is contained in:
1 parent
6d88e9a228
commit
ba45aef1cd
4 files changed
+65
-8
No files matched your search
@@ -47,7 +47,7 @@ object TaskSearch {
|
||||
* [pendingTitles] are titles just added whose task has not shown up in
|
||||
* [tasks] yet; they count as taken so the same title is not offered twice.
|
||||
*/
|
||||
fun inList(tasks: List<Task>, query: String, pendingTitles: Set<String> = emptySet()): ListSearchResult {
|
||||
fun inList(tasks: List<Task>, query: String, pendingTitles: Collection<String> = emptySet()): ListSearchResult {
|
||||
val q = query.trim()
|
||||
if (q.isEmpty()) return ListSearchResult.Empty
|
||||
val key = titleKey(q)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package de.jeanlucmakiola.agendula.ui.tasklist
|
||||
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.update
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
|
||||
/** Titles added from search whose task has not shown up yet, one entry per add so two of a title stay apart. */
|
||||
internal class PendingAdds {
|
||||
private val next = AtomicLong()
|
||||
private val adds = MutableStateFlow<Map<Long, String>>(emptyMap())
|
||||
|
||||
val titles: Collection<String> get() = adds.value.values
|
||||
|
||||
val titlesFlow = adds.asStateFlow().map { it.values }
|
||||
|
||||
/** Marks [title] pending; the token ends just this add. */
|
||||
fun start(title: String): Long = next.incrementAndGet().also { token -> adds.update { it + (token to title) } }
|
||||
|
||||
fun finish(token: Long) = adds.update { it - token }
|
||||
|
||||
fun clear() { adds.value = emptyMap() }
|
||||
}
|
||||
@@ -281,7 +281,7 @@ class TaskListViewModel @Inject constructor(
|
||||
fun bind(taskFilter: TaskFilter) {
|
||||
if (filter.value != null && filter.value != taskFilter) {
|
||||
closeSearch()
|
||||
pendingAdds.value = emptySet()
|
||||
pendingAdds.clear()
|
||||
}
|
||||
filter.value = taskFilter
|
||||
}
|
||||
@@ -417,7 +417,7 @@ class TaskListViewModel @Inject constructor(
|
||||
private val query = savedState.getStateFlow(KEY_SEARCH_QUERY, "")
|
||||
|
||||
/** Titles added from search whose task has not reached [state] yet. */
|
||||
private val pendingAdds = MutableStateFlow<Set<String>>(emptySet())
|
||||
private val pendingAdds = PendingAdds()
|
||||
|
||||
private val _searchReceipt = MutableStateFlow<SearchReceipt?>(null)
|
||||
|
||||
@@ -441,7 +441,7 @@ class TaskListViewModel @Inject constructor(
|
||||
val tasks = state
|
||||
.map { st -> (st as? TaskListUiState.Content)?.takeIf { it.filter == filter.value }?.searchable.orEmpty() }
|
||||
.distinctUntilChanged { a, b -> a === b }
|
||||
combine(tasks, pendingAdds) { list, pending -> TaskSearch.inList(list, q, pending) }
|
||||
combine(tasks, pendingAdds.titlesFlow) { list, pending -> TaskSearch.inList(list, q, pending) }
|
||||
}
|
||||
}
|
||||
.flowOn(Dispatchers.Default)
|
||||
@@ -475,7 +475,7 @@ class TaskListViewModel @Inject constructor(
|
||||
* Returns false when there was nothing to do.
|
||||
*/
|
||||
fun submitSearch(query: String, listId: Long, canCreate: Boolean): Boolean {
|
||||
val result = TaskSearch.inList(searchable(), query, pendingAdds.value)
|
||||
val result = TaskSearch.inList(searchable(), query, pendingAdds.titles)
|
||||
when (val action = result.submitAction(canCreate)) {
|
||||
is SearchSubmit.Reopen -> reopenFromSearch(action.task)
|
||||
is SearchSubmit.Create -> add(action.title, listId)
|
||||
@@ -489,7 +489,7 @@ class TaskListViewModel @Inject constructor(
|
||||
|
||||
/** The "Add …" row: adds [title] to [listId] unless a task with that title turned up meanwhile. */
|
||||
fun addFromSearch(title: String, listId: Long) {
|
||||
val result = TaskSearch.inList(searchable(), title, pendingAdds.value)
|
||||
val result = TaskSearch.inList(searchable(), title, pendingAdds.titles)
|
||||
if (result.offerCreate) add(result.query, listId)
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ class TaskListViewModel @Inject constructor(
|
||||
*/
|
||||
private fun add(title: String, listId: Long) {
|
||||
if (listId <= 0L) return
|
||||
pendingAdds.update { it + title }
|
||||
val pending = pendingAdds.start(title)
|
||||
appScope.launch {
|
||||
val id = runCatching { repository.createTask(settingsPrefs.settings.first().quickTask(title, listId)) }.getOrNull()
|
||||
withContext(Dispatchers.Main) {
|
||||
@@ -510,7 +510,7 @@ class TaskListViewModel @Inject constructor(
|
||||
state.first { st -> (st as? TaskListUiState.Content)?.searchable.orEmpty().any { it.taskId == id } }
|
||||
}
|
||||
}
|
||||
pendingAdds.update { it - title }
|
||||
pendingAdds.finish(pending)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package de.jeanlucmakiola.agendula.ui.tasklist
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class PendingAddsTest {
|
||||
|
||||
@Test
|
||||
fun `two adds of one title end on their own`() {
|
||||
val pending = PendingAdds()
|
||||
val first = pending.start("Milk")
|
||||
val second = pending.start("Milk")
|
||||
|
||||
pending.finish(first)
|
||||
|
||||
assertThat(pending.titles).containsExactly("Milk")
|
||||
pending.finish(second)
|
||||
assertThat(pending.titles).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an add that ends after a clear leaves a later one pending`() {
|
||||
val pending = PendingAdds()
|
||||
val before = pending.start("Milk")
|
||||
pending.clear()
|
||||
pending.start("Milk")
|
||||
|
||||
pending.finish(before)
|
||||
|
||||
assertThat(pending.titles).containsExactly("Milk")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user