Render home from the Home view's blocks (#59)

This commit is contained in:
makiolaj committed 2026-11-26 17:23:10 +01:00
1 parent b0bb853854
commit 6dec534528
2 files changed
+256 -163

No files matched your search

@@ -82,7 +82,14 @@ 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.Block
import de.jeanlucmakiola.agendula.domain.BlockOptions
import de.jeanlucmakiola.agendula.domain.BlockSource
import de.jeanlucmakiola.agendula.domain.BlockType
import de.jeanlucmakiola.agendula.domain.BlockWidth
import de.jeanlucmakiola.agendula.domain.TaskEnd
import de.jeanlucmakiola.agendula.domain.View
import de.jeanlucmakiola.agendula.domain.rows
import de.jeanlucmakiola.agendula.domain.Priority
import de.jeanlucmakiola.agendula.domain.SmartList
import de.jeanlucmakiola.agendula.domain.Task
@@ -108,11 +115,13 @@ import java.time.LocalDate
import java.time.ZoneId
/**
* Home: a Today progress hero (wavy ring over tasks due today), an Overdue + All
* 2-up of tonal tiles, a live preview of the next upcoming tasks, then the user's
* lists grouped by account. Tapping a tile opens that smart list, an upcoming row
* opens that task, and the FAB starts a new task. The search action in the top bar
* slides open into a full-width field that filters every task by title.
* Home, laid out by the blocks of the active [View]. The Home view is a Today
* progress hero (wavy ring over tasks due today), an Overdue + All 2-up of tonal
* tiles, a live preview of the next upcoming tasks, then the user's lists grouped
* by account, tags and saved filters. Tapping a tile opens that smart list, an
* upcoming row opens that task, and the FAB starts a new task. The search action
* in the top bar slides open into a full-width field that filters every task by
* title.
*/
@Composable
fun ListsScreen(
@@ -125,6 +134,7 @@ fun ListsScreen(
viewModel: ListsViewModel = hiltViewModel(),
) {
val state by viewModel.state.collectAsStateWithLifecycle()
val activeView by viewModel.activeView.collectAsStateWithLifecycle()
val syncAvailable by viewModel.syncAvailable.collectAsStateWithLifecycle()
val syncing by viewModel.syncing.collectAsStateWithLifecycle()
var query by rememberSaveable { mutableStateOf("") }
@@ -190,6 +200,7 @@ fun ListsScreen(
CenteredMessage(stringResource(R.string.lists_failure), PaddingValues(0.dp))
is ListsUiState.Content -> {
ListsContent(
view = activeView,
state = s,
onOpenFilter = onOpenFilter,
onOpenTask = onOpenTask,
@@ -265,6 +276,7 @@ internal fun listWriteFailureMessage(failure: ListWriteFailure): Int = when (fai
@Composable
private fun ListsContent(
view: View,
state: ListsUiState.Content,
onOpenFilter: (TaskFilter) -> Unit,
onOpenTask: (Task) -> Unit,
@@ -276,6 +288,7 @@ private fun ListsContent(
) {
// Lists get drag handles only while reordering, so the home screen stays calm.
var reordering by rememberSaveable { mutableStateOf(false) }
val rows = remember(view.blocks) { view.blocks.rows() }
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(
@@ -283,150 +296,231 @@ private fun ListsContent(
bottom = bottomPadding,
),
) {
item {
TodayHero(
done = state.todayDone,
total = state.todayTotal,
onClick = { onOpenFilter(TaskFilter.Smart(SmartList.TODAY)) },
)
}
// Overdue + All sit below the Today hero as a quieter 2-up; Upcoming is no
// longer a count tile — it becomes the live preview further down.
item {
SmartPairRow(
counts = state.smartCounts.filter {
it.smart == SmartList.OVERDUE || it.smart == SmartList.ALL
},
onOpenFilter = onOpenFilter,
)
}
if (state.upcoming.isNotEmpty()) {
item { SectionHeader(stringResource(R.string.smart_upcoming)) }
item {
UpcomingPreview(
tasks = state.upcoming,
onOpenTask = onOpenTask,
onViewAll = { onOpenFilter(TaskFilter.Smart(SmartList.UPCOMING)) },
)
// TODO(#59): apply the view's scope once views other than Home can have one.
rows.forEach { row ->
val block = row.singleOrNull()?.takeIf { it.width == BlockWidth.FULL }
if (block == null) {
item { HalfRow(row, state, onOpenFilter) }
return@forEach
}
}
if (state.groups.isEmpty()) {
item { EmptyLists(onNewList = onNewList) }
} else {
item {
SectionHeader(
text = stringResource(R.string.lists_header),
action = {
IconButton(onClick = { reordering = !reordering }) {
Icon(
if (reordering) Icons.Rounded.Check else Icons.Rounded.SwapVert,
contentDescription = stringResource(
if (reordering) R.string.lists_reorder_done else R.string.lists_reorder,
),
)
}
ShapedActionButton(
shape = ActionShapes.AddList,
morphTo = ActionShapes.AddListPressed,
icon = Icons.Rounded.Add,
contentDescription = stringResource(R.string.list_add),
onClick = onNewList,
)
},
)
}
state.groups.forEach { group ->
item(key = "acct-${group.accountName}") { AccountHeader(group.accountName) }
if (reordering) {
item(key = "reorder-${group.accountName}") {
ReorderableColumn(
items = group.lists,
keyOf = { it.list.id },
onReorder = { ordered -> onReorder(ordered.map { it.list.id }) },
rowHeight = 72.dp,
) { overview, position, dragHandle, isDragging ->
GroupedRow(
title = overview.list.name,
position = position,
minHeight = 72.dp,
gapBelow = false,
container = if (isDragging) MaterialTheme.colorScheme.secondaryContainer else null,
leading = { ListColorChip(overview.list.color) },
trailing = {
Box(modifier = dragHandle.size(48.dp), contentAlignment = Alignment.Center) {
Icon(
Icons.Rounded.DragHandle,
contentDescription = stringResource(R.string.lists_reorder_handle),
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
},
)
}
}
return@forEach
when (block.type) {
BlockType.PROGRESS -> progressFor(block, state)?.let { progress ->
item { TodayHero(progress, onClick = { onOpenFilter(progress.filter) }) }
}
itemsIndexed(group.lists, key = { _, o -> o.list.id }) { index, overview ->
BlockType.COUNT_TILE -> item { HalfRow(row, state, onOpenFilter) }
BlockType.PREVIEW -> previewSection(block, state, onOpenFilter, onOpenTask)
BlockType.LISTS -> listsSection(
key = "${block.id}",
groups = state.groups,
reordering = reordering,
onToggleReorder = { reordering = !reordering },
onOpenFilter = onOpenFilter,
onNewList = onNewList,
onReorder = onReorder,
)
BlockType.TAGS -> tagsSection("${block.id}", state.tags, onOpenFilter)
BlockType.FILTERS -> if (state.groups.isNotEmpty()) {
filtersSection("${block.id}", state.filters, onOpenFilter, onNewFilter)
}
// TODO(#59): the rest of the catalog renders from phase 3 on.
BlockType.TASK_LIST,
BlockType.SECTION_HEADING,
BlockType.WEEK_STRIP,
BlockType.RECENTLY_COMPLETED,
-> Unit
}
}
}
}
/** What a progress block shows: done of total for its source, or `null` for a source it can't show yet. */
private data class ProgressState(val done: Int, val total: Int, val filter: TaskFilter, val title: String?)
private fun progressFor(block: Block, state: ListsUiState.Content): ProgressState? {
val source = block.source as? BlockSource.Smart ?: return null
if (source.smart != SmartList.TODAY) return null
val title = (block.options as BlockOptions.Progress).title
return ProgressState(state.todayDone, state.todayTotal, TaskFilter.Smart(SmartList.TODAY), title)
}
/** The smart count a count tile shows; `null` for a source it can't show yet. */
private fun countFor(block: Block, state: ListsUiState.Content): SmartCount? {
val source = block.source as? BlockSource.Smart ?: return null
return state.smartCounts.firstOrNull { it.smart == source.smart }
}
/** A row of tiles: two half blocks side by side, or one with an empty half beside it. */
@Composable
private fun HalfRow(row: List<Block>, state: ListsUiState.Content, onOpenFilter: (TaskFilter) -> Unit) {
Row(
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp).padding(top = 8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
row.forEach { block ->
val cell = Modifier.weight(1f)
when (block.type) {
BlockType.COUNT_TILE -> countFor(block, state)?.let { count ->
SmartCard(
count = count,
modifier = cell,
onClick = { onOpenFilter(TaskFilter.Smart(count.smart)) },
)
} ?: Spacer(cell)
BlockType.PROGRESS -> progressFor(block, state)?.let { progress ->
TodayHero(progress, onClick = { onOpenFilter(progress.filter) }, modifier = cell)
} ?: Spacer(cell)
else -> Spacer(cell)
}
}
if (row.size == 1 && row.single().width == BlockWidth.HALF) Spacer(Modifier.weight(1f))
}
}
/** A preview of the source's next tasks, under its heading; nothing while there are none. */
private fun LazyListScope.previewSection(
block: Block,
state: ListsUiState.Content,
onOpenFilter: (TaskFilter) -> Unit,
onOpenTask: (Task) -> Unit,
) {
// TODO(#59): other sources once the preview block can take them.
if (block.source != BlockSource.Smart(SmartList.UPCOMING)) return
val options = block.options as BlockOptions.Preview
val tasks = state.upcoming.take(options.count)
if (tasks.isEmpty()) return
item { SectionHeader(stringResource(R.string.smart_upcoming)) }
item {
UpcomingPreview(
tasks = tasks,
onOpenTask = onOpenTask,
onViewAll = if (options.showAll) ({ onOpenFilter(TaskFilter.Smart(SmartList.UPCOMING)) }) else null,
)
}
}
/** The user's lists by account, reorderable in place, or the way to make the first one. */
private fun LazyListScope.listsSection(
key: String,
groups: List<AccountGroup>,
reordering: Boolean,
onToggleReorder: () -> Unit,
onOpenFilter: (TaskFilter) -> Unit,
onNewList: () -> Unit,
onReorder: (List<Long>) -> Unit,
) {
if (groups.isEmpty()) {
item { EmptyLists(onNewList = onNewList) }
return
}
item {
SectionHeader(
text = stringResource(R.string.lists_header),
action = {
IconButton(onClick = onToggleReorder) {
Icon(
if (reordering) Icons.Rounded.Check else Icons.Rounded.SwapVert,
contentDescription = stringResource(
if (reordering) R.string.lists_reorder_done else R.string.lists_reorder,
),
)
}
ShapedActionButton(
shape = ActionShapes.AddList,
morphTo = ActionShapes.AddListPressed,
icon = Icons.Rounded.Add,
contentDescription = stringResource(R.string.list_add),
onClick = onNewList,
)
},
)
}
groups.forEach { group ->
item(key = "$key-acct-${group.accountName}") { AccountHeader(group.accountName) }
if (reordering) {
item(key = "$key-reorder-${group.accountName}") {
ReorderableColumn(
items = group.lists,
keyOf = { it.list.id },
onReorder = { ordered -> onReorder(ordered.map { it.list.id }) },
rowHeight = 72.dp,
) { overview, position, dragHandle, isDragging ->
GroupedRow(
title = overview.list.name,
position = positionOf(index, group.lists.size),
position = position,
minHeight = 72.dp,
gapBelow = false,
container = if (isDragging) MaterialTheme.colorScheme.secondaryContainer else null,
leading = { ListColorChip(overview.list.color) },
trailing = {
if (overview.openCount > 0) {
Text(
overview.openCount.toString(),
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
Box(modifier = dragHandle.size(48.dp), contentAlignment = Alignment.Center) {
Icon(
Icons.Rounded.DragHandle,
contentDescription = stringResource(R.string.lists_reorder_handle),
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
},
onClick = { onOpenFilter(TaskFilter.OfList(overview.list.id)) },
)
}
}
return@forEach
}
if (state.tags.isNotEmpty()) {
item(key = "tags-header") { SectionHeader(stringResource(R.string.tags_header)) }
itemsIndexed(state.tags, key = { _, t -> "tag-${t.tag}" }) { index, overview ->
GroupedRow(
title = overview.tag,
position = positionOf(index, state.tags.size),
leading = {
Icon(
Icons.Rounded.Sell,
contentDescription = null,
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
},
trailing = {
itemsIndexed(group.lists, key = { _, o -> "$key-list-${o.list.id}" }) { index, overview ->
GroupedRow(
title = overview.list.name,
position = positionOf(index, group.lists.size),
minHeight = 72.dp,
leading = { ListColorChip(overview.list.color) },
trailing = {
if (overview.openCount > 0) {
Text(
overview.openCount.toString(),
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
},
onClick = { onOpenFilter(TaskFilter.Tagged(overview.tag)) },
)
}
}
},
onClick = { onOpenFilter(TaskFilter.OfList(overview.list.id)) },
)
}
}
}
if (state.groups.isNotEmpty()) {
filtersSection(state.filters, onOpenFilter, onNewFilter)
}
/** Each tag on an open task with its count; nothing while there are none. */
private fun LazyListScope.tagsSection(key: String, tags: List<TagOverview>, onOpenFilter: (TaskFilter) -> Unit) {
if (tags.isEmpty()) return
item(key = "$key-tags-header") { SectionHeader(stringResource(R.string.tags_header)) }
itemsIndexed(tags, key = { _, t -> "$key-tag-${t.tag}" }) { index, overview ->
GroupedRow(
title = overview.tag,
position = positionOf(index, tags.size),
leading = {
Icon(
Icons.Rounded.Sell,
contentDescription = null,
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
},
trailing = {
Text(
overview.openCount.toString(),
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
},
onClick = { onOpenFilter(TaskFilter.Tagged(overview.tag)) },
)
}
}
/** The saved filters, or a single row offering the first one. */
private fun LazyListScope.filtersSection(
key: String,
filters: List<FilterOverview>,
onOpenFilter: (TaskFilter) -> Unit,
onNewFilter: () -> Unit,
) {
item(key = "filters-header") {
item(key = "$key-filters-header") {
SectionHeader(
text = stringResource(R.string.filters_header),
action = if (filters.isEmpty()) {
@@ -441,7 +535,7 @@ private fun LazyListScope.filtersSection(
)
}
if (filters.isEmpty()) {
item(key = "filters-empty") {
item(key = "$key-filters-empty") {
GroupedRow(
title = stringResource(R.string.filter_new_title),
summary = stringResource(R.string.filters_empty_hint),
@@ -452,7 +546,7 @@ private fun LazyListScope.filtersSection(
}
return
}
itemsIndexed(filters, key = { _, f -> "filter-${f.filter.id}" }) { index, overview ->
itemsIndexed(filters, key = { _, f -> "$key-filter-${f.filter.id}" }) { index, overview ->
GroupedRow(
title = overview.filter.name,
position = positionOf(index, filters.size),
@@ -634,7 +728,12 @@ private fun SearchResults(
*/
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
private fun TodayHero(done: Int, total: Int, onClick: () -> Unit) {
private fun TodayHero(
progress: ProgressState,
onClick: () -> Unit,
modifier: Modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),
) {
val (done, total) = progress
val interaction = remember { MutableInteractionSource() }
val pressed by interaction.collectIsPressedAsState()
val corner by animateDpAsState(if (pressed) 34.dp else 22.dp, label = "todayCorner")
@@ -645,7 +744,7 @@ private fun TodayHero(done: Int, total: Int, onClick: () -> Unit) {
color = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
interactionSource = interaction,
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp).height(140.dp),
modifier = modifier.height(140.dp),
) {
Row(
modifier = Modifier.fillMaxSize().padding(20.dp),
@@ -661,7 +760,7 @@ private fun TodayHero(done: Int, total: Int, onClick: () -> Unit) {
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Icon(Icons.Rounded.Today, contentDescription = null, modifier = Modifier.size(20.dp))
Text(stringResource(R.string.smart_today), style = MaterialTheme.typography.titleMedium)
Text(progress.title ?: stringResource(R.string.smart_today), style = MaterialTheme.typography.titleMedium)
}
val headline = when {
total == 0 -> stringResource(R.string.home_today_empty)
@@ -692,30 +791,12 @@ private fun TodayHero(done: Int, total: Int, onClick: () -> Unit) {
}
}
/** Overdue + All as a 2-up row of the existing tonal tiles. */
@Composable
private fun SmartPairRow(counts: List<SmartCount>, onOpenFilter: (TaskFilter) -> Unit) {
Row(
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp).padding(top = 8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
counts.forEach { smart ->
SmartCard(
count = smart,
modifier = Modifier.weight(1f),
onClick = { onOpenFilter(TaskFilter.Smart(smart.smart)) },
)
}
if (counts.size == 1) Spacer(Modifier.weight(1f))
}
}
/** A grouped card previewing the next few upcoming tasks, with a "view all" tail. */
@Composable
private fun UpcomingPreview(
tasks: List<Task>,
onOpenTask: (Task) -> Unit,
onViewAll: () -> Unit,
onViewAll: (() -> Unit)?,
) {
Surface(
shape = RoundedCornerShape(22.dp),
@@ -726,24 +807,29 @@ private fun UpcomingPreview(
tasks.forEach { task ->
UpcomingRow(task = task, onClick = { onOpenTask(task) })
}
Surface(onClick = onViewAll, color = Color.Transparent, modifier = Modifier.fillMaxWidth()) {
Row(
modifier = Modifier.fillMaxWidth().heightIn(min = 48.dp).padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
stringResource(R.string.home_upcoming_view_all),
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.primary,
)
Spacer(Modifier.weight(1f))
Icon(
Icons.Rounded.ChevronRight,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
)
}
}
if (onViewAll != null) ViewAllRow(onViewAll)
}
}
}
@Composable
private fun ViewAllRow(onViewAll: () -> Unit) {
Surface(onClick = onViewAll, color = Color.Transparent, modifier = Modifier.fillMaxWidth()) {
Row(
modifier = Modifier.fillMaxWidth().heightIn(min = 48.dp).padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
stringResource(R.string.home_upcoming_view_all),
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.primary,
)
Spacer(Modifier.weight(1f))
Icon(
Icons.Rounded.ChevronRight,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
)
}
}
}
@@ -9,6 +9,7 @@ import de.jeanlucmakiola.agendula.data.sync.RemoteLists
import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
import de.jeanlucmakiola.agendula.data.tasks.recoveringFromProviderFailure
import de.jeanlucmakiola.floret.time.DayWindow
import de.jeanlucmakiola.agendula.domain.BlockOptions
import de.jeanlucmakiola.agendula.domain.RuleClock
import de.jeanlucmakiola.agendula.domain.SavedFilter
import de.jeanlucmakiola.agendula.domain.SmartList
@@ -17,6 +18,7 @@ import de.jeanlucmakiola.agendula.domain.TaskFilter
import de.jeanlucmakiola.agendula.domain.TaskFiltering
import de.jeanlucmakiola.agendula.domain.TaskList
import de.jeanlucmakiola.agendula.domain.UpcomingRange
import de.jeanlucmakiola.agendula.domain.View
import de.jeanlucmakiola.agendula.domain.tagsInUse
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
@@ -95,7 +97,7 @@ sealed interface ListsUiState {
/** Completed vs. total tasks *due today* — drives the progress ring. */
val todayDone: Int,
val todayTotal: Int,
/** The next few open tasks (due tomorrow onward) for the inline preview. */
/** The next few open tasks (due tomorrow onward) for the inline preview, as many as the longest one shows. */
val upcoming: List<Task>,
/** Every task (open and completed) — the corpus the home search filters. */
val allTasks: List<Task>,
@@ -106,7 +108,8 @@ sealed interface ListsUiState {
) : ListsUiState
}
private const val UPCOMING_PREVIEW = 3
/** As many upcoming tasks as the longest preview shows; each preview block takes its own count. */
private val UPCOMING_PREVIEW = BlockOptions.PREVIEW_COUNTS.max()
/** The home overview: smart lists with live counts, then user lists by account. */
@HiltViewModel
@@ -124,6 +127,10 @@ class ListsViewModel @Inject constructor(
fun syncAll() = manualSync.syncAll()
/** The view Home renders; for now always the first one, Home. */
val activeView: StateFlow<View> = settingsPrefs.views.map { it.first() }.distinctUntilChanged()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), View.home())
/**
* Where a new list may go: the device, then every account whose server said
* it would take one.