Make the Upcoming range configurable (#128)

This commit is contained in:
makiolaj committed 2026-11-16 19:01:18 +01:00
1 parent 1463b08d6f
commit 3347bb13cd
8 files changed
+183 -14

No files matched your search

@@ -19,6 +19,8 @@ import de.jeanlucmakiola.agendula.domain.TaskSection
import de.jeanlucmakiola.agendula.domain.asTaskReminder
import de.jeanlucmakiola.agendula.domain.asReminderLead
import de.jeanlucmakiola.agendula.domain.TaskSortOrder
import de.jeanlucmakiola.agendula.domain.SmartList
import de.jeanlucmakiola.agendula.domain.UpcomingRange
import de.jeanlucmakiola.floret.reminders.ReminderLead
import de.jeanlucmakiola.floret.reminders.ReminderOverride
import de.jeanlucmakiola.floret.reminders.ReminderOverrideCodec
@@ -28,9 +30,12 @@ import de.jeanlucmakiola.floret.reminders.reminderLeadsFor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import java.time.DayOfWeek
import java.time.ZoneId
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.time.Clock
import kotlin.time.Instant
/** [BLACK] is dark with pure-black surfaces, for AMOLED screens. */
enum class ThemeMode {
@@ -122,14 +127,26 @@ data class Settings(
val defaultStartToday: Boolean = false,
/** Task-list sections folded away; Done starts folded. */
val collapsedSections: Set<TaskSection> = DEFAULT_COLLAPSED_SECTIONS,
/** How far ahead the Upcoming smart list reaches. */
val upcomingRange: UpcomingRange = UpcomingRange.ALL,
) {
/** [tasks] as [filter] shows them: a smart list drops the lists kept out of it. */
fun visibleIn(filter: TaskFilter, tasks: List<Task>): List<Task> =
if (filter is TaskFilter.Smart && hiddenFromSmartLists.isNotEmpty()) {
tasks.filter { it.listId !in hiddenFromSmartLists }
} else {
tasks
/**
* [tasks] as [filter] shows them: a smart list drops the lists kept out of it,
* and Upcoming whatever is due past [upcomingRange].
*/
fun visibleIn(
filter: TaskFilter,
tasks: List<Task>,
now: Instant = Clock.System.now(),
zone: ZoneId = ZoneId.systemDefault(),
): List<Task> {
if (filter !is TaskFilter.Smart) return tasks
val limited = filter.list == SmartList.UPCOMING && upcomingRange != UpcomingRange.ALL
if (hiddenFromSmartLists.isEmpty() && !limited) return tasks
return tasks.filter {
it.listId !in hiddenFromSmartLists && (!limited || upcomingRange.includes(it, now, zone))
}
}
/** The lead times for a task in [listId]: its override if set, else the global default. */
fun reminderLeadsFor(listId: Long): List<ReminderLead> =
@@ -202,6 +219,8 @@ class SettingsPrefs @Inject constructor(
?.mapNotNull { name -> runCatching { TaskSection.valueOf(name) }.getOrNull() }
?.toSet()
?: DEFAULT_COLLAPSED_SECTIONS,
upcomingRange = p[UPCOMING_RANGE]?.let { runCatching { UpcomingRange.valueOf(it) }.getOrNull() }
?: UpcomingRange.ALL,
)
}
@@ -210,6 +229,8 @@ class SettingsPrefs @Inject constructor(
p[COLLAPSED_SECTIONS] = if (collapsed) current + section.name else current - section.name
}
suspend fun setUpcomingRange(range: UpcomingRange) = dataStore.edit { it[UPCOMING_RANGE] = range.name }
suspend fun setSortOrder(order: TaskSortOrder) = dataStore.edit { it[SORT_ORDER] = order.name }
suspend fun setSnoozeMinutes(minutes: Int) = dataStore.edit { it[SNOOZE_MINUTES] = minutes.coerceAtLeast(1) }
@@ -372,6 +393,7 @@ class SettingsPrefs @Inject constructor(
val SMART_LIST_HIDDEN = stringSetPreferencesKey("smart_list_hidden")
val DEFAULT_START_TODAY = booleanPreferencesKey("default_start_today")
val COLLAPSED_SECTIONS = stringSetPreferencesKey("collapsed_sections")
val UPCOMING_RANGE = stringPreferencesKey("upcoming_range")
val SAVED_FILTERS = stringPreferencesKey("saved_filters")
val SAVED_FILTER_NEXT_ID = longPreferencesKey("saved_filter_next_id")
}
@@ -0,0 +1,29 @@
package de.jeanlucmakiola.agendula.domain
import java.time.LocalDate
import java.time.ZoneId
import kotlin.time.Instant
/** How far ahead the Upcoming smart list reaches; [ALL] has no limit. */
enum class UpcomingRange {
ALL, WEEK, TWO_WEEKS, MONTH, THREE_MONTHS, YEAR;
/** The last day Upcoming shows when today is [today]; `null` for no limit. */
fun lastDay(today: LocalDate): LocalDate? = when (this) {
ALL -> null
WEEK -> today.plusWeeks(1)
TWO_WEEKS -> today.plusWeeks(2)
MONTH -> today.plusMonths(1)
THREE_MONTHS -> today.plusMonths(3)
YEAR -> today.plusYears(1)
}
/** Whether [task]'s due falls on or before [lastDay]; an undated task always does. */
fun includes(task: Task, now: Instant, zone: ZoneId = ZoneId.systemDefault()): Boolean {
val today = java.time.Instant.ofEpochMilli(now.toEpochMilliseconds()).atZone(zone).toLocalDate()
val last = lastDay(today) ?: return true
val due = task.wallClock(task.due, zone) ?: return true
val end = last.plusDays(1).atStartOfDay(zone).toInstant().toEpochMilli()
return due.toEpochMilliseconds() < end
}
}
@@ -15,6 +15,7 @@ import de.jeanlucmakiola.agendula.domain.Task
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.tagsInUse
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
@@ -141,10 +142,10 @@ class ListsViewModel @Inject constructor(
// Open smart lists drop completed tasks, but the Today ring needs the
// ones already ticked off to show "x of y done", so read them too.
repository.tasks(TaskFilter.Smart(SmartList.COMPLETED)),
settingsPrefs.settings.map { it.hiddenFromSmartLists }.distinctUntilChanged(),
settingsPrefs.settings.map { it.hiddenFromSmartLists to it.upcomingRange }.distinctUntilChanged(),
settingsPrefs.savedFilters,
) { lists, openTasks, completedTasks, hidden, filters ->
buildContent(lists, openTasks, completedTasks, hidden, filters) as ListsUiState
) { lists, openTasks, completedTasks, (hidden, upcomingRange), filters ->
buildContent(lists, openTasks, completedTasks, hidden, upcomingRange, filters) as ListsUiState
}.recoveringFromProviderFailure { ListsUiState.Failure }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), ListsUiState.Loading)
@@ -153,17 +154,22 @@ class ListsViewModel @Inject constructor(
openTasks: List<Task>,
completedTasks: List<Task>,
hidden: Set<Long>,
upcomingRange: UpcomingRange,
savedFilters: List<SavedFilter>,
): ListsUiState.Content {
val (todayStart, todayEnd) = DayWindow.today(Clock.System.now(), ZoneId.systemDefault())
val now = Clock.System.now()
val zone = ZoneId.systemDefault()
val (todayStart, todayEnd) = DayWindow.today(now, zone)
// Count only top-level tasks: a subtask is represented by its parent (and
// its progress chip), and an open subtask under a *completed* parent must
// not read as an open item here. Mirrors the task list collapsing subtasks.
val topLevel = openTasks.filter { !it.isSubtask }
// The smart counts leave out the hidden lists; each list's own count does not.
val smartTopLevel = topLevel.filter { it.listId !in hidden }
fun count(smart: SmartList) =
smartTopLevel.count { TaskFiltering.matches(it, TaskFilter.Smart(smart), todayStart, todayEnd) }
fun inSmart(task: Task, smart: SmartList) =
TaskFiltering.matches(task, TaskFilter.Smart(smart), todayStart, todayEnd) &&
(smart != SmartList.UPCOMING || upcomingRange.includes(task, now, zone))
fun count(smart: SmartList) = smartTopLevel.count { inSmart(it, smart) }
val smartCounts = listOf(
SmartCount(SmartList.TODAY, count(SmartList.TODAY)),
@@ -182,7 +188,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 { TaskFiltering.matches(it, TaskFilter.Smart(SmartList.UPCOMING), todayStart, todayEnd) }
.filter { inSmart(it, SmartList.UPCOMING) }
.take(UPCOMING_PREVIEW)
val openByList = topLevel.groupingBy { it.listId }.eachCount()
val groups = lists
@@ -22,6 +22,7 @@ import de.jeanlucmakiola.agendula.data.appname.LauncherName
import de.jeanlucmakiola.agendula.data.prefs.ThemeMode
import de.jeanlucmakiola.agendula.data.prefs.TimeFormatPref
import de.jeanlucmakiola.agendula.data.prefs.is24Hour
import de.jeanlucmakiola.agendula.domain.UpcomingRange
import de.jeanlucmakiola.agendula.ui.common.formatMinuteOfDay
import de.jeanlucmakiola.agendula.ui.common.localeFirstDayOfWeek
import de.jeanlucmakiola.floret.components.CollapsingScaffold
@@ -35,7 +36,7 @@ import de.jeanlucmakiola.floret.locale.currentLocale
import java.time.DayOfWeek
import java.time.format.TextStyle
/** How the app looks: the theme, wallpaper colours, and how times and weeks are written. */
/** How the app looks: the theme, wallpaper colours, how times and weeks are written, and how far Upcoming reaches. */
@Composable
internal fun AppearanceScreen(
state: SettingsUiState,
@@ -49,6 +50,7 @@ internal fun AppearanceScreen(
val launcherName by viewModel.launcherName.collectAsStateWithLifecycle()
var showTimeFormat by remember { mutableStateOf(false) }
var showWeekStart by remember { mutableStateOf(false) }
var showUpcomingRange by remember { mutableStateOf(false) }
val systemIs24Hour = DateFormat.is24HourFormat(LocalContext.current)
val locale = currentLocale()
@@ -100,6 +102,14 @@ internal fun AppearanceScreen(
onClick = { showWeekStart = true },
)
Spacer(Modifier.height(24.dp))
GroupedRow(
title = stringResource(R.string.settings_upcoming_range),
summary = stringResource(upcomingRangeLabel(state.settings.upcomingRange)),
position = Position.Alone,
onClick = { showUpcomingRange = true },
)
Spacer(Modifier.height(24.dp))
GroupedRow(
title = stringResource(R.string.settings_app_name),
@@ -173,6 +183,21 @@ internal fun AppearanceScreen(
)
}
if (showUpcomingRange) {
OptionPicker(
title = stringResource(R.string.settings_upcoming_range),
options = UpcomingRange.entries,
selected = state.settings.upcomingRange,
label = { stringResource(upcomingRangeLabel(it)) },
header = {
SettingsHint(stringResource(R.string.settings_upcoming_range_hint))
Spacer(Modifier.height(8.dp))
},
onSelect = viewModel::setUpcomingRange,
onDismiss = { showUpcomingRange = false },
)
}
if (showTheme) {
OptionPicker(
title = stringResource(R.string.settings_theme),
@@ -217,3 +242,12 @@ private fun themeLabel(mode: ThemeMode): Int = when (mode) {
ThemeMode.DARK -> R.string.settings_theme_dark
ThemeMode.BLACK -> R.string.settings_theme_black
}
private fun upcomingRangeLabel(range: UpcomingRange): Int = when (range) {
UpcomingRange.ALL -> R.string.settings_upcoming_range_all
UpcomingRange.WEEK -> R.string.settings_upcoming_range_week
UpcomingRange.TWO_WEEKS -> R.string.settings_upcoming_range_two_weeks
UpcomingRange.MONTH -> R.string.settings_upcoming_range_month
UpcomingRange.THREE_MONTHS -> R.string.settings_upcoming_range_three_months
UpcomingRange.YEAR -> R.string.settings_upcoming_range_year
}
@@ -24,6 +24,7 @@ import de.jeanlucmakiola.agendula.data.tasks.transfer.ExternalImport
import de.jeanlucmakiola.agendula.data.tasks.transfer.TransferCounts
import de.jeanlucmakiola.agendula.data.tasks.transfer.TransferResult
import de.jeanlucmakiola.agendula.domain.TaskFormField
import de.jeanlucmakiola.agendula.domain.UpcomingRange
import de.jeanlucmakiola.agendula.domain.TaskList
import de.jeanlucmakiola.floret.reminders.ReminderLead
import de.jeanlucmakiola.floret.reminders.ReminderOverride
@@ -259,6 +260,7 @@ class SettingsViewModel @Inject constructor(
fun setAutofocusTitle(enabled: Boolean) = viewModelScope.launch { prefs.setAutofocusTitle(enabled) }
fun setTimeFormat(pref: TimeFormatPref) = viewModelScope.launch { prefs.setTimeFormat(pref) }
fun setWeekStart(day: DayOfWeek?) = viewModelScope.launch { prefs.setWeekStart(day) }
fun setUpcomingRange(range: UpcomingRange) = viewModelScope.launch { prefs.setUpcomingRange(range) }
fun setSnoozeMinutes(minutes: Int) = viewModelScope.launch { prefs.setSnoozeMinutes(minutes) }
fun setReminderDescription(pref: ReminderDescription) = viewModelScope.launch { prefs.setReminderDescription(pref) }
fun setShowAddSubtaskRow(show: Boolean) = viewModelScope.launch { prefs.setShowAddSubtaskRow(show) }
+8
View File
@@ -431,6 +431,14 @@
<string name="settings_week_start">Week starts on</string>
<string name="settings_week_start_auto">Automatic</string>
<string name="settings_week_start_auto_summary">Currently %1$s</string>
<string name="settings_upcoming_range">Upcoming shows</string>
<string name="settings_upcoming_range_hint">Tasks due further ahead stay in All until they come closer.</string>
<string name="settings_upcoming_range_all">Everything ahead</string>
<string name="settings_upcoming_range_week">The next week</string>
<string name="settings_upcoming_range_two_weeks">The next 2 weeks</string>
<string name="settings_upcoming_range_month">The next month</string>
<string name="settings_upcoming_range_three_months">The next 3 months</string>
<string name="settings_upcoming_range_year">The next year</string>
<!-- About -->
<string name="settings_about_author">by Jean-Luc Makiola</string>
<string name="settings_about_source">Source</string>
@@ -3,8 +3,11 @@ package de.jeanlucmakiola.agendula.data.prefs
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.agendula.domain.SmartList
import de.jeanlucmakiola.agendula.domain.TaskFilter
import de.jeanlucmakiola.agendula.domain.UpcomingRange
import de.jeanlucmakiola.agendula.domain.testTask
import org.junit.jupiter.api.Test
import java.time.ZoneId
import kotlin.time.Instant
class SettingsVisibilityTest {
@@ -20,4 +23,17 @@ class SettingsVisibilityTest {
fun `the hidden list still shows its own tasks`() {
assertThat(settings.visibleIn(TaskFilter.OfList(2), tasks)).isEqualTo(tasks)
}
@Test
fun `upcoming leaves out what is due past its range`() {
val zone = ZoneId.of("UTC")
val dayMs = 86_400_000L
val now = Instant.fromEpochMilliseconds(1000 * dayMs)
val near = testTask(id = 1, due = Instant.fromEpochMilliseconds(1005 * dayMs))
val far = testTask(id = 2, due = Instant.fromEpochMilliseconds(1100 * dayMs))
val limited = Settings(upcomingRange = UpcomingRange.TWO_WEEKS)
val upcoming = limited.visibleIn(TaskFilter.Smart(SmartList.UPCOMING), listOf(near, far), now, zone)
assertThat(upcoming.map { it.taskId }).containsExactly(1L)
assertThat(limited.visibleIn(TaskFilter.Smart(SmartList.ALL), listOf(near, far), now, zone)).hasSize(2)
}
}
@@ -0,0 +1,52 @@
package de.jeanlucmakiola.agendula.domain
import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test
import java.time.LocalDate
import java.time.ZoneId
import kotlin.time.Instant
class UpcomingRangeTest {
private val zone = ZoneId.of("Europe/Berlin")
private val today = LocalDate.of(2026, 3, 10)
private val now = at(today, hour = 9)
private fun at(date: LocalDate, hour: Int = 0): Instant =
Instant.fromEpochMilliseconds(date.atTime(hour, 0).atZone(zone).toInstant().toEpochMilli())
private fun allDay(date: LocalDate): Instant =
Instant.fromEpochMilliseconds(date.atStartOfDay(ZoneId.of("UTC")).toInstant().toEpochMilli())
@Test
fun `all has no limit`() {
assertThat(UpcomingRange.ALL.lastDay(today)).isNull()
assertThat(UpcomingRange.ALL.includes(testTask(due = at(today.plusYears(5))), now, zone)).isTrue()
}
@Test
fun `two weeks keeps the last day and drops the one after`() {
val range = UpcomingRange.TWO_WEEKS
assertThat(range.includes(testTask(due = at(today.plusDays(14), hour = 23)), now, zone)).isTrue()
assertThat(range.includes(testTask(due = at(today.plusDays(15))), now, zone)).isFalse()
}
@Test
fun `an all-day due is read as its calendar date`() {
val range = UpcomingRange.WEEK
assertThat(range.includes(testTask(due = allDay(today.plusDays(7)), isAllDay = true), now, zone)).isTrue()
assertThat(range.includes(testTask(due = allDay(today.plusDays(8)), isAllDay = true), now, zone)).isFalse()
}
@Test
fun `months follow the calendar`() {
assertThat(UpcomingRange.MONTH.lastDay(LocalDate.of(2026, 1, 31))).isEqualTo(LocalDate.of(2026, 2, 28))
assertThat(UpcomingRange.THREE_MONTHS.lastDay(today)).isEqualTo(LocalDate.of(2026, 6, 10))
assertThat(UpcomingRange.YEAR.lastDay(today)).isEqualTo(LocalDate.of(2027, 3, 10))
}
@Test
fun `an undated task is never cut`() {
assertThat(UpcomingRange.WEEK.includes(testTask(), now, zone)).isTrue()
}
}