Quick-add follows the view's task list and the scope's lists, keeps what was typed when an add fails, and its receipt animates and stays with its view (#186)
This commit is contained in:
1 parent
2907f1c460
commit
1b370f2739
12 files changed
+143
-77
No files matched your search
@@ -23,6 +23,7 @@ import de.jeanlucmakiola.agendula.domain.datesForDue
|
||||
import de.jeanlucmakiola.agendula.domain.datesForTimedDue
|
||||
import de.jeanlucmakiola.agendula.domain.defaultTaskList
|
||||
import de.jeanlucmakiola.agendula.domain.dueVerdict
|
||||
import de.jeanlucmakiola.agendula.domain.listUniverse
|
||||
import de.jeanlucmakiola.agendula.domain.openTaskDated
|
||||
import de.jeanlucmakiola.agendula.domain.scopePreset
|
||||
import de.jeanlucmakiola.agendula.domain.localeWeekStart
|
||||
@@ -272,7 +273,7 @@ data class Settings(
|
||||
* A task from the quick-add bar, in a view of [scope] (`null` for none) read
|
||||
* against [clock]; `null` when there is no list to put it in. It goes to the
|
||||
* list [parse] names, else the one the scope keeps tasks to, else the default
|
||||
* list, and takes the tags, priority and, without a date of its own, the due
|
||||
* list (one the scope admits, where it can), and takes the tags, priority and, without a date of its own, the due
|
||||
* date that keep it in the scope (see [scopePreset]). [writable] are the lists that take tasks.
|
||||
*/
|
||||
fun quickAddTask(
|
||||
@@ -285,7 +286,7 @@ data class Settings(
|
||||
val preset = scope.scopePreset(clock)
|
||||
val listId = parse.list?.id?.takeIf { id -> writable.any { it.id == id } }
|
||||
?: preset.listId?.takeIf { id -> writable.any { it.id == id } }
|
||||
?: defaultTaskList(writable, defaultListId)
|
||||
?: defaultTaskList(writable, defaultListId, within = scope?.listUniverse().orEmpty())
|
||||
?: return null
|
||||
val due = parse.due
|
||||
val dates = when {
|
||||
|
||||
@@ -54,7 +54,7 @@ fun datesForDue(dueDate: LocalDate?, start: DefaultStartDate, today: LocalDate,
|
||||
|
||||
/**
|
||||
* The dates a new task due at [time] on [dueDate] opens with: the default [start]
|
||||
* goes along at that time, a start of today being [now]; never after the due.
|
||||
* goes along at that time, a start of today being [now]; never before now, nor after the due.
|
||||
*/
|
||||
fun datesForTimedDue(
|
||||
dueDate: LocalDate,
|
||||
@@ -71,7 +71,8 @@ fun datesForTimedDue(
|
||||
DefaultStartDate.TODAY -> now
|
||||
else -> start.dateFor(dueDate, today)?.let(::at)
|
||||
}
|
||||
return NewTaskDates(start = startAt?.let { minOf(it, due) }, due = due, isAllDay = false)
|
||||
// A start counted back to today at the due's time may be past already; it starts now then.
|
||||
return NewTaskDates(start = startAt?.let { minOf(maxOf(it, now), due) }, due = due, isAllDay = false)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,11 +117,15 @@ fun NewTaskDates.showsIn(
|
||||
|
||||
/**
|
||||
* The list a new task goes to when nothing names one: the default list when it
|
||||
* takes tasks and is one of [within] (any when empty), else the first of
|
||||
* [within], else the first synced list, else any. [writable] are the lists that take tasks.
|
||||
* takes tasks and is one of [within], else the first of [within], else the first
|
||||
* synced list, else any. [within] counts only when one of its lists takes tasks.
|
||||
* [writable] are the lists that take tasks.
|
||||
*/
|
||||
fun defaultTaskList(writable: List<TaskList>, defaultListId: Long?, within: Set<Long> = emptySet()): Long? =
|
||||
defaultListId?.takeIf { id -> writable.any { it.id == id } && (within.isEmpty() || id in within) }
|
||||
?: writable.firstOrNull { it.id in within }?.id
|
||||
fun defaultTaskList(writable: List<TaskList>, defaultListId: Long?, within: Set<Long> = emptySet()): Long? {
|
||||
val inside = writable.filter { it.id in within }
|
||||
val candidates = inside.ifEmpty { writable }
|
||||
return defaultListId?.takeIf { id -> candidates.any { it.id == id } }
|
||||
?: inside.firstOrNull()?.id
|
||||
?: writable.firstOrNull { !it.isLocal }?.id
|
||||
?: writable.firstOrNull()?.id
|
||||
}
|
||||
@@ -28,15 +28,15 @@ data class ScopePreset(
|
||||
)
|
||||
|
||||
/**
|
||||
* The [ScopePreset] of this scope. Only the conditions at its top, alone or
|
||||
* joined by all-of, count; one under any-of or not says nothing a task can be
|
||||
* given. The due date is the first of none, today and tomorrow that the top's
|
||||
* The [ScopePreset] of this scope. The list is the one every task in it is in
|
||||
* (see [listUniverse]); otherwise only the conditions at its top, alone or joined
|
||||
* by all-of, count, and one under any-of or not says nothing a task can be given. The due date is the first of none, today and tomorrow that the top's
|
||||
* due conditions all admit; none of them, or no due condition, leaves it empty.
|
||||
*/
|
||||
fun Rule?.scopePreset(clock: RuleClock): ScopePreset {
|
||||
val required = this?.requiredConditions().orEmpty()
|
||||
if (required.isEmpty()) return ScopePreset()
|
||||
val lists = required.filterIsInstance<Rule.InList>().map { it.listIds }.reduceOrNull { a, b -> a intersect b }
|
||||
val listId = this?.listUniverse()?.singleOrNull()
|
||||
if (required.isEmpty()) return ScopePreset(listId = listId)
|
||||
val dues = required.filterIsInstance<Rule.Due>()
|
||||
val dueDate = if (dues.isEmpty()) {
|
||||
null
|
||||
@@ -46,7 +46,7 @@ fun Rule?.scopePreset(clock: RuleClock): ScopePreset {
|
||||
?.firstOrNull { day -> dues.all { it.spec.matches(openTaskDated(null, allDayInstantOf(day), true), clock) } }
|
||||
}
|
||||
return ScopePreset(
|
||||
listId = lists?.singleOrNull(),
|
||||
listId = listId,
|
||||
tags = required.filterIsInstance<Rule.HasTag>().map { it.tag }.distinct(),
|
||||
priority = required.filterIsInstance<Rule.MinPriority>().maxOfOrNull { it.priority } ?: Priority.NONE,
|
||||
dueDate = dueDate,
|
||||
|
||||
@@ -43,6 +43,7 @@ import de.jeanlucmakiola.agendula.domain.appliedScope
|
||||
import de.jeanlucmakiola.agendula.domain.datesForTimedDue
|
||||
import de.jeanlucmakiola.agendula.domain.defaultTaskList
|
||||
import de.jeanlucmakiola.agendula.domain.dueVerdict
|
||||
import de.jeanlucmakiola.agendula.domain.listUniverse
|
||||
import de.jeanlucmakiola.agendula.domain.openTaskDated
|
||||
import de.jeanlucmakiola.agendula.domain.scopePreset
|
||||
import de.jeanlucmakiola.agendula.domain.newTaskPreset
|
||||
@@ -286,7 +287,7 @@ class TaskEditViewModel @Inject constructor(
|
||||
val writable = lists.filter { it.acceptsWrites }
|
||||
val defaultList = presetListId?.takeIf { id -> writable.any { it.id == id } }
|
||||
?: scopePreset.listId?.takeIf { id -> writable.any { it.id == id } }
|
||||
?: defaultTaskList(writable, settings.defaultListId, within = preset?.listIds.orEmpty())
|
||||
?: defaultTaskList(writable, settings.defaultListId, within = preset?.listIds?.takeIf { it.isNotEmpty() } ?: scope?.listUniverse().orEmpty())
|
||||
defaultStart = dates.start?.let { start ->
|
||||
DefaultStart(
|
||||
at = start,
|
||||
|
||||
@@ -74,7 +74,7 @@ import de.jeanlucmakiola.agendula.domain.taskFilter
|
||||
import de.jeanlucmakiola.agendula.ui.common.ActionShapes
|
||||
import de.jeanlucmakiola.agendula.ui.quickadd.NewTaskSeed
|
||||
import de.jeanlucmakiola.agendula.ui.quickadd.QuickAddBar
|
||||
import de.jeanlucmakiola.agendula.ui.quickadd.QuickAddReceiptChip
|
||||
import de.jeanlucmakiola.agendula.ui.quickadd.QuickAddReceipt
|
||||
import de.jeanlucmakiola.agendula.ui.quickadd.QuickAddViewModel
|
||||
import de.jeanlucmakiola.agendula.ui.common.SearchPill
|
||||
import de.jeanlucmakiola.agendula.ui.common.SyncPullBox
|
||||
@@ -202,7 +202,9 @@ fun ListsScreen(
|
||||
}
|
||||
val undo by homeTaskModel.undo.collectAsStateWithLifecycle()
|
||||
// The bar takes the FAB's place; with no list to add to, making one stays the FAB's.
|
||||
val quickAddShown = content?.view?.quickAdd == true && !noLists && !searchActive && !editing
|
||||
val quickAddShown = content?.view?.quickAdd == true && !noLists && quickAddLists?.isEmpty() != true && !searchActive && !editing
|
||||
// A receipt is about the view it was made in.
|
||||
LaunchedEffect(content?.view?.id) { quickAdd.dismissReceipt() }
|
||||
val scopedViewId = content?.view?.takeIf { it.scope != null }?.id
|
||||
val viewSaveFailed by viewModel.viewSaveFailed.collectAsStateWithLifecycle()
|
||||
val actions = BlockActions(
|
||||
@@ -259,8 +261,10 @@ fun ListsScreen(
|
||||
bottomBar = {
|
||||
if (quickAddShown) {
|
||||
QuickAddBar(
|
||||
lists = quickAddLists,
|
||||
onAdd = { parse -> quickAdd.add(parse, content?.scopeRule) },
|
||||
text = quickAdd.text,
|
||||
onTextChange = quickAdd::onTextChange,
|
||||
lists = quickAddLists.orEmpty(),
|
||||
onAdd = { parse -> quickAdd.add(quickAdd.text, parse, content?.scopeRule, taskFilter) },
|
||||
onExpand = { parse ->
|
||||
onNewTask(NewTaskSeed(scopedViewId, taskFilter, parse.title, parse.list?.id, parse.due))
|
||||
},
|
||||
@@ -336,34 +340,36 @@ fun ListsScreen(
|
||||
val receipt = undo
|
||||
val gone = removed
|
||||
val added = quickAddReceipt
|
||||
if (failure == null && !viewSaveFailed && gone == null && added != null) {
|
||||
QuickAddReceiptChip(added, onOpenList = { id ->
|
||||
quickAdd.dismissReceipt()
|
||||
onOpenFilter(TaskFilter.OfList(id), null)
|
||||
})
|
||||
} else {
|
||||
SnackChip(
|
||||
visible = failure != null || viewSaveFailed || gone != null || receipt != null,
|
||||
message = when {
|
||||
failure != null -> stringResource(listWriteFailureMessage(failure))
|
||||
viewSaveFailed -> stringResource(R.string.view_save_failed)
|
||||
gone != null -> stringResource(R.string.block_removed)
|
||||
receipt is UndoReceipt.Completed -> stringResource(R.string.task_completed)
|
||||
else -> stringResource(R.string.task_deleted)
|
||||
},
|
||||
actionLabel = if (failure == null && !viewSaveFailed && (gone != null || receipt != null)) {
|
||||
stringResource(R.string.undo)
|
||||
} else {
|
||||
null
|
||||
},
|
||||
onAction = when {
|
||||
failure != null || viewSaveFailed -> null
|
||||
gone != null -> viewModel::undoRemove
|
||||
receipt != null -> homeTaskModel::undo
|
||||
else -> null
|
||||
},
|
||||
)
|
||||
}
|
||||
val notShown = (added as? QuickAddReceipt.NotShown).takeIf { failure == null && !viewSaveFailed && gone == null }
|
||||
val addFailed = added is QuickAddReceipt.Failed && failure == null && !viewSaveFailed && gone == null
|
||||
SnackChip(
|
||||
visible = failure != null || viewSaveFailed || gone != null || added != null || receipt != null,
|
||||
message = when {
|
||||
failure != null -> stringResource(listWriteFailureMessage(failure))
|
||||
viewSaveFailed -> stringResource(R.string.view_save_failed)
|
||||
gone != null -> stringResource(R.string.block_removed)
|
||||
notShown != null -> stringResource(R.string.quick_add_not_shown, notShown.listName)
|
||||
addFailed -> stringResource(R.string.edit_save_failed)
|
||||
receipt is UndoReceipt.Completed -> stringResource(R.string.task_completed)
|
||||
else -> stringResource(R.string.task_deleted)
|
||||
},
|
||||
actionLabel = when {
|
||||
failure != null || viewSaveFailed || addFailed -> null
|
||||
notShown != null -> stringResource(R.string.quick_add_open)
|
||||
gone != null || receipt != null -> stringResource(R.string.undo)
|
||||
else -> null
|
||||
},
|
||||
onAction = when {
|
||||
failure != null || viewSaveFailed || addFailed -> null
|
||||
gone != null -> ({ viewModel.undoRemove() })
|
||||
notShown != null -> ({
|
||||
quickAdd.dismissReceipt()
|
||||
onOpenFilter(TaskFilter.OfList(notShown.listId), null)
|
||||
})
|
||||
receipt != null -> ({ homeTaskModel.undo() })
|
||||
else -> null
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,6 @@ import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -95,18 +94,19 @@ import java.time.LocalDate
|
||||
/**
|
||||
* The quick-add bar at the bottom of a view or list: a field reading the `@date`
|
||||
* and `+list` syntax as it is typed (see [QuickAddSyntax]), its tokens marked and
|
||||
* what they stand for shown as chips above it. [onAdd] takes what was typed;
|
||||
* [onExpand] opens it in the full editor instead. Rides above the keyboard and
|
||||
* what they stand for shown as chips above it. [onAdd] takes what was typed and
|
||||
* empties [text]; [onExpand] opens it in the full editor instead. Rides above the keyboard and
|
||||
* clears the navigation bar.
|
||||
*/
|
||||
@Composable
|
||||
fun QuickAddBar(
|
||||
text: String,
|
||||
onTextChange: (String) -> Unit,
|
||||
lists: List<TaskList>,
|
||||
onAdd: (QuickAddParse) -> Unit,
|
||||
onExpand: (QuickAddParse) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
var text by rememberSaveable { mutableStateOf("") }
|
||||
val context = rememberQuickAddContext(lists)
|
||||
val parse = remember(text, context) { QuickAddSyntax.parse(text, context) }
|
||||
Column(
|
||||
@@ -121,15 +121,12 @@ fun QuickAddBar(
|
||||
QuickAddChips(parse, context.today)
|
||||
QuickAddField(
|
||||
text = text,
|
||||
onTextChange = { text = it },
|
||||
onTextChange = onTextChange,
|
||||
canSubmit = parse.title.isNotBlank(),
|
||||
onSubmit = {
|
||||
onAdd(parse)
|
||||
text = ""
|
||||
},
|
||||
onSubmit = { onAdd(parse) },
|
||||
onExpand = {
|
||||
onExpand(parse)
|
||||
text = ""
|
||||
onTextChange("")
|
||||
},
|
||||
tokens = parse.tokens,
|
||||
elevated = true,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.jeanlucmakiola.agendula.ui.quickadd
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@@ -12,9 +13,14 @@ import de.jeanlucmakiola.agendula.domain.Rule
|
||||
import de.jeanlucmakiola.agendula.domain.RuleClock
|
||||
import de.jeanlucmakiola.agendula.domain.TaskFilter
|
||||
import de.jeanlucmakiola.agendula.domain.TaskList
|
||||
import de.jeanlucmakiola.agendula.domain.DueSpec
|
||||
import de.jeanlucmakiola.agendula.domain.asOpenTask
|
||||
import de.jeanlucmakiola.agendula.domain.dueWindows
|
||||
import de.jeanlucmakiola.agendula.domain.matches
|
||||
import de.jeanlucmakiola.floret.time.DayWindow
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.async
|
||||
@@ -52,6 +58,7 @@ sealed interface QuickAddReceipt {
|
||||
}
|
||||
|
||||
private const val RECEIPT_MS = 4_000L
|
||||
private const val KEY_TEXT = "quick_add_text"
|
||||
|
||||
/** The quick-add bar of a view or list: the lists it can name, and the adding. */
|
||||
@HiltViewModel
|
||||
@@ -59,13 +66,23 @@ class QuickAddViewModel @Inject constructor(
|
||||
private val repository: TasksRepository,
|
||||
private val settingsPrefs: SettingsPrefs,
|
||||
@ApplicationScope private val appScope: CoroutineScope,
|
||||
private val savedState: SavedStateHandle,
|
||||
) : ViewModel() {
|
||||
|
||||
/** The lists a task can be added to. */
|
||||
val lists: StateFlow<List<TaskList>> = repository.taskLists()
|
||||
/** The lists a task can be added to; `null` until read. */
|
||||
val lists: StateFlow<List<TaskList>?> = repository.taskLists()
|
||||
.map { lists -> lists.filter { it.acceptsWrites } }
|
||||
.catch { emit(emptyList()) }
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null)
|
||||
|
||||
/** What is typed in the bar, kept here so a failed add can give it back. */
|
||||
var text by mutableStateOf(savedState.get<String>(KEY_TEXT).orEmpty())
|
||||
private set
|
||||
|
||||
fun onTextChange(value: String) {
|
||||
text = value
|
||||
savedState[KEY_TEXT] = value
|
||||
}
|
||||
|
||||
private val _receipt = MutableStateFlow<QuickAddReceipt?>(null)
|
||||
|
||||
@@ -74,12 +91,16 @@ class QuickAddViewModel @Inject constructor(
|
||||
private var receiptTimer: Job? = null
|
||||
|
||||
/**
|
||||
* Adds what [parse] holds within [scope] (`null` for none); says so when the
|
||||
* task falls outside it. The write runs in the application scope, so leaving
|
||||
* the screen right after cannot cancel it.
|
||||
* Adds what [parse], read from [typed], holds within [scope] (`null` for none);
|
||||
* says so when the task falls outside it. [preset] is what the view's task list
|
||||
* shows, which gives the task a list, tags and a due date as its own new-task
|
||||
* action does, without narrowing what counts as shown. The write runs in the
|
||||
* application scope, so leaving the screen right after cannot cancel it; when
|
||||
* it fails, an empty bar gets [typed] back.
|
||||
*/
|
||||
fun add(parse: QuickAddParse, scope: Rule?) {
|
||||
fun add(typed: String, parse: QuickAddParse, scope: Rule?, preset: TaskFilter? = null) {
|
||||
if (parse.title.isBlank()) return
|
||||
onTextChange("")
|
||||
val write = appScope.async {
|
||||
runCatching {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
@@ -87,8 +108,9 @@ class QuickAddViewModel @Inject constructor(
|
||||
val now = Clock.System.now()
|
||||
val zone = ZoneId.systemDefault()
|
||||
val (todayStart, todayEnd) = DayWindow.today(now, zone)
|
||||
val clock = RuleClock(todayStart, todayEnd, zone, settings.effectiveWeekStart)
|
||||
val form = settings.quickAddTask(parse, scope, writable, clock, now) ?: return@runCatching QuickAddReceipt.Failed()
|
||||
val clock = RuleClock(todayStart, todayEnd, zone, settingsPrefs.firstDayOfWeek.first())
|
||||
val solved = listOfNotNull(scope, presetRule(preset)).let { if (it.size > 1) Rule.AllOf(it) else it.singleOrNull() }
|
||||
val form = settings.quickAddTask(parse, solved, writable, clock, now) ?: return@runCatching QuickAddReceipt.Failed()
|
||||
repository.createTask(form)
|
||||
if (scope == null || scope.matches(form.asOpenTask(), clock)) {
|
||||
null
|
||||
@@ -97,7 +119,21 @@ class QuickAddViewModel @Inject constructor(
|
||||
}
|
||||
}.getOrElse { QuickAddReceipt.Failed() }
|
||||
}
|
||||
viewModelScope.launch { write.await()?.let(::show) }
|
||||
viewModelScope.launch {
|
||||
val receipt = write.await() ?: return@launch
|
||||
if (receipt is QuickAddReceipt.Failed && text.isEmpty()) onTextChange(typed)
|
||||
show(receipt)
|
||||
}
|
||||
}
|
||||
|
||||
/** The rule a task shown by [filter] meets, as far as a new task can be made to. */
|
||||
private suspend fun presetRule(filter: TaskFilter?): Rule? = when (filter) {
|
||||
is TaskFilter.OfList -> Rule.InList(setOf(filter.listId))
|
||||
is TaskFilter.Tagged -> Rule.HasTag(filter.tag)
|
||||
is TaskFilter.Saved -> settingsPrefs.savedFilter(filter.id).first()?.rule
|
||||
is TaskFilter.Matching -> filter.rule
|
||||
is TaskFilter.Smart -> filter.list.dueWindows.singleOrNull()?.let { Rule.Due(DueSpec.Window(it)) }
|
||||
is TaskFilter.DueOn, is TaskFilter.OfLists, null -> null
|
||||
}
|
||||
|
||||
private fun show(receipt: QuickAddReceipt) {
|
||||
|
||||
@@ -335,8 +335,10 @@ fun TaskListScreen(
|
||||
// The list and the view's scope it is narrowed to, both of which a new task should stay in.
|
||||
val scope = Rule.AllOf(listOfNotNull(Rule.InList(setOf(listId)), content?.appliedScope?.rule))
|
||||
QuickAddBar(
|
||||
lists = quickAddLists,
|
||||
onAdd = { parse -> quickAdd.add(parse, scope) },
|
||||
text = quickAdd.text,
|
||||
onTextChange = quickAdd::onTextChange,
|
||||
lists = quickAddLists.orEmpty(),
|
||||
onAdd = { parse -> quickAdd.add(quickAdd.text, parse, scope) },
|
||||
onExpand = { parse ->
|
||||
onNewTask(
|
||||
NewTaskSeed(
|
||||
@@ -418,9 +420,10 @@ fun TaskListScreen(
|
||||
visible = true,
|
||||
message = stringResource(listWriteFailureMessage(failure)),
|
||||
)
|
||||
// This list already: what keeps the task out of sight is the view's scope, so that goes.
|
||||
added != null -> QuickAddReceiptChip(added, onOpenList = { id ->
|
||||
quickAdd.dismissReceipt()
|
||||
onOpenList(id)
|
||||
if (id == listId) viewModel.dropScope() else onOpenList(id)
|
||||
})
|
||||
searchReceipt != null -> SnackChip(
|
||||
visible = true,
|
||||
|
||||
@@ -147,7 +147,7 @@ fun EditViewScreen(
|
||||
checked = view.quickAdd,
|
||||
onCheckedChange = viewModel::setQuickAdd,
|
||||
position = if (isNew) Position.Alone else Position.Bottom,
|
||||
summary = quickAddTarget?.let { stringResource(R.string.view_quick_add_target, it) }
|
||||
summary = quickAddTarget?.takeIf { view.quickAdd }?.let { stringResource(R.string.view_quick_add_target, it) }
|
||||
?: stringResource(R.string.view_quick_add_hint),
|
||||
leading = { Icon(Icons.Rounded.PostAdd, contentDescription = null) },
|
||||
)
|
||||
|
||||
@@ -11,6 +11,7 @@ import de.jeanlucmakiola.agendula.domain.ViewIcon
|
||||
import de.jeanlucmakiola.agendula.domain.RuleClock
|
||||
import de.jeanlucmakiola.agendula.domain.appliedScope
|
||||
import de.jeanlucmakiola.agendula.domain.defaultTaskList
|
||||
import de.jeanlucmakiola.agendula.domain.listUniverse
|
||||
import de.jeanlucmakiola.agendula.domain.newView
|
||||
import de.jeanlucmakiola.agendula.domain.scopePreset
|
||||
import de.jeanlucmakiola.floret.time.DayWindow
|
||||
@@ -83,16 +84,17 @@ class EditViewViewModel @Inject constructor(
|
||||
_draft,
|
||||
repository.taskLists().catch { emit(emptyList()) },
|
||||
settingsPrefs.settings,
|
||||
) { draft, lists, settings ->
|
||||
settingsPrefs.firstDayOfWeek,
|
||||
) { draft, lists, settings, weekStart ->
|
||||
if (draft == null) return@combine null
|
||||
val writable = lists.filter { it.acceptsWrites }
|
||||
val now = Clock.System.now()
|
||||
val zone = ZoneId.systemDefault()
|
||||
val (todayStart, todayEnd) = DayWindow.today(now, zone)
|
||||
val clock = RuleClock(todayStart, todayEnd, zone, settings.effectiveWeekStart)
|
||||
val clock = RuleClock(todayStart, todayEnd, zone, weekStart)
|
||||
val scope = draft.appliedScope(lists.mapTo(HashSet()) { it.id }).rule
|
||||
val id = scope.scopePreset(clock).listId?.takeIf { id -> writable.any { it.id == id } }
|
||||
?: defaultTaskList(writable, settings.defaultListId)
|
||||
?: defaultTaskList(writable, settings.defaultListId, within = scope?.listUniverse().orEmpty())
|
||||
writable.firstOrNull { it.id == id }?.name
|
||||
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null)
|
||||
|
||||
|
||||
@@ -79,7 +79,8 @@ class QuickAddTaskTest {
|
||||
@Test
|
||||
fun `the scope's one list comes before the default`() {
|
||||
assertThat(add(QuickAddParse("x"), scope = Rule.InList(setOf(1L)))!!.listId).isEqualTo(1L)
|
||||
assertThat(add(QuickAddParse("x"), scope = Rule.InList(setOf(1L, 3L)))!!.listId).isEqualTo(2L)
|
||||
assertThat(add(QuickAddParse("x"), scope = Rule.InList(setOf(1L, 3L)))!!.listId).isEqualTo(1L)
|
||||
assertThat(add(QuickAddParse("x"), scope = Rule.InList(setOf(2L, 3L)))!!.listId).isEqualTo(2L)
|
||||
// A scope list the task can't go to isn't one.
|
||||
assertThat(add(QuickAddParse("x"), scope = Rule.InList(setOf(9L)))!!.listId).isEqualTo(2L)
|
||||
}
|
||||
@@ -156,6 +157,13 @@ class QuickAddTaskTest {
|
||||
assertThat(form.validate()).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a start counted back to today at a time already past starts now`() {
|
||||
val weekBefore = Settings(defaultListId = 2, defaultStartDate = DefaultStartDate.WEEK_BEFORE_DUE)
|
||||
val form = add(QuickAddParse("x", due = QuickDue(today.plusDays(3), LocalTime.of(9, 0))), settings = weekBefore)!!
|
||||
assertThat(form.start).isEqualTo(now)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a task the scope won't show is told apart`() {
|
||||
val scope = Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.Due(DueSpec.Window(DueWindow.TODAY))))
|
||||
|
||||
@@ -51,11 +51,18 @@ class ScopePresetTest {
|
||||
fun `nothing under any-of or not is solved for`() {
|
||||
assertThat(any(Rule.InList(setOf(1L)), Rule.HasTag("x")).scopePreset(clock)).isEqualTo(ScopePreset())
|
||||
assertThat(Rule.Not(Rule.InList(setOf(1L))).scopePreset(clock)).isEqualTo(ScopePreset())
|
||||
assertThat(any(Rule.InList(setOf(1L)), Rule.InList(setOf(1L))).scopePreset(clock)).isEqualTo(ScopePreset())
|
||||
assertThat(any(Rule.InList(setOf(1L)), Rule.InList(setOf(2L))).scopePreset(clock)).isEqualTo(ScopePreset())
|
||||
val mixed = all(Rule.InList(setOf(1L)), any(Rule.HasTag("a"), Rule.HasTag("b")), Rule.Not(Rule.HasTag("c")))
|
||||
assertThat(mixed.scopePreset(clock)).isEqualTo(ScopePreset(listId = 1L))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the list is the one every task in the scope is in, whatever joins the conditions`() {
|
||||
val either = any(all(Rule.InList(setOf(1L)), Rule.HasTag("x")), all(Rule.InList(setOf(1L)), Rule.MinPriority(Priority.HIGH)))
|
||||
assertThat(either.scopePreset(clock)).isEqualTo(ScopePreset(listId = 1L))
|
||||
assertThat(all(Rule.InList(setOf(1L)), Rule.Not(Rule.InList(setOf(2L)))).scopePreset(clock).listId).isEqualTo(1L)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the due date is the first of none, today and tomorrow the scope admits`() {
|
||||
assertThat(due(DueWindow.TODAY).scopePreset(clock).dueDate).isEqualTo(today)
|
||||
|
||||
Reference in new issue
Block a user