Quick-add bar on every view and list, with live tokens, scope presets and a receipt when the task lands elsewhere (#59, #186)
This commit is contained in:
1 parent
3c5ed514df
commit
3c271e8ff3
15 files changed
+632
-174
No files matched your search
@@ -15,13 +15,6 @@ fun Rule.limitingListIds(): Set<Long> = when (this) {
|
||||
is Rule.Not, is Rule.HasTag, is Rule.MinPriority, is Rule.Due -> emptySet()
|
||||
}
|
||||
|
||||
/** A tag every task passing this rule carries: one at the top, alone or among all-of conditions. */
|
||||
fun Rule.requiredTag(): String? = when (this) {
|
||||
is Rule.HasTag -> tag
|
||||
is Rule.AllOf -> children.firstNotNullOfOrNull { (it as? Rule.HasTag)?.tag }
|
||||
else -> null
|
||||
}
|
||||
|
||||
/**
|
||||
* What a new task gets so that a scope admits it, read from the conditions every
|
||||
* task in it meets: the [listId] when they leave one list, their [tags], the
|
||||
|
||||
@@ -54,6 +54,9 @@ object ActionShapes {
|
||||
/** Throw away a layout's edits — a clover, apart from the softer ✓ beside it. */
|
||||
val DiscardLayout: RoundedPolygon get() = MaterialShapes.Clover8Leaf
|
||||
|
||||
/** Open what the quick-add bar holds in the full editor — a gem, apart from the round ✓ beside it. */
|
||||
val ExpandQuickAdd: RoundedPolygon get() = MaterialShapes.Gem
|
||||
|
||||
/** Take a block out of a layout — a soft burst, apart from [Delete], since it can be undone. */
|
||||
val RemoveBlock: RoundedPolygon get() = MaterialShapes.SoftBurst
|
||||
}
|
||||
@@ -72,6 +72,10 @@ import de.jeanlucmakiola.agendula.domain.View
|
||||
import de.jeanlucmakiola.agendula.domain.rows
|
||||
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.QuickAddViewModel
|
||||
import de.jeanlucmakiola.agendula.ui.common.SearchPill
|
||||
import de.jeanlucmakiola.agendula.ui.common.SyncPullBox
|
||||
import de.jeanlucmakiola.agendula.ui.tasklist.ClearCompletedDialog
|
||||
@@ -108,7 +112,7 @@ import kotlinx.coroutines.delay
|
||||
fun ListsScreen(
|
||||
onOpenFilter: (TaskFilter, Long?) -> Unit,
|
||||
onOpenTask: (Task) -> Unit,
|
||||
onNewTask: (listId: Long?, tag: String?, preset: TaskFilter?) -> Unit,
|
||||
onNewTask: (NewTaskSeed) -> Unit,
|
||||
onOpenSettings: () -> Unit,
|
||||
onManageViews: () -> Unit,
|
||||
onEditView: (Long) -> Unit,
|
||||
@@ -119,8 +123,11 @@ fun ListsScreen(
|
||||
rulesResult: BlockSource? = null,
|
||||
onRulesResultTaken: () -> Unit = {},
|
||||
viewModel: ListsViewModel = hiltViewModel(),
|
||||
quickAdd: QuickAddViewModel = hiltViewModel(),
|
||||
) {
|
||||
val state by viewModel.state.collectAsStateWithLifecycle()
|
||||
val quickAddLists by quickAdd.lists.collectAsStateWithLifecycle()
|
||||
val quickAddReceipt by quickAdd.receipt.collectAsStateWithLifecycle()
|
||||
val syncAvailable by viewModel.syncAvailable.collectAsStateWithLifecycle()
|
||||
val syncing by viewModel.syncing.collectAsStateWithLifecycle()
|
||||
var query by rememberSaveable { mutableStateOf("") }
|
||||
@@ -194,6 +201,9 @@ fun ListsScreen(
|
||||
null
|
||||
}
|
||||
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 scopedViewId = content?.view?.takeIf { it.scope != null }?.id
|
||||
val viewSaveFailed by viewModel.viewSaveFailed.collectAsStateWithLifecycle()
|
||||
val actions = BlockActions(
|
||||
openFilter = onOpenFilter,
|
||||
@@ -235,9 +245,10 @@ fun ListsScreen(
|
||||
},
|
||||
floatingActionButton = {
|
||||
// The FAB would otherwise float over the search results, or the layout being edited.
|
||||
if (!searchActive && !editing) {
|
||||
// Not while loading either, which would flash it before the bar replaces it.
|
||||
if (!searchActive && !editing && !quickAddShown && state !is ListsUiState.Loading) {
|
||||
ExtendedFloatingActionButton(
|
||||
onClick = if (noLists) ({ newList = true }) else ({ onNewTask(content?.newTaskList, content?.newTaskTag, taskFilter) }),
|
||||
onClick = if (noLists) ({ newList = true }) else ({ onNewTask(NewTaskSeed(scopedViewId, taskFilter)) }),
|
||||
icon = { Icon(Icons.Rounded.Add, contentDescription = null) },
|
||||
text = {
|
||||
Text(stringResource(if (noLists) R.string.list_add else R.string.new_task))
|
||||
@@ -245,6 +256,17 @@ fun ListsScreen(
|
||||
)
|
||||
}
|
||||
},
|
||||
bottomBar = {
|
||||
if (quickAddShown) {
|
||||
QuickAddBar(
|
||||
lists = quickAddLists,
|
||||
onAdd = { parse -> quickAdd.add(parse, content?.scopeRule) },
|
||||
onExpand = { parse ->
|
||||
onNewTask(NewTaskSeed(scopedViewId, taskFilter, parse.title, parse.list?.id, parse.due))
|
||||
},
|
||||
)
|
||||
}
|
||||
},
|
||||
) { inner ->
|
||||
SyncPullBox(
|
||||
available = syncAvailable && !searchActive && !editing,
|
||||
@@ -276,7 +298,7 @@ fun ListsScreen(
|
||||
taskBlock = blockModel?.let { model ->
|
||||
taskBlock?.let { block -> TaskBlock(block, taskFilter!!, scopeFor(block), model) }
|
||||
},
|
||||
bottomPadding = inner.calculateBottomPadding() + 96.dp,
|
||||
bottomPadding = inner.calculateBottomPadding() + if (quickAddShown) 16.dp else 96.dp,
|
||||
)
|
||||
// While searching with a non-blank query, results cover the home
|
||||
// content; an empty query leaves the home content visible behind
|
||||
@@ -309,31 +331,39 @@ fun ListsScreen(
|
||||
.height(SnackChipHeight),
|
||||
contentAlignment = Alignment.CenterStart,
|
||||
) {
|
||||
// One chip: a refused write, else a removed block's undo, else the task block's undo.
|
||||
// One chip: a refused write, else a removed block's undo, else a quick add, else the task block's undo.
|
||||
val failure = writeFailure
|
||||
val receipt = undo
|
||||
val gone = removed
|
||||
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 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
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import de.jeanlucmakiola.agendula.domain.restored
|
||||
import de.jeanlucmakiola.agendula.domain.weekDays
|
||||
import de.jeanlucmakiola.agendula.domain.withBlock
|
||||
import de.jeanlucmakiola.agendula.domain.without
|
||||
import de.jeanlucmakiola.agendula.domain.Rule
|
||||
import de.jeanlucmakiola.agendula.domain.RuleClock
|
||||
import de.jeanlucmakiola.agendula.domain.SavedFilter
|
||||
import de.jeanlucmakiola.agendula.domain.SmartList
|
||||
@@ -39,7 +40,6 @@ import de.jeanlucmakiola.agendula.domain.contains
|
||||
import de.jeanlucmakiola.agendula.domain.counts
|
||||
import de.jeanlucmakiola.agendula.domain.forBlock
|
||||
import de.jeanlucmakiola.agendula.domain.isGone
|
||||
import de.jeanlucmakiola.agendula.domain.requiredTag
|
||||
import de.jeanlucmakiola.agendula.domain.tagsInUse
|
||||
import de.jeanlucmakiola.agendula.ui.views.ViewSession
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -184,10 +184,8 @@ sealed interface ListsUiState {
|
||||
val allTasks: List<Task> = emptyList(),
|
||||
/** [allTasks] within the view's scope, where a search in a scoped view starts; filtered once searched. */
|
||||
private val scopedCorpus: Lazy<List<Task>> = lazyOf(allTasks),
|
||||
/** The list a new task goes to, so it shows in the view: the scope's one list, if it keeps tasks to one. */
|
||||
val newTaskList: Long? = null,
|
||||
/** A tag the scope requires, which a new task gets so it stays in the view. */
|
||||
val newTaskTag: String? = null,
|
||||
/** The view's scope as it applies, lists since deleted matching nothing; `null` for none. */
|
||||
val scopeRule: Rule? = null,
|
||||
) : ListsUiState {
|
||||
val sections: HomeSections get() = scopedSections.value
|
||||
val unscopedSections: HomeSections get() = everySection.value
|
||||
@@ -300,8 +298,7 @@ internal fun homeContent(
|
||||
} else {
|
||||
lazy(LazyThreadSafetyMode.PUBLICATION) { allTasks.filter { scope.admits(it, clock) } }
|
||||
},
|
||||
newTaskList = scope.lists?.singleOrNull()?.takeIf { it in listIds },
|
||||
newTaskTag = scope.rule?.requiredTag(),
|
||||
scopeRule = scope.rule,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ import de.jeanlucmakiola.agendula.ui.filters.RuleEditViewModel
|
||||
import de.jeanlucmakiola.agendula.ui.filters.RulePurpose
|
||||
import de.jeanlucmakiola.agendula.ui.imports.ImportScreen
|
||||
import de.jeanlucmakiola.agendula.ui.lists.ListsScreen
|
||||
import de.jeanlucmakiola.agendula.ui.quickadd.NewTaskSeed
|
||||
import de.jeanlucmakiola.agendula.ui.settings.SettingsScreen
|
||||
import de.jeanlucmakiola.agendula.ui.tasklist.TaskListScreen
|
||||
import de.jeanlucmakiola.agendula.ui.tasklist.TaskListViewModel
|
||||
@@ -148,18 +149,8 @@ fun AgendulaNavHost(
|
||||
ListsScreen(
|
||||
onOpenFilter = { filter, scopeViewId -> nav.navigate(Dest.TaskList.build(filter, scopeViewId)) },
|
||||
onOpenTask = { task -> nav.navigate(task.detailRoute()) },
|
||||
// The view's scope picks the list and tag; its task list block's source adds its own presets.
|
||||
onNewTask = { listId, tag, preset ->
|
||||
nav.navigate(
|
||||
Dest.TaskEdit.buildNew(
|
||||
presetListId = listId ?: (preset as? TaskFilter.OfList)?.listId,
|
||||
tag = tag ?: (preset as? TaskFilter.Tagged)?.tag,
|
||||
filterId = (preset as? TaskFilter.Saved)?.id,
|
||||
smartList = (preset as? TaskFilter.Smart)?.list,
|
||||
rule = (preset as? TaskFilter.Matching)?.rule,
|
||||
),
|
||||
)
|
||||
},
|
||||
// The view's scope presets the task; its task list block's source adds its own presets.
|
||||
onNewTask = { seed -> nav.navigate(seed.route()) },
|
||||
onNewFilter = { nav.navigate(Dest.FilterEdit.build()) },
|
||||
onOpenSettings = { nav.navigate(Dest.SETTINGS) },
|
||||
onManageViews = { nav.navigate(Dest.MANAGE_VIEWS) },
|
||||
@@ -271,19 +262,9 @@ fun AgendulaNavHost(
|
||||
filter = filter,
|
||||
viewModel = vm,
|
||||
onOpenTask = onOpenTask,
|
||||
onNewTask = {
|
||||
nav.navigate(
|
||||
Dest.TaskEdit.buildNew(
|
||||
presetListId = (filter as? TaskFilter.OfList)?.listId,
|
||||
tag = (filter as? TaskFilter.Tagged)?.tag,
|
||||
filterId = (filter as? TaskFilter.Saved)?.id,
|
||||
smartList = (filter as? TaskFilter.Smart)?.list,
|
||||
due = (filter as? TaskFilter.DueOn)?.date,
|
||||
rule = (filter as? TaskFilter.Matching)?.rule,
|
||||
),
|
||||
)
|
||||
},
|
||||
onNewTask = { seed -> nav.navigate(seed.copy(preset = filter).route()) },
|
||||
onEditFilter = { id -> nav.navigate(Dest.FilterEdit.build(id)) },
|
||||
onOpenList = { id -> nav.navigate(Dest.TaskList.build(TaskFilter.OfList(id))) },
|
||||
// The new view shows on home, so the screen it was pinned from goes.
|
||||
// Only while this screen is still the one shown; the user may have moved on.
|
||||
onPinned = {
|
||||
@@ -360,6 +341,8 @@ fun AgendulaNavHost(
|
||||
val smartList = smartListNamed(args?.getString(Dest.TaskEdit.ARG_SMART_LIST))
|
||||
val presetRule = args?.getString(Dest.TaskEdit.ARG_RULE)?.let { runCatching { Dest.TaskList.ruleOf(it) }.getOrNull() }
|
||||
val presetDue = args?.getString(Dest.TaskEdit.ARG_DUE)?.let { runCatching { java.time.LocalDate.parse(it) }.getOrNull() }
|
||||
val presetDueTime = args?.getString(Dest.TaskEdit.ARG_DUE_TIME)?.let { runCatching { java.time.LocalTime.parse(it) }.getOrNull() }
|
||||
val scopeViewId = args?.getLong(Dest.TaskEdit.ARG_VIEW)?.takeIf { it != Dest.NO_ID }
|
||||
val vm: TaskEditViewModel = hiltViewModel()
|
||||
LaunchedEffect(taskId, presetListId, parentId) {
|
||||
if (taskId != Dest.NO_ID) {
|
||||
@@ -374,6 +357,8 @@ fun AgendulaNavHost(
|
||||
presetSmartList = smartList,
|
||||
presetDueDate = presetDue,
|
||||
presetRule = presetRule,
|
||||
scopeViewId = scopeViewId,
|
||||
presetDueTime = presetDueTime,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -389,6 +374,19 @@ fun AgendulaNavHost(
|
||||
}
|
||||
}
|
||||
|
||||
/** The editor for a new task as [NewTaskSeed] has it; what it was typed into presets it too. */
|
||||
private fun NewTaskSeed.route(): String = Dest.TaskEdit.buildNew(
|
||||
presetListId = listId ?: (preset as? TaskFilter.OfList)?.listId,
|
||||
title = title,
|
||||
tag = (preset as? TaskFilter.Tagged)?.tag,
|
||||
filterId = (preset as? TaskFilter.Saved)?.id,
|
||||
smartList = (preset as? TaskFilter.Smart)?.list,
|
||||
due = due?.date ?: (preset as? TaskFilter.DueOn)?.date,
|
||||
rule = (preset as? TaskFilter.Matching)?.rule,
|
||||
scopeViewId = scopeViewId,
|
||||
dueTime = due?.time,
|
||||
)
|
||||
|
||||
/** Routes holding a draft that leaving would lose. */
|
||||
private val FORMS = setOf(Dest.TaskEdit.route, Dest.FilterEdit.route, Dest.EditView.route, Dest.RuleEdit.route)
|
||||
|
||||
|
||||
@@ -0,0 +1,350 @@
|
||||
package de.jeanlucmakiola.agendula.ui.quickadd
|
||||
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.EnterTransition
|
||||
import androidx.compose.animation.ExitTransition
|
||||
import androidx.compose.animation.expandVertically
|
||||
import androidx.compose.animation.fadeIn
|
||||
import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.animation.shrinkVertically
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.FlowRow
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.WindowInsets
|
||||
import androidx.compose.foundation.layout.WindowInsetsSides
|
||||
import androidx.compose.foundation.layout.displayCutout
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.ime
|
||||
import androidx.compose.foundation.layout.isImeVisible
|
||||
import androidx.compose.foundation.layout.navigationBars
|
||||
import androidx.compose.foundation.layout.only
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.union
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.layout.windowInsetsPadding
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Add
|
||||
import androidx.compose.material.icons.rounded.Check
|
||||
import androidx.compose.material.icons.rounded.Event
|
||||
import androidx.compose.material.icons.rounded.OpenInFull
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
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
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.LiveRegionMode
|
||||
import androidx.compose.ui.semantics.clearAndSetSemantics
|
||||
import androidx.compose.ui.semantics.contentDescription
|
||||
import androidx.compose.ui.semantics.liveRegion
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import androidx.compose.ui.text.SpanStyle
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.OffsetMapping
|
||||
import androidx.compose.ui.text.input.TransformedText
|
||||
import androidx.compose.ui.text.input.VisualTransformation
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.jeanlucmakiola.agendula.R
|
||||
import de.jeanlucmakiola.agendula.domain.QuickAddContext
|
||||
import de.jeanlucmakiola.agendula.domain.QuickAddKeywords
|
||||
import de.jeanlucmakiola.agendula.domain.QuickAddParse
|
||||
import de.jeanlucmakiola.agendula.domain.QuickAddSyntax
|
||||
import de.jeanlucmakiola.agendula.domain.QuickDue
|
||||
import de.jeanlucmakiola.agendula.domain.QuickToken
|
||||
import de.jeanlucmakiola.agendula.domain.ShortDateFormat
|
||||
import de.jeanlucmakiola.agendula.domain.TaskList
|
||||
import de.jeanlucmakiola.agendula.ui.common.ActionShapes
|
||||
import de.jeanlucmakiola.agendula.ui.common.LocalFirstDayOfWeek
|
||||
import de.jeanlucmakiola.agendula.ui.common.LocalUse24HourFormat
|
||||
import de.jeanlucmakiola.agendula.ui.common.formatMinuteOfDay
|
||||
import de.jeanlucmakiola.agendula.ui.common.rememberDayFormat
|
||||
import de.jeanlucmakiola.floret.components.ActionButton
|
||||
import de.jeanlucmakiola.floret.components.InlineTextField
|
||||
import de.jeanlucmakiola.floret.components.SnackChip
|
||||
import de.jeanlucmakiola.floret.components.pastelize
|
||||
import de.jeanlucmakiola.floret.identity.FloretDensity
|
||||
import de.jeanlucmakiola.floret.identity.byDensity
|
||||
import de.jeanlucmakiola.floret.identity.rememberReduceMotion
|
||||
import de.jeanlucmakiola.floret.locale.currentLocale
|
||||
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
|
||||
* clears the navigation bar.
|
||||
*/
|
||||
@Composable
|
||||
fun QuickAddBar(
|
||||
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(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.windowInsetsPadding(
|
||||
WindowInsets.ime
|
||||
.union(WindowInsets.navigationBars)
|
||||
.union(WindowInsets.displayCutout.only(WindowInsetsSides.Horizontal)),
|
||||
),
|
||||
) {
|
||||
QuickAddChips(parse, context.today)
|
||||
QuickAddField(
|
||||
text = text,
|
||||
onTextChange = { text = it },
|
||||
canSubmit = parse.title.isNotBlank(),
|
||||
onSubmit = {
|
||||
onAdd(parse)
|
||||
text = ""
|
||||
},
|
||||
onExpand = {
|
||||
onExpand(parse)
|
||||
text = ""
|
||||
},
|
||||
tokens = parse.tokens,
|
||||
elevated = true,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** What [QuickAddSyntax] reads against here: today, the week start, the locale's words and date form, and [lists]. */
|
||||
@Composable
|
||||
private fun rememberQuickAddContext(lists: List<TaskList>): QuickAddContext {
|
||||
val locale = currentLocale()
|
||||
val resources = LocalContext.current.resources
|
||||
val weekStart = LocalFirstDayOfWeek.current
|
||||
val keywords = remember(locale, resources) {
|
||||
QuickAddKeywords.English + QuickAddKeywords.fromTranslations { name -> KEYWORD_RESOURCES[name]?.let(resources::getString) }
|
||||
}
|
||||
val shortDate = remember(locale) { ShortDateFormat.of(locale) }
|
||||
val today = LocalDate.now()
|
||||
return remember(today, weekStart, keywords, shortDate, lists) { QuickAddContext(today, weekStart, keywords, shortDate, lists) }
|
||||
}
|
||||
|
||||
private val KEYWORD_RESOURCES: Map<String, Int> = mapOf(
|
||||
QuickAddKeywords.TODAY to R.string.quick_add_keyword_today,
|
||||
QuickAddKeywords.TOMORROW to R.string.quick_add_keyword_tomorrow,
|
||||
QuickAddKeywords.NEXT_WEEK to R.string.quick_add_keyword_next_week,
|
||||
QuickAddKeywords.weekdayName(java.time.DayOfWeek.MONDAY) to R.string.quick_add_keyword_monday,
|
||||
QuickAddKeywords.weekdayName(java.time.DayOfWeek.TUESDAY) to R.string.quick_add_keyword_tuesday,
|
||||
QuickAddKeywords.weekdayName(java.time.DayOfWeek.WEDNESDAY) to R.string.quick_add_keyword_wednesday,
|
||||
QuickAddKeywords.weekdayName(java.time.DayOfWeek.THURSDAY) to R.string.quick_add_keyword_thursday,
|
||||
QuickAddKeywords.weekdayName(java.time.DayOfWeek.FRIDAY) to R.string.quick_add_keyword_friday,
|
||||
QuickAddKeywords.weekdayName(java.time.DayOfWeek.SATURDAY) to R.string.quick_add_keyword_saturday,
|
||||
QuickAddKeywords.weekdayName(java.time.DayOfWeek.SUNDAY) to R.string.quick_add_keyword_sunday,
|
||||
QuickAddKeywords.DAYS to R.string.quick_add_keyword_days,
|
||||
QuickAddKeywords.WEEKS to R.string.quick_add_keyword_weeks,
|
||||
QuickAddKeywords.MONTHS to R.string.quick_add_keyword_months,
|
||||
)
|
||||
|
||||
/** The due day and list [parse] found, as chips; announced as they change. */
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
private fun QuickAddChips(parse: QuickAddParse, today: LocalDate) {
|
||||
val due = parse.due
|
||||
val list = parse.list
|
||||
val reduceMotion = rememberReduceMotion()
|
||||
AnimatedVisibility(
|
||||
visible = due != null || list != null,
|
||||
enter = if (reduceMotion) EnterTransition.None else fadeIn() + expandVertically(),
|
||||
exit = if (reduceMotion) ExitTransition.None else fadeOut() + shrinkVertically(),
|
||||
) {
|
||||
FlowRow(
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp)
|
||||
.padding(top = 8.dp)
|
||||
.semantics { liveRegion = LiveRegionMode.Polite },
|
||||
) {
|
||||
if (due != null) {
|
||||
val label = dueLabel(due, today)
|
||||
QuickChip(
|
||||
label = label,
|
||||
description = stringResource(R.string.quick_add_due_chip, label),
|
||||
leading = { Icon(Icons.Rounded.Event, contentDescription = null, modifier = Modifier.size(18.dp)) },
|
||||
)
|
||||
}
|
||||
if (list != null) {
|
||||
val dot = pastelize(list.color, isSystemInDarkTheme())
|
||||
QuickChip(
|
||||
label = list.name,
|
||||
description = stringResource(R.string.quick_add_list_chip, list.name),
|
||||
leading = { Box(Modifier.size(10.dp).background(dot, CircleShape)) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun dueLabel(due: QuickDue, today: LocalDate): String {
|
||||
val day = rememberDayFormat().format(due.date, today)
|
||||
val time = due.time ?: return day
|
||||
return "$day · ${formatMinuteOfDay(time.hour * 60 + time.minute, LocalUse24HourFormat.current, currentLocale())}"
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun QuickChip(label: String, description: String, leading: @Composable () -> Unit) {
|
||||
Surface(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
color = MaterialTheme.colorScheme.secondaryContainer,
|
||||
contentColor = MaterialTheme.colorScheme.onSecondaryContainer,
|
||||
modifier = Modifier.clearAndSetSemantics { contentDescription = description },
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.heightIn(min = 32.dp).padding(horizontal = 8.dp),
|
||||
) {
|
||||
leading()
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Text(label, style = MaterialTheme.typography.labelLarge, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The rounded quick-add field: a title, [tokens] in it marked. [onExpand], when
|
||||
* set, adds a button to open the full editor. Shared with the onboarding step
|
||||
* that previews the bottom bar.
|
||||
*/
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
internal fun QuickAddField(
|
||||
text: String,
|
||||
onTextChange: (String) -> Unit,
|
||||
canSubmit: Boolean,
|
||||
onSubmit: () -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
onExpand: (() -> Unit)? = null,
|
||||
tokens: List<QuickToken> = emptyList(),
|
||||
elevated: Boolean = false,
|
||||
) {
|
||||
// Compose keeps a TextField focused (cursor blinking) even after the keyboard
|
||||
// is dismissed by a tap outside it — so when the IME hides, drop focus too.
|
||||
val focusManager = LocalFocusManager.current
|
||||
val imeVisible = WindowInsets.isImeVisible
|
||||
LaunchedEffect(imeVisible) {
|
||||
if (!imeVisible) focusManager.clearFocus()
|
||||
}
|
||||
val tokenStyle = SpanStyle(color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.Medium)
|
||||
val highlight = remember(tokens, tokenStyle) { TokenHighlight(tokens, tokenStyle) }
|
||||
Surface(
|
||||
shape = RoundedCornerShape(22.dp),
|
||||
// Floating use (bottom quick-add) steps up to the brightest container tone
|
||||
// — a real step above the surfaceContainerHigh task cards — and lifts off
|
||||
// the list with a heavier shadow, so it doesn't blend into the rows.
|
||||
color = if (elevated) {
|
||||
MaterialTheme.colorScheme.surfaceContainerHighest
|
||||
} else {
|
||||
MaterialTheme.colorScheme.surfaceContainerHigh
|
||||
},
|
||||
shadowElevation = if (elevated) 8.dp else 0.dp,
|
||||
modifier = modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = FloretDensity.cardRowInset),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(min = byDensity(56.dp, 48.dp))
|
||||
.padding(start = 16.dp, end = 4.dp),
|
||||
) {
|
||||
Icon(Icons.Rounded.Add, contentDescription = null, tint = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Box(modifier = Modifier.weight(1f)) {
|
||||
InlineTextField(
|
||||
value = text,
|
||||
onValueChange = onTextChange,
|
||||
placeholder = stringResource(R.string.add_task_hint),
|
||||
textStyle = MaterialTheme.typography.bodyLarge,
|
||||
imeAction = ImeAction.Done,
|
||||
onImeAction = { if (canSubmit) onSubmit() },
|
||||
visualTransformation = highlight,
|
||||
)
|
||||
}
|
||||
if (onExpand != null) {
|
||||
ActionButton(
|
||||
shape = ActionShapes.ExpandQuickAdd,
|
||||
icon = Icons.Rounded.OpenInFull,
|
||||
contentDescription = stringResource(R.string.quick_add_expand),
|
||||
onClick = onExpand,
|
||||
iconSize = 20.dp,
|
||||
)
|
||||
}
|
||||
if (canSubmit) {
|
||||
IconButton(onClick = onSubmit) {
|
||||
Icon(Icons.Rounded.Check, contentDescription = stringResource(R.string.cd_add_task))
|
||||
}
|
||||
} else if (onExpand == null) {
|
||||
Spacer(Modifier.width(12.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Marks [tokens] in [style], leaving the text as it is. */
|
||||
private class TokenHighlight(private val tokens: List<QuickToken>, private val style: SpanStyle) : VisualTransformation {
|
||||
override fun filter(text: AnnotatedString): TransformedText {
|
||||
if (tokens.isEmpty()) return TransformedText(text, OffsetMapping.Identity)
|
||||
val marked = AnnotatedString.Builder(text).apply {
|
||||
tokens.forEach { if (it.end <= text.length) addStyle(style, it.start, it.end) }
|
||||
}.toAnnotatedString()
|
||||
return TransformedText(marked, OffsetMapping.Identity)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?) = other is TokenHighlight && other.tokens == tokens && other.style == style
|
||||
|
||||
override fun hashCode() = 31 * tokens.hashCode() + style.hashCode()
|
||||
}
|
||||
|
||||
/** [receipt] as the screen's snack chip; a task that went elsewhere offers to open its list. */
|
||||
@Composable
|
||||
fun QuickAddReceiptChip(receipt: QuickAddReceipt, onOpenList: (Long) -> Unit) {
|
||||
when (receipt) {
|
||||
is QuickAddReceipt.NotShown -> SnackChip(
|
||||
visible = true,
|
||||
message = stringResource(R.string.quick_add_not_shown, receipt.listName),
|
||||
actionLabel = stringResource(R.string.quick_add_open),
|
||||
onAction = { onOpenList(receipt.listId) },
|
||||
)
|
||||
is QuickAddReceipt.Failed -> SnackChip(visible = true, message = stringResource(R.string.edit_save_failed))
|
||||
}
|
||||
}
|
||||
|
||||
/** A plain, unmarked quick-add field for a preview: nothing happens on it. */
|
||||
@Composable
|
||||
internal fun QuickAddFieldPreview(modifier: Modifier = Modifier) {
|
||||
var text by remember { mutableStateOf("") }
|
||||
QuickAddField(text = text, onTextChange = { text = it }, canSubmit = false, onSubmit = {}, modifier = modifier, elevated = true)
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package de.jeanlucmakiola.agendula.ui.quickadd
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import de.jeanlucmakiola.agendula.data.di.ApplicationScope
|
||||
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
|
||||
import de.jeanlucmakiola.agendula.domain.QuickAddParse
|
||||
import de.jeanlucmakiola.agendula.domain.QuickDue
|
||||
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.asOpenTask
|
||||
import de.jeanlucmakiola.agendula.domain.matches
|
||||
import de.jeanlucmakiola.floret.time.DayWindow
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import java.time.ZoneId
|
||||
import javax.inject.Inject
|
||||
import kotlin.time.Clock
|
||||
|
||||
/** What the full editor opens a new task with. */
|
||||
data class NewTaskSeed(
|
||||
/** The view whose scope the task should stay in. */
|
||||
val scopeViewId: Long? = null,
|
||||
/** What the screen shows, which presets the task as its own new-task action does. */
|
||||
val preset: TaskFilter? = null,
|
||||
val title: String? = null,
|
||||
val listId: Long? = null,
|
||||
val due: QuickDue? = null,
|
||||
)
|
||||
|
||||
/** How a quick add went, when there is something to say. Not data classes: a repeat must still show. */
|
||||
sealed interface QuickAddReceipt {
|
||||
/** Added to list [listId], [listName], which the view doesn't show. */
|
||||
class NotShown(val listId: Long, val listName: String) : QuickAddReceipt
|
||||
|
||||
class Failed : QuickAddReceipt
|
||||
}
|
||||
|
||||
private const val RECEIPT_MS = 4_000L
|
||||
|
||||
/** The quick-add bar of a view or list: the lists it can name, and the adding. */
|
||||
@HiltViewModel
|
||||
class QuickAddViewModel @Inject constructor(
|
||||
private val repository: TasksRepository,
|
||||
private val settingsPrefs: SettingsPrefs,
|
||||
@ApplicationScope private val appScope: CoroutineScope,
|
||||
) : ViewModel() {
|
||||
|
||||
/** The lists a task can be added to. */
|
||||
val lists: StateFlow<List<TaskList>> = repository.taskLists()
|
||||
.map { lists -> lists.filter { it.acceptsWrites } }
|
||||
.catch { emit(emptyList()) }
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
|
||||
|
||||
private val _receipt = MutableStateFlow<QuickAddReceipt?>(null)
|
||||
|
||||
/** Shown for a moment after an add that went elsewhere or failed. */
|
||||
val receipt: StateFlow<QuickAddReceipt?> = _receipt.asStateFlow()
|
||||
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.
|
||||
*/
|
||||
fun add(parse: QuickAddParse, scope: Rule?) {
|
||||
if (parse.title.isBlank()) return
|
||||
val write = appScope.async {
|
||||
runCatching {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
val writable = repository.taskLists().first().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 form = settings.quickAddTask(parse, scope, writable, clock, now) ?: return@runCatching QuickAddReceipt.Failed()
|
||||
repository.createTask(form)
|
||||
if (scope == null || scope.matches(form.asOpenTask(), clock)) {
|
||||
null
|
||||
} else {
|
||||
QuickAddReceipt.NotShown(form.listId, writable.firstOrNull { it.id == form.listId }?.name.orEmpty())
|
||||
}
|
||||
}.getOrElse { QuickAddReceipt.Failed() }
|
||||
}
|
||||
viewModelScope.launch { write.await()?.let(::show) }
|
||||
}
|
||||
|
||||
private fun show(receipt: QuickAddReceipt) {
|
||||
receiptTimer?.cancel()
|
||||
_receipt.value = receipt
|
||||
receiptTimer = viewModelScope.launch {
|
||||
delay(RECEIPT_MS)
|
||||
_receipt.value = null
|
||||
}
|
||||
}
|
||||
|
||||
fun dismissReceipt() {
|
||||
receiptTimer?.cancel()
|
||||
_receipt.value = null
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.jeanlucmakiola.agendula.R
|
||||
import de.jeanlucmakiola.agendula.ui.quickadd.QuickAddFieldPreview
|
||||
import de.jeanlucmakiola.agendula.domain.Priority
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
import de.jeanlucmakiola.agendula.domain.TaskStatus
|
||||
@@ -32,7 +33,7 @@ import de.jeanlucmakiola.floret.components.positionOf
|
||||
* A task list as it will look, for a chooser that offers a choice about it —
|
||||
* today only the bottom quick-add bar against the floating "New task" button.
|
||||
*
|
||||
* Drawn from the screen's own [TaskRowContent] and [InlineAdd] over sample
|
||||
* Drawn from the screen's own [TaskRowContent] and [QuickAddFieldPreview] over sample
|
||||
* tasks, so what the preview shows cannot drift from what the app does. The
|
||||
* chrome around them (a title, the FAB) is a still of the real scaffold rather
|
||||
* than the scaffold itself: [TaskListScreen] needs a ViewModel, a list id and a
|
||||
@@ -67,7 +68,7 @@ internal fun TaskListPreview(bottomAddBar: Boolean, height: Dp) {
|
||||
|
||||
if (bottomAddBar) {
|
||||
Box(Modifier.align(Alignment.BottomCenter)) {
|
||||
InlineAdd(onAdd = {}, elevated = true)
|
||||
QuickAddFieldPreview()
|
||||
}
|
||||
} else {
|
||||
ExtendedFloatingActionButton(
|
||||
|
||||
@@ -20,9 +20,6 @@ import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.WindowInsets
|
||||
import androidx.compose.foundation.layout.WindowInsetsSides
|
||||
import androidx.compose.foundation.layout.displayCutout
|
||||
import androidx.compose.foundation.layout.only
|
||||
import androidx.compose.foundation.layout.ime
|
||||
import androidx.compose.foundation.layout.isImeVisible
|
||||
import androidx.compose.foundation.layout.navigationBars
|
||||
@@ -74,7 +71,6 @@ import de.jeanlucmakiola.agendula.domain.TaskEnd
|
||||
import de.jeanlucmakiola.agendula.domain.TaskSortOrder
|
||||
import de.jeanlucmakiola.agendula.domain.UpcomingRange
|
||||
import de.jeanlucmakiola.floret.components.revealOnFocus
|
||||
import de.jeanlucmakiola.floret.components.InlineTextField
|
||||
import de.jeanlucmakiola.floret.components.FullScreenPicker
|
||||
import de.jeanlucmakiola.floret.components.GroupedRow
|
||||
import de.jeanlucmakiola.floret.components.GroupedSectionHeader
|
||||
@@ -95,7 +91,6 @@ import kotlinx.coroutines.delay
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.draw.rotate
|
||||
import androidx.compose.ui.graphics.Color
|
||||
@@ -113,6 +108,11 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import de.jeanlucmakiola.agendula.R
|
||||
import de.jeanlucmakiola.agendula.domain.Rule
|
||||
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.QuickAddViewModel
|
||||
import de.jeanlucmakiola.agendula.domain.Priority
|
||||
import de.jeanlucmakiola.agendula.domain.SmartList
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
@@ -159,15 +159,19 @@ import java.time.ZoneId
|
||||
fun TaskListScreen(
|
||||
filter: TaskFilter,
|
||||
onOpenTask: (Task) -> Unit,
|
||||
onNewTask: () -> Unit,
|
||||
onNewTask: (NewTaskSeed) -> Unit,
|
||||
onBack: () -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
onEditFilter: (Long) -> Unit = {},
|
||||
onOpenList: (Long) -> Unit = {},
|
||||
onPinned: () -> Unit = {},
|
||||
backClosesSearch: Boolean = true,
|
||||
viewModel: TaskListViewModel = hiltViewModel(),
|
||||
quickAdd: QuickAddViewModel = hiltViewModel(),
|
||||
) {
|
||||
val state by viewModel.state.collectAsStateWithLifecycle()
|
||||
val quickAddLists by quickAdd.lists.collectAsStateWithLifecycle()
|
||||
val quickAddReceipt by quickAdd.receipt.collectAsStateWithLifecycle()
|
||||
val syncAvailable by viewModel.syncAvailable.collectAsStateWithLifecycle()
|
||||
val syncing by viewModel.syncing.collectAsStateWithLifecycle()
|
||||
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
|
||||
@@ -320,7 +324,7 @@ fun TaskListScreen(
|
||||
floatingActionButton = {
|
||||
if (!showBottomAddBar && !searching) {
|
||||
ExtendedFloatingActionButton(
|
||||
onClick = onNewTask,
|
||||
onClick = { onNewTask(NewTaskSeed(scopeViewId = content?.scope?.viewId)) },
|
||||
icon = { Icon(Icons.Rounded.Add, contentDescription = null) },
|
||||
text = { Text(stringResource(R.string.new_task)) },
|
||||
)
|
||||
@@ -328,7 +332,22 @@ fun TaskListScreen(
|
||||
},
|
||||
bottomBar = {
|
||||
if (showBottomAddBar && !searching) {
|
||||
QuickAddBar(onAdd = { title -> viewModel.quickAdd(title, listId) })
|
||||
// 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) },
|
||||
onExpand = { parse ->
|
||||
onNewTask(
|
||||
NewTaskSeed(
|
||||
scopeViewId = content?.scope?.viewId,
|
||||
title = parse.title,
|
||||
listId = parse.list?.id,
|
||||
due = parse.due,
|
||||
),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
},
|
||||
) { inner ->
|
||||
@@ -383,8 +402,9 @@ fun TaskListScreen(
|
||||
.height(SnackChipHeight),
|
||||
contentAlignment = Alignment.CenterStart,
|
||||
) {
|
||||
// One chip, in priority order: undo, a refused list write, a search outcome, the read-only hint.
|
||||
// One chip, in priority order: undo, a refused list write, a quick add, a search outcome, the read-only hint.
|
||||
val failure = listWriteFailure
|
||||
val added = quickAddReceipt
|
||||
when {
|
||||
undo != null -> SnackChip(
|
||||
visible = true,
|
||||
@@ -398,6 +418,10 @@ fun TaskListScreen(
|
||||
visible = true,
|
||||
message = stringResource(listWriteFailureMessage(failure)),
|
||||
)
|
||||
added != null -> QuickAddReceiptChip(added, onOpenList = { id ->
|
||||
quickAdd.dismissReceipt()
|
||||
onOpenList(id)
|
||||
})
|
||||
searchReceipt != null -> SnackChip(
|
||||
visible = true,
|
||||
message = when (val r = searchReceipt) {
|
||||
@@ -754,88 +778,6 @@ internal fun LazyListScope.taskRows(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inline "add a task" — title only, into the current list. Shared with the
|
||||
* onboarding step that previews the bottom bar this sits in.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
internal fun InlineAdd(onAdd: (String) -> Unit, elevated: Boolean = false) {
|
||||
var text by remember { mutableStateOf("") }
|
||||
fun submit() {
|
||||
if (text.isNotBlank()) {
|
||||
onAdd(text.trim())
|
||||
text = ""
|
||||
}
|
||||
}
|
||||
// Compose keeps a TextField focused (cursor blinking) even after the keyboard
|
||||
// is dismissed by a tap outside it — so when the IME hides, drop focus too.
|
||||
val focusManager = LocalFocusManager.current
|
||||
val imeVisible = WindowInsets.isImeVisible
|
||||
LaunchedEffect(imeVisible) {
|
||||
if (!imeVisible) focusManager.clearFocus()
|
||||
}
|
||||
Surface(
|
||||
shape = RoundedCornerShape(22.dp),
|
||||
// Floating use (bottom quick-add) steps up to the brightest container tone
|
||||
// — a real step above the surfaceContainerHigh task cards — and lifts off
|
||||
// the list with a heavier shadow, so it doesn't blend into the rows.
|
||||
color = if (elevated) {
|
||||
MaterialTheme.colorScheme.surfaceContainerHighest
|
||||
} else {
|
||||
MaterialTheme.colorScheme.surfaceContainerHigh
|
||||
},
|
||||
shadowElevation = if (elevated) 8.dp else 0.dp,
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.fillMaxWidth().heightIn(min = 56.dp).padding(start = 16.dp, end = 4.dp),
|
||||
) {
|
||||
Icon(Icons.Rounded.Add, contentDescription = null, tint = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Box(modifier = Modifier.weight(1f)) {
|
||||
InlineTextField(
|
||||
value = text,
|
||||
onValueChange = { text = it },
|
||||
placeholder = stringResource(R.string.add_task_hint),
|
||||
textStyle = MaterialTheme.typography.bodyLarge,
|
||||
imeAction = ImeAction.Done,
|
||||
onImeAction = ::submit,
|
||||
)
|
||||
}
|
||||
if (text.isNotBlank()) {
|
||||
IconButton(onClick = ::submit) {
|
||||
Icon(Icons.Rounded.Check, contentDescription = stringResource(R.string.cd_add_task))
|
||||
}
|
||||
} else {
|
||||
Spacer(Modifier.width(12.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The bottom quick-add field: [InlineAdd] pinned as the scaffold's bottomBar, but
|
||||
* with no bar plate of its own — the rounded field floats on the screen background
|
||||
* (its own shadow lifting it off the list). Rides above the keyboard and clears the
|
||||
* navigation bar (ime ∪ navigation-bar insets).
|
||||
*/
|
||||
@Composable
|
||||
private fun QuickAddBar(onAdd: (String) -> Unit) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.windowInsetsPadding(
|
||||
WindowInsets.ime
|
||||
.union(WindowInsets.navigationBars)
|
||||
.union(WindowInsets.displayCutout.only(WindowInsetsSides.Horizontal)),
|
||||
),
|
||||
) {
|
||||
InlineAdd(onAdd = onAdd, elevated = true)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One task row: a tonal grouped-card surface (corners from [position], morphing
|
||||
* rounder on press — the app's shape family) wrapped in a swipe box. Swiping
|
||||
|
||||
@@ -648,12 +648,6 @@ class TaskListViewModel @Inject constructor(
|
||||
withContext(Dispatchers.Main) { showSearchReceipt(receipt) }
|
||||
}
|
||||
|
||||
/** Inline "add task" — a title into [listId], with the default dates. */
|
||||
fun quickAdd(title: String, listId: Long) = viewModelScope.launch {
|
||||
if (title.isBlank() || listId <= 0L) return@launch
|
||||
runCatching { repository.createTask(settingsPrefs.settings.first().quickTask(title, listId)) }
|
||||
}
|
||||
|
||||
private val _listWriteFailure = MutableStateFlow<ListWriteFailure?>(null)
|
||||
|
||||
/** Set when a list write is refused; the screen shows it and clears it. */
|
||||
|
||||
@@ -68,6 +68,7 @@ fun EditViewScreen(
|
||||
) {
|
||||
val draft by viewModel.draft.collectAsStateWithLifecycle()
|
||||
val listNames by viewModel.listNames.collectAsStateWithLifecycle()
|
||||
val quickAddTarget by viewModel.quickAddTarget.collectAsStateWithLifecycle()
|
||||
val saveFailed by viewModel.saveFailed.collectAsStateWithLifecycle()
|
||||
val view = draft
|
||||
val isNew = view?.id == 0L
|
||||
@@ -146,7 +147,8 @@ fun EditViewScreen(
|
||||
checked = view.quickAdd,
|
||||
onCheckedChange = viewModel::setQuickAdd,
|
||||
position = if (isNew) Position.Alone else Position.Bottom,
|
||||
summary = stringResource(R.string.view_quick_add_hint),
|
||||
summary = quickAddTarget?.let { stringResource(R.string.view_quick_add_target, it) }
|
||||
?: stringResource(R.string.view_quick_add_hint),
|
||||
leading = { Icon(Icons.Rounded.PostAdd, contentDescription = null) },
|
||||
)
|
||||
|
||||
|
||||
@@ -8,13 +8,21 @@ import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
|
||||
import de.jeanlucmakiola.agendula.domain.Rule
|
||||
import de.jeanlucmakiola.agendula.domain.View
|
||||
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.newView
|
||||
import de.jeanlucmakiola.agendula.domain.scopePreset
|
||||
import de.jeanlucmakiola.floret.time.DayWindow
|
||||
import java.time.ZoneId
|
||||
import kotlin.time.Clock
|
||||
import de.jeanlucmakiola.agendula.ui.filters.admitsEverything
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
@@ -70,6 +78,24 @@ class EditViewViewModel @Inject constructor(
|
||||
val listNames: StateFlow<Map<Long, String>> = repository.listNames()
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyMap())
|
||||
|
||||
/** The name of the list the quick-add bar puts a task in, as the scope stands; `null` with no list to put it in. */
|
||||
val quickAddTarget: StateFlow<String?> = combine(
|
||||
_draft,
|
||||
repository.taskLists().catch { emit(emptyList()) },
|
||||
settingsPrefs.settings,
|
||||
) { draft, lists, settings ->
|
||||
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 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)
|
||||
writable.firstOrNull { it.id == id }?.name
|
||||
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null)
|
||||
|
||||
private var bound = false
|
||||
private var saving = false
|
||||
|
||||
|
||||
@@ -1189,6 +1189,8 @@
|
||||
<string name="view_scope_blocks_hint">Every block shows its own tasks that also match the scope. A block can ignore the scope.</string>
|
||||
<string name="view_quick_add">Quick-add bar</string>
|
||||
<string name="view_quick_add_hint">A bar at the bottom of the view to add tasks</string>
|
||||
<!-- %1$s is the list a task added through the bar goes to. -->
|
||||
<string name="view_quick_add_target">New tasks go to %1$s</string>
|
||||
|
||||
<!-- Quick-add: the words typed after @ for a due date, e.g. "Call mum @tomorrow".
|
||||
Each is a comma-separated list of lowercase words without spaces; the English ones always work as well.
|
||||
@@ -1208,6 +1210,14 @@
|
||||
<string name="quick_add_keyword_days">d</string>
|
||||
<string name="quick_add_keyword_weeks">w</string>
|
||||
<string name="quick_add_keyword_months">m</string>
|
||||
<!-- %1$s is a list's name. -->
|
||||
<string name="quick_add_not_shown">Added to %1$s, not shown in this view</string>
|
||||
<string name="quick_add_open">Open</string>
|
||||
<string name="quick_add_expand">Open in the full editor</string>
|
||||
<!-- %1$s is a day, maybe with a time. -->
|
||||
<string name="quick_add_due_chip">Due %1$s</string>
|
||||
<!-- %1$s is a list's name. -->
|
||||
<string name="quick_add_list_chip">In %1$s</string>
|
||||
<string name="view_delete">Delete view</string>
|
||||
<string name="view_delete_title">Delete “%1$s”?</string>
|
||||
<string name="view_delete_body">The view and its layout go. Its tasks, lists and filters stay.</string>
|
||||
|
||||
@@ -179,11 +179,7 @@ class ViewScopeTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a scope's required tag is one at its top`() {
|
||||
assertThat(Rule.HasTag("x").requiredTag()).isEqualTo("x")
|
||||
assertThat(Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.HasTag("y"))).requiredTag()).isEqualTo("y")
|
||||
assertThat(Rule.AnyOf(listOf(Rule.HasTag("x"), Rule.HasTag("y"))).requiredTag()).isNull()
|
||||
assertThat(Rule.Not(Rule.HasTag("x")).requiredTag()).isNull()
|
||||
fun `a scope's limiting lists leave out those under a not`() {
|
||||
assertThat(Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.Not(Rule.InList(setOf(2L))))).limitingListIds()).containsExactly(1L)
|
||||
}
|
||||
}
|
||||
@@ -152,11 +152,11 @@ class HomePreviewsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a new task in a scoped view takes its one list and required tag`() {
|
||||
val state = content(scoped(Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.HasTag("x")))))
|
||||
assertThat(state.newTaskList).isEqualTo(1L)
|
||||
assertThat(state.newTaskTag).isEqualTo("x")
|
||||
assertThat(content(scoped(Rule.InList(setOf(1L, 2L)))).newTaskList).isNull()
|
||||
fun `a scoped view hands on its scope as it applies, deleted lists matching nothing`() {
|
||||
val rule = Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.HasTag("x")))
|
||||
assertThat(content(scoped(rule)).scopeRule).isEqualTo(rule)
|
||||
assertThat(content(scoped(Rule.InList(setOf(1L, 99L)))).scopeRule).isEqualTo(Rule.InList(setOf(1L)))
|
||||
assertThat(content(scoped(null)).scopeRule).isNull()
|
||||
assertThat(content(scoped(Rule.Not(Rule.InList(setOf(9L))))).scopeListsGone).isFalse()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user