diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt index aaafe1f..e7b1f2e 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt @@ -33,6 +33,7 @@ import de.jeanlucmakiola.agendula.domain.TaskSortOrder import de.jeanlucmakiola.agendula.domain.SmartList import de.jeanlucmakiola.agendula.domain.UpcomingRange import de.jeanlucmakiola.agendula.domain.View +import de.jeanlucmakiola.agendula.domain.withHomeFirst import de.jeanlucmakiola.agendula.domain.withinUpcoming import de.jeanlucmakiola.floret.reminders.ReminderLead import de.jeanlucmakiola.floret.reminders.ReminderOverride @@ -40,11 +41,13 @@ import de.jeanlucmakiola.floret.reminders.ReminderOverrideCodec import de.jeanlucmakiola.floret.reminders.applyReminderOverride import de.jeanlucmakiola.floret.reminders.normalizeReminders import de.jeanlucmakiola.floret.reminders.reminderLeadsFor +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flowOn import java.time.DayOfWeek import java.time.ZoneId import java.time.LocalDate @@ -620,15 +623,15 @@ class SettingsPrefs @Inject constructor( } /** - * The views in order, with the list ids of the current store. Home is always - * first: the default one stands in when the stored Home doesn't read. + * The views in order, with the list ids of the current store (see [listScope]), + * Home first (see [withHomeFirst]). */ val views: Flow> = - dataStore.data.map { p -> p[VIEWS] to p[STORAGE_MODE] } + dataStore.data.map { p -> p[VIEWS] to scopeOf(p) } .distinctUntilChanged() - .map { (raw, mode) -> ViewCodec.decode(raw, (storedMode(mode) ?: resolver.autoMode()).name) } - .map { views -> listOf(views.firstOrNull { it.id == View.HOME_ID } ?: View.home()) + views.filter { it.id != View.HOME_ID } } + .map { (raw, scope) -> ViewCodec.decode(raw, scope).withHomeFirst() } .distinctUntilChanged() + .flowOn(Dispatchers.Default) suspend fun setDefaultEditFields(fields: Set) = dataStore.edit { it[DEFAULT_EDIT_FIELDS] = fields.mapTo(mutableSetOf()) { field -> field.name } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodec.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodec.kt index 7c97bcb..34bc7ad 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodec.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodec.kt @@ -26,6 +26,9 @@ import java.net.URLEncoder * `list:OWN%3A3`, `tag:…`, `filter:4` or `rules:…`; options are `key=value` * pairs joined by `;`. Text is URL-encoded and booleans are `1` / `0`. * + * A writer has to keep the lines [decode] skips rather than encode only what it + * read, or a view from a newer version, or with a scope that doesn't parse, is lost. + * * Fixed once released — don't change without a data migration. */ internal object ViewCodec { @@ -130,7 +133,7 @@ internal object ViewCodec { return when (kind) { "smart" -> BlockSource.Smart(SmartList.valueOf(value)) "list" -> decode(value).let { ref -> - ref.removePrefix("$mode:").takeIf { it != ref }?.toLong()?.let(BlockSource::List) + ref.removePrefix("$mode:").takeIf { it != ref }?.toLongOrNull()?.let(BlockSource::List) ?: BlockSource.ListElsewhere(ref) } "tag" -> BlockSource.Tag(decode(value)) @@ -188,7 +191,7 @@ internal object ViewCodec { inlineDone = p["inlineDone"] != "0", ) BlockType.LISTS -> BlockOptions.Lists( - accounts = p["accounts"]?.split(',')?.filter { it.isNotEmpty() }?.mapTo(HashSet(), ::decode), + accounts = p["accounts"]?.split(',')?.mapTo(HashSet(), ::decode), counts = p["counts"] != "0", ) BlockType.TAGS -> BlockOptions.Tags diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt index f59236e..9c6ddc4 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt @@ -46,7 +46,7 @@ data class Block( val id: Long, val type: BlockType, val source: BlockSource? = null, - val width: BlockWidth = BlockWidth.FULL, + val width: BlockWidth = type.widths.first(), val ignoreScope: Boolean = false, val options: BlockOptions = BlockOptions.defaultFor(type), ) { @@ -55,6 +55,13 @@ data class Block( } } +/** + * These views with Home first: the stored one, or the default when it is missing, + * e.g. because its line didn't read. + */ +fun List.withHomeFirst(): List = + listOf(firstOrNull { it.id == View.HOME_ID } ?: View.home()) + filter { it.id != View.HOME_ID } + /** How much of a row a block takes: half-width blocks pack two to a row. */ enum class BlockWidth { HALF, FULL } @@ -127,6 +134,10 @@ sealed interface BlockOptions { /** [accounts] limits the lists to those accounts; every account when `null`. */ data class Lists(val accounts: Set? = null, val counts: Boolean = true) : BlockOptions { + init { + require(accounts == null || accounts.isNotEmpty()) { "No accounts; null means every one" } + } + override val type get() = BlockType.LISTS } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt index ae4d970..b13461b 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt @@ -303,8 +303,9 @@ private fun ListsContent( return@forEach } when (block.type) { - BlockType.PROGRESS -> item { Tile(block, state, onOpenFilter) } - BlockType.COUNT_TILE -> Unit + BlockType.PROGRESS -> item { + Tile(block, state, onOpenFilter, Modifier.fillMaxWidth().padding(horizontal = 16.dp)) + } BlockType.PREVIEW -> previewSection(block, state, onOpenFilter, onOpenTask) BlockType.LISTS -> listsSection( key = "${block.id}", @@ -320,6 +321,8 @@ private fun ListsContent( BlockType.FILTERS -> if (state.groups.isNotEmpty()) { filtersSection("${block.id}", state.filters, onOpenFilter, onNewFilter) } + // Count tiles always go to a tile row above. + BlockType.COUNT_TILE -> Unit // TODO(#59): the rest of the catalog renders from phase 3 on. BlockType.TASK_LIST, BlockType.SECTION_HEADING, @@ -368,8 +371,9 @@ private fun Tile( block: Block, state: ListsUiState.Content, onOpenFilter: (TaskFilter) -> Unit, - modifier: Modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp), + modifier: Modifier = Modifier, ) { + // TODO(#59): the wave switch and a tile's own title, colour and icon render from phase 3 on. when (block.type) { BlockType.PROGRESS -> progressFor(block, state)?.let { progress -> TodayHero(progress, onClick = { onOpenFilter(progress.filter) }, modifier = modifier) @@ -391,7 +395,7 @@ private fun LazyListScope.previewSection( // 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) + val tasks = state.previews[block.id].orEmpty() if (tasks.isEmpty()) return item { SectionHeader(stringResource(R.string.smart_upcoming)) } item { @@ -419,6 +423,7 @@ private fun LazyListScope.listsSection( return } val groups = options.accounts?.let { accounts -> allGroups.filter { it.accountName in accounts } } ?: allGroups + if (groups.isEmpty()) return item { SectionHeader( text = stringResource(R.string.lists_header), @@ -738,7 +743,7 @@ private fun SearchResults( private fun TodayHero( progress: ProgressState, onClick: () -> Unit, - modifier: Modifier, + modifier: Modifier = Modifier, ) { val (done, total) = progress val interaction = remember { MutableInteractionSource() } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt index 9d46343..160b1d8 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt @@ -99,8 +99,8 @@ 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), as many as the view's longest Upcoming preview shows. */ - val upcoming: List, + /** What each preview block of [view] shows, by block id. */ + val previews: Map>, /** Every task (open and completed) — the corpus the home search filters. */ val allTasks: List, /** Each tag on an open task, alphabetically. */ @@ -112,11 +112,13 @@ sealed interface ListsUiState { ) : ListsUiState } - -/** How many upcoming tasks this view's previews of Upcoming show at most. */ -internal fun View.upcomingPreviewCount(): Int = blocks +/** + * The tasks each preview block of this view shows, by block id: the first of + * [upcoming] for a preview of Upcoming. Other sources show nothing yet. + */ +internal fun View.previews(upcoming: Sequence): Map> = blocks .filter { it.type == BlockType.PREVIEW && it.source == BlockSource.Smart(SmartList.UPCOMING) } - .maxOfOrNull { (it.options as BlockOptions.Preview).count } ?: 0 + .associate { it.id to upcoming.take((it.options as BlockOptions.Preview).count).toList() } /** The home overview: smart lists with live counts, then user lists by account. */ @HiltViewModel @@ -134,7 +136,6 @@ class ListsViewModel @Inject constructor( fun syncAll() = manualSync.syncAll() - /** * Where a new list may go: the device, then every account whose server said * it would take one. @@ -172,10 +173,6 @@ class ListsViewModel @Inject constructor( }.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 = state.map { (it as? ListsUiState.Content)?.view }.distinctUntilChanged() - .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null) - private fun buildContent( lists: List, openTasks: List, @@ -216,9 +213,7 @@ class ListsViewModel @Inject constructor( // Upcoming preview: the next handful of open tasks due tomorrow onward, // already sorted by the repository's default ordering. - val upcoming = smartTopLevel - .filter { inSmart(it, SmartList.UPCOMING) } - .take(view.upcomingPreviewCount()) + val upcoming = smartTopLevel.asSequence().filter { inSmart(it, SmartList.UPCOMING) } val openByList = topLevel.groupingBy { it.listId }.eachCount() val groups = lists .groupBy { it.accountName } @@ -254,7 +249,7 @@ class ListsViewModel @Inject constructor( groups = groups, todayDone = completedToday, todayTotal = todayTotal, - upcoming = upcoming, + previews = view.previews(upcoming), allTasks = allTasks, tags = tags, ) diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodecTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodecTest.kt index d9e934f..137f0bf 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodecTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodecTest.kt @@ -89,6 +89,23 @@ class ViewCodecTest { assertThat(ViewCodec.encode(listOf(view), "OWN").lines()[1]).isEqualTo("view|5|x|HOME|1|and(tag(a+b),list(OWN%3A3))") } + @Test + fun `an account without a name and a list id that isn't one survive`() { + val view = View(1, "x", ViewIcon.HOME, null, listOf(Block(1, BlockType.LISTS, options = BlockOptions.Lists(accounts = setOf(""))))) + assertThat(ViewCodec.decode(ViewCodec.encode(listOf(view), "OWN"), "OWN")).containsExactly(view) + val odd = ViewCodec.decode("v1\nview|1|x|HOME|1|\nblock|4|COUNT_TILE|HALF|0|list:OWN%3Aabc|", "OWN").single() + assertThat(odd.blocks.single().source).isEqualTo(BlockSource.ListElsewhere("OWN:abc")) + } + + @Test + fun `an external provider's lists are its own`() { + val view = View(1, "x", ViewIcon.HOME, null, listOf(Block(1, BlockType.COUNT_TILE, BlockSource.List(5)))) + val raw = ViewCodec.encode(listOf(view), "EXTERNAL@org.tasks.opentasks") + assertThat(ViewCodec.decode(raw, "EXTERNAL@org.tasks.opentasks")).containsExactly(view) + assertThat(ViewCodec.decode(raw, "EXTERNAL").single().blocks.single().source) + .isEqualTo(BlockSource.ListElsewhere("EXTERNAL@org.tasks.opentasks:5")) + } + @Test fun `nothing stored, or another version, reads as no views`() { assertThat(ViewCodec.decode(null, "OWN")).isEmpty() diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt index e8b8aa0..f903f76 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt @@ -90,4 +90,20 @@ class ViewTest { assertThat(listOf(Block(5, BlockType.LISTS, BlockSource.Smart(SmartList.ALL)), full(5)).normalized()) .containsExactly(full(5)) } + + @Test + fun `a block takes its type's first width unless told`() { + assertThat(Block(1, BlockType.COUNT_TILE, BlockSource.Smart(SmartList.ALL)).width).isEqualTo(BlockWidth.HALF) + assertThat(Block(1, BlockType.PROGRESS, BlockSource.Smart(SmartList.TODAY)).width).isEqualTo(BlockWidth.FULL) + assertThrows { BlockOptions.Lists(accounts = emptySet()) } + } + + @Test + fun `home comes first, the default standing in when it is missing`() { + val work = View(2, "Work", ViewIcon.WORK, null, emptyList()) + val mine = View.home().copy(name = "Mine") + assertThat(listOf(work, mine).withHomeFirst()).containsExactly(mine, work).inOrder() + assertThat(listOf(work).withHomeFirst()).containsExactly(View.home(), work).inOrder() + assertThat(emptyList().withHomeFirst()).containsExactly(View.home()) + } } diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/HomePreviewsTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/HomePreviewsTest.kt new file mode 100644 index 0000000..27ad9f7 --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/HomePreviewsTest.kt @@ -0,0 +1,35 @@ +package de.jeanlucmakiola.agendula.ui.lists + +import com.google.common.truth.Truth.assertThat +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.SmartList +import de.jeanlucmakiola.agendula.domain.Task +import de.jeanlucmakiola.agendula.domain.View +import de.jeanlucmakiola.agendula.domain.testTask +import org.junit.jupiter.api.Test + +class HomePreviewsTest { + + private val upcoming = (1L..12L).map { testTask(id = it) } + + @Test + fun `home previews the next three upcoming tasks, as 1_4 did`() { + assertThat(View.home().previews(upcoming.asSequence())).containsExactly(4L, upcoming.take(3)) + } + + @Test + fun `each Upcoming preview takes its own count, other sources show nothing yet`() { + val view = View.home().copy( + blocks = listOf( + Block(1, BlockType.PREVIEW, BlockSource.Smart(SmartList.UPCOMING), options = BlockOptions.Preview(count = 5)), + Block(2, BlockType.PREVIEW, BlockSource.Smart(SmartList.UPCOMING), options = BlockOptions.Preview(count = 10)), + Block(3, BlockType.PREVIEW, BlockSource.Smart(SmartList.TODAY)), + ), + ) + assertThat(view.previews(upcoming.asSequence())).containsExactly(1L, upcoming.take(5), 2L, upcoming.take(10)) + assertThat(view.previews(emptySequence())).containsExactly(1L, emptyList(), 2L, emptyList()) + } +} diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/UpcomingPreviewCountTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/UpcomingPreviewCountTest.kt deleted file mode 100644 index 890615b..0000000 --- a/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/UpcomingPreviewCountTest.kt +++ /dev/null @@ -1,30 +0,0 @@ -package de.jeanlucmakiola.agendula.ui.lists - -import com.google.common.truth.Truth.assertThat -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.SmartList -import de.jeanlucmakiola.agendula.domain.View -import org.junit.jupiter.api.Test - -class UpcomingPreviewCountTest { - - @Test - fun `home previews three upcoming tasks, as 1_4 did`() { - assertThat(View.home().upcomingPreviewCount()).isEqualTo(3) - } - - @Test - fun `the longest Upcoming preview sets the count, other previews don't`() { - val view = View.home().copy( - blocks = listOf( - Block(1, BlockType.PREVIEW, BlockSource.Smart(SmartList.UPCOMING), options = BlockOptions.Preview(count = 5)), - Block(2, BlockType.PREVIEW, BlockSource.Smart(SmartList.TODAY), options = BlockOptions.Preview(count = 10)), - ), - ) - assertThat(view.upcomingPreviewCount()).isEqualTo(5) - assertThat(view.copy(blocks = emptyList()).upcomingPreviewCount()).isEqualTo(0) - } -}