Carry the view in the home state, drop view pruning for now (#59)
This commit is contained in:
1 parent
3f97683c6d
commit
355daad87b
6 files changed
+42
-100
No files matched your search
@@ -407,7 +407,6 @@ class SettingsPrefs @Inject constructor(
|
||||
drop(GROUPINGS) { it.substringAfter(GROUPING_SEPARATOR) in keys }
|
||||
drop(COLLAPSED_SECTIONS) { it in keys }
|
||||
forgetInFilters(p, store, mode) { it in listIds }
|
||||
forgetInViews(p, store, mode) { it in listIds }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -428,7 +427,6 @@ class SettingsPrefs @Inject constructor(
|
||||
prune(GROUPINGS, ListEntry.GROUPING)
|
||||
prune(COLLAPSED_SECTIONS, ListEntry.COLLAPSED)
|
||||
forgetInFilters(p, store, mode) { it !in present }
|
||||
forgetInViews(p, store, mode) { it !in present }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -443,14 +441,6 @@ class SettingsPrefs @Inject constructor(
|
||||
if (kept != raw) p[SAVED_FILTERS] = kept
|
||||
}
|
||||
|
||||
/** Drops the lists [gone] names from the views, under the same rule as [forgetInFilters]. */
|
||||
private fun forgetInViews(p: MutablePreferences, store: StorageMode, mode: String, gone: (Long) -> Boolean) {
|
||||
if (mode != store.name) return
|
||||
val raw = p[VIEWS] ?: return
|
||||
val kept = ViewCodec.withoutLists(raw, store.name, gone)
|
||||
if (kept != null && kept != raw) p[VIEWS] = kept
|
||||
}
|
||||
|
||||
suspend fun setSyncIntervalMinutes(minutes: Int) = dataStore.edit { it[SYNC_INTERVAL] = minutes }
|
||||
|
||||
suspend fun setPushEnabled(enabled: Boolean) = dataStore.edit { it[PUSH_ENABLED] = enabled }
|
||||
@@ -627,7 +617,6 @@ class SettingsPrefs @Inject constructor(
|
||||
p[SAVED_FILTERS] = SavedFilterCodec.remove(p[SAVED_FILTERS], id)
|
||||
val key = TaskFilter.Saved(id).groupingKey()
|
||||
p[GROUPINGS] = p[GROUPINGS].orEmpty().filterTo(HashSet()) { it.substringAfter(GROUPING_SEPARATOR) != key }
|
||||
p[VIEWS]?.let { raw -> ViewCodec.withoutFilter(raw, modeOf(p).name, id)?.let { p[VIEWS] = it } }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,8 +13,6 @@ import de.jeanlucmakiola.agendula.domain.TileColor
|
||||
import de.jeanlucmakiola.agendula.domain.View
|
||||
import de.jeanlucmakiola.agendula.domain.ViewIcon
|
||||
import de.jeanlucmakiola.agendula.domain.normalized
|
||||
import de.jeanlucmakiola.agendula.domain.withoutFilter
|
||||
import de.jeanlucmakiola.agendula.domain.withoutLists
|
||||
import java.net.URLDecoder
|
||||
import java.net.URLEncoder
|
||||
|
||||
@@ -93,23 +91,6 @@ internal object ViewCodec {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* [raw] without the lists of [mode]'s store [gone] names (see [View.withoutLists]);
|
||||
* as it was when nothing changes or it doesn't read.
|
||||
*/
|
||||
fun withoutLists(raw: String?, mode: String, gone: (Long) -> Boolean): String? =
|
||||
rewrite(raw, mode) { it.withoutLists(gone) }
|
||||
|
||||
/** [raw] without the blocks showing the saved filter [filterId]. */
|
||||
fun withoutFilter(raw: String?, mode: String, filterId: Long): String? =
|
||||
rewrite(raw, mode) { it.withoutFilter(filterId) }
|
||||
|
||||
private fun rewrite(raw: String?, mode: String, change: (View) -> View): String? {
|
||||
val views = decode(raw, mode)
|
||||
val changed = views.map(change)
|
||||
return if (changed == views) raw else encode(changed, mode)
|
||||
}
|
||||
|
||||
private fun encodeBlock(block: Block, mode: String): String = listOf(
|
||||
"block",
|
||||
block.id.toString(),
|
||||
|
||||
@@ -255,21 +255,3 @@ fun List<Block>.rows(): List<List<Block>> {
|
||||
return rows
|
||||
}
|
||||
|
||||
/**
|
||||
* This view without the lists [gone] names: the scope and rule sources lose
|
||||
* them (see [Rule.withoutLists]) and blocks showing one of them drop out.
|
||||
*/
|
||||
fun View.withoutLists(gone: (Long) -> Boolean): View = copy(
|
||||
scope = scope?.withoutLists(gone),
|
||||
blocks = blocks.mapNotNull { block ->
|
||||
when (val source = block.source) {
|
||||
is BlockSource.List -> block.takeUnless { gone(source.listId) }
|
||||
is BlockSource.Rules -> block.copy(source = BlockSource.Rules(source.rule.withoutLists(gone)))
|
||||
else -> block
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
/** This view without the blocks showing the saved filter [filterId]. */
|
||||
fun View.withoutFilter(filterId: Long): View =
|
||||
copy(blocks = blocks.filterNot { it.source == BlockSource.Filter(filterId) })
|
||||
@@ -88,7 +88,6 @@ 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
|
||||
@@ -115,7 +114,7 @@ import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
|
||||
/**
|
||||
* Home, laid out by the blocks of the active [View]. The Home view is a Today
|
||||
* 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
|
||||
@@ -134,7 +133,6 @@ 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("") }
|
||||
@@ -199,20 +197,16 @@ fun ListsScreen(
|
||||
ListsUiState.Failure ->
|
||||
CenteredMessage(stringResource(R.string.lists_failure), PaddingValues(0.dp))
|
||||
is ListsUiState.Content -> {
|
||||
// The state already waits for the view, so this is only a guard.
|
||||
activeView?.let { view ->
|
||||
ListsContent(
|
||||
view = view,
|
||||
state = s,
|
||||
onOpenFilter = onOpenFilter,
|
||||
onOpenTask = onOpenTask,
|
||||
onNewList = { newList = true },
|
||||
onNewFilter = onNewFilter,
|
||||
onReorder = viewModel::reorderLists,
|
||||
topPadding = 0.dp,
|
||||
bottomPadding = inner.calculateBottomPadding() + 96.dp,
|
||||
)
|
||||
}
|
||||
ListsContent(
|
||||
state = s,
|
||||
onOpenFilter = onOpenFilter,
|
||||
onOpenTask = onOpenTask,
|
||||
onNewList = { newList = true },
|
||||
onNewFilter = onNewFilter,
|
||||
onReorder = viewModel::reorderLists,
|
||||
topPadding = 0.dp,
|
||||
bottomPadding = inner.calculateBottomPadding() + 96.dp,
|
||||
)
|
||||
// While searching with a non-blank query, results cover the home
|
||||
// content; an empty query leaves the home content visible behind
|
||||
// the open field.
|
||||
@@ -279,7 +273,6 @@ internal fun listWriteFailureMessage(failure: ListWriteFailure): Int = when (fai
|
||||
|
||||
@Composable
|
||||
private fun ListsContent(
|
||||
view: View,
|
||||
state: ListsUiState.Content,
|
||||
onOpenFilter: (TaskFilter) -> Unit,
|
||||
onOpenTask: (Task) -> Unit,
|
||||
@@ -292,7 +285,8 @@ private fun ListsContent(
|
||||
// Lists get drag handles only while reordering, so the home screen stays calm.
|
||||
// Holds the id of the lists block being reordered.
|
||||
var reordering by rememberSaveable { mutableStateOf<Long?>(null) }
|
||||
val rows = remember(view.blocks) { view.blocks.rows() }
|
||||
val blocks = state.view.blocks
|
||||
val rows = remember(blocks) { blocks.rows() }
|
||||
LazyColumn(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(
|
||||
@@ -302,20 +296,20 @@ private fun ListsContent(
|
||||
) {
|
||||
// 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 }
|
||||
// Tiles share a row, but a full progress tile stands alone like the Today hero always has.
|
||||
val block = row.singleOrNull()?.takeIf { it.width == BlockWidth.FULL && it.type != BlockType.COUNT_TILE }
|
||||
if (block == null) {
|
||||
item { HalfRow(row, state, onOpenFilter) }
|
||||
return@forEach
|
||||
}
|
||||
when (block.type) {
|
||||
BlockType.PROGRESS -> if (progressFor(block, state) != null) {
|
||||
item { Tile(block, state, onOpenFilter) }
|
||||
}
|
||||
BlockType.COUNT_TILE -> item { HalfRow(row, state, onOpenFilter) }
|
||||
BlockType.PROGRESS -> item { Tile(block, state, onOpenFilter) }
|
||||
BlockType.COUNT_TILE -> Unit
|
||||
BlockType.PREVIEW -> previewSection(block, state, onOpenFilter, onOpenTask)
|
||||
BlockType.LISTS -> listsSection(
|
||||
key = "${block.id}",
|
||||
groups = state.groups,
|
||||
options = block.options as BlockOptions.Lists,
|
||||
allGroups = state.groups,
|
||||
reordering = reordering == block.id,
|
||||
onToggleReorder = { reordering = if (reordering == block.id) null else block.id },
|
||||
onOpenFilter = onOpenFilter,
|
||||
@@ -343,7 +337,7 @@ private data class ProgressState(val done: Int, val total: Int, val filter: Task
|
||||
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?.takeIf { it.isNotBlank() }
|
||||
val title = (block.options as BlockOptions.Progress).title
|
||||
return ProgressState(state.todayDone, state.todayTotal, TaskFilter.Smart(SmartList.TODAY), title)
|
||||
}
|
||||
|
||||
@@ -412,17 +406,19 @@ private fun LazyListScope.previewSection(
|
||||
/** 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>,
|
||||
options: BlockOptions.Lists,
|
||||
allGroups: List<AccountGroup>,
|
||||
reordering: Boolean,
|
||||
onToggleReorder: () -> Unit,
|
||||
onOpenFilter: (TaskFilter) -> Unit,
|
||||
onNewList: () -> Unit,
|
||||
onReorder: (List<Long>) -> Unit,
|
||||
) {
|
||||
if (groups.isEmpty()) {
|
||||
if (allGroups.isEmpty()) {
|
||||
item { EmptyLists(onNewList = onNewList) }
|
||||
return
|
||||
}
|
||||
val groups = options.accounts?.let { accounts -> allGroups.filter { it.accountName in accounts } } ?: allGroups
|
||||
item {
|
||||
SectionHeader(
|
||||
text = stringResource(R.string.lists_header),
|
||||
@@ -483,7 +479,7 @@ private fun LazyListScope.listsSection(
|
||||
minHeight = 72.dp,
|
||||
leading = { ListColorChip(overview.list.color) },
|
||||
trailing = {
|
||||
if (overview.openCount > 0) {
|
||||
if (options.counts && overview.openCount > 0) {
|
||||
Text(
|
||||
overview.openCount.toString(),
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
|
||||
@@ -107,6 +107,8 @@ sealed interface ListsUiState {
|
||||
val tags: List<TagOverview> = emptyList(),
|
||||
/** The saved filters, oldest first. */
|
||||
val filters: List<FilterOverview> = emptyList(),
|
||||
/** The view these are laid out by. */
|
||||
val view: View = View.home(),
|
||||
) : ListsUiState
|
||||
}
|
||||
|
||||
@@ -132,10 +134,6 @@ class ListsViewModel @Inject constructor(
|
||||
|
||||
fun syncAll() = manualSync.syncAll()
|
||||
|
||||
private val homeView = settingsPrefs.views.map { views -> views.first { it.id == View.HOME_ID } }.distinctUntilChanged()
|
||||
|
||||
/** The view Home renders, for now always Home; `null` until it has been read. */
|
||||
val activeView: StateFlow<View?> = homeView.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null)
|
||||
|
||||
/**
|
||||
* Where a new list may go: the device, then every account whose server said
|
||||
@@ -164,15 +162,20 @@ class ListsViewModel @Inject constructor(
|
||||
combine(
|
||||
settingsPrefs.settings.map { it.hiddenFromSmartLists to it.upcomingRange }.distinctUntilChanged(),
|
||||
settingsPrefs.firstDayOfWeek,
|
||||
homeView.map { it.upcomingPreviewCount() }.distinctUntilChanged(),
|
||||
) { (hidden, range), weekStart, previewCount -> Triple(hidden, range, weekStart) to previewCount },
|
||||
// For now Home is always the view shown.
|
||||
settingsPrefs.views.map { views -> views.first { it.id == View.HOME_ID } }.distinctUntilChanged(),
|
||||
) { (hidden, range), weekStart, view -> Triple(hidden, range, weekStart) to view },
|
||||
settingsPrefs.savedFilters,
|
||||
) { lists, openTasks, completedTasks, (settings, previewCount), filters ->
|
||||
) { lists, openTasks, completedTasks, (settings, view), filters ->
|
||||
val (hidden, upcomingRange, weekStart) = settings
|
||||
buildContent(lists, openTasks, completedTasks, hidden, upcomingRange, filters, weekStart, previewCount) as ListsUiState
|
||||
buildContent(lists, openTasks, completedTasks, hidden, upcomingRange, filters, weekStart, view) as ListsUiState
|
||||
}.recoveringFromProviderFailure { ListsUiState.Failure }
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), ListsUiState.Loading)
|
||||
|
||||
/** The view Home renders, for now always Home; `null` until the state has been read. */
|
||||
val activeView: StateFlow<View?> = state.map { (it as? ListsUiState.Content)?.view }.distinctUntilChanged()
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null)
|
||||
|
||||
private fun buildContent(
|
||||
lists: List<TaskList>,
|
||||
openTasks: List<Task>,
|
||||
@@ -181,7 +184,7 @@ class ListsViewModel @Inject constructor(
|
||||
upcomingRange: UpcomingRange,
|
||||
savedFilters: List<SavedFilter>,
|
||||
weekStart: DayOfWeek,
|
||||
previewCount: Int,
|
||||
view: View,
|
||||
): ListsUiState.Content {
|
||||
val now = Clock.System.now()
|
||||
val zone = ZoneId.systemDefault()
|
||||
@@ -215,7 +218,7 @@ class ListsViewModel @Inject constructor(
|
||||
// already sorted by the repository's default ordering.
|
||||
val upcoming = smartTopLevel
|
||||
.filter { inSmart(it, SmartList.UPCOMING) }
|
||||
.take(previewCount)
|
||||
.take(view.upcomingPreviewCount())
|
||||
val openByList = topLevel.groupingBy { it.listId }.eachCount()
|
||||
val groups = lists
|
||||
.groupBy { it.accountName }
|
||||
@@ -245,6 +248,7 @@ class ListsViewModel @Inject constructor(
|
||||
FilterOverview(saved, matching.count { !it.isSubtask || it.parentId !in ids })
|
||||
}
|
||||
return ListsUiState.Content(
|
||||
view = view,
|
||||
smartCounts = smartCounts,
|
||||
filters = filters,
|
||||
groups = groups,
|
||||
|
||||
@@ -84,19 +84,9 @@ class ViewCodecTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deleted lists and filters leave the views`() {
|
||||
val raw = ViewCodec.encode(views, "OWN")
|
||||
val pruned = ViewCodec.decode(ViewCodec.withoutLists(raw, "OWN") { it == 9L || it == 2L }, "OWN")[1]
|
||||
assertThat(pruned.blocks.map { it.id }).doesNotContain(2L)
|
||||
val scope = pruned.scope as Rule.AllOf
|
||||
// A nested list condition left without this store's lists still names the other store's.
|
||||
assertThat((scope.children[0] as Rule.AnyOf).children[0]).isEqualTo(Rule.InList(emptySet(), setOf("EXTERNAL:7")))
|
||||
assertThat(ViewCodec.withoutLists(raw, "OWN") { it == 99L }).isSameInstanceAs(raw)
|
||||
|
||||
val withoutFilter = ViewCodec.decode(ViewCodec.withoutFilter(raw, "OWN", 12), "OWN")[1]
|
||||
assertThat(withoutFilter.blocks.map { it.id }).doesNotContain(4L)
|
||||
assertThat(ViewCodec.withoutFilter(raw, "OWN", 13)).isSameInstanceAs(raw)
|
||||
assertThat(ViewCodec.withoutFilter("v9\nwhatever", "OWN", 12)).isEqualTo("v9\nwhatever")
|
||||
fun `rules are stored in the saved filters' grammar`() {
|
||||
val view = View(5, "x", ViewIcon.HOME, Rule.AllOf(listOf(Rule.HasTag("a b"), Rule.InList(setOf(3L)))), emptyList())
|
||||
assertThat(ViewCodec.encode(listOf(view), "OWN").lines()[1]).isEqualTo("view|5|x|HOME|1|and(tag(a+b),list(OWN%3A3))")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user