Respect smart lists and widgets for the default due date (#107)

This commit is contained in:
makiolaj committed 2026-11-16 19:14:00 +01:00
1 parent aa6182b991
commit 5639124fb0
14 files changed
+141 -55

No files matched your search

@@ -197,15 +197,25 @@ class MainActivity : ComponentActivity() {
/**
* The launcher shortcut's, Quick Settings tile's and a widget's "New
* task". [listId] preset the list when it names exactly one, [dueToday]
* the due date for a Today widget; the data URI keeps each widget's
* PendingIntent apart from the others.
* task". [listId] preset the list when it names exactly one, [filterId]
* or [smartList] what a widget showing one fills in; the data URI keeps
* each widget's PendingIntent apart from the others.
*/
fun newTaskIntent(context: Context, listId: Long? = null, dueToday: Boolean = false): Intent {
val path = listOfNotNull(listId?.toString(), "today".takeIf { dueToday }).joinToString("/")
fun newTaskIntent(
context: Context,
listId: Long? = null,
smartList: SmartList? = null,
filterId: Long? = null,
): Intent {
val path = listOfNotNull(
listId?.toString(),
smartList?.let { if (it == SmartList.TODAY) "today" else it.name.lowercase() },
filterId?.let { "filter$it" },
).joinToString("/")
return Intent(ACTION_NEW_TASK, path.ifEmpty { null }?.let { "agendula://newtask/$it".toUri() }, context, MainActivity::class.java)
.putExtra(EXTRA_PRESET_LIST_ID, listId ?: -1L)
.putExtra(EXTRA_DUE_TODAY, dueToday)
.putExtra(EXTRA_SMART_LIST, smartList?.name)
.putExtra(EXTRA_FILTER_ID, filterId ?: -1L)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
@@ -241,7 +251,11 @@ class MainActivity : ComponentActivity() {
when (intent.action) {
ACTION_NEW_TASK -> return NavRequest.NewTask(
listId = intent.getLongExtra(EXTRA_PRESET_LIST_ID, -1L).takeIf { it > 0L },
dueToday = intent.getBooleanExtra(EXTRA_DUE_TODAY, false),
filterId = intent.getLongExtra(EXTRA_FILTER_ID, -1L).takeIf { it > 0L },
smartList = intent.getStringExtra(EXTRA_SMART_LIST)
?.let { name -> SmartList.entries.firstOrNull { it.name == name } }
// Widgets placed before 1.4 still carry the old flag.
?: SmartList.TODAY.takeIf { intent.getBooleanExtra(EXTRA_DUE_TODAY, false) },
returnHome = true,
)
ACTION_TODAY -> return NavRequest.OpenSmart(SmartList.TODAY)
@@ -12,7 +12,9 @@ import de.jeanlucmakiola.agendula.data.tasks.ProviderResolver
import de.jeanlucmakiola.agendula.data.tasks.StorageMode
import de.jeanlucmakiola.agendula.domain.DefaultDueDate
import de.jeanlucmakiola.agendula.domain.DefaultStartDate
import de.jeanlucmakiola.agendula.domain.NewTaskDates
import de.jeanlucmakiola.agendula.domain.SavedFilter
import de.jeanlucmakiola.agendula.domain.newTaskDates
import de.jeanlucmakiola.agendula.domain.Task
import de.jeanlucmakiola.agendula.domain.TaskFilter
import de.jeanlucmakiola.agendula.domain.TaskFormField
@@ -30,6 +32,8 @@ import de.jeanlucmakiola.floret.reminders.reminderLeadsFor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import java.time.DayOfWeek
import java.time.LocalDate
import kotlin.time.Instant
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton
@@ -151,6 +155,10 @@ data class Settings(
fun hasOwnReminders(listId: Long, allDay: Boolean): Boolean =
listId in (if (allDay) perListAllDayReminderOverride else perListReminderOverride)
/** The dates a new task starts with where nothing presets its due date. */
fun defaultNewTaskDates(today: LocalDate, now: Instant): NewTaskDates =
newTaskDates(defaultDueDate.dateFrom(today), defaultStartDate, today, now)
/** Whether [listId] has its own default reminders for timed or all-day tasks. */
fun hasOwnReminders(listId: Long): Boolean =
hasOwnReminders(listId, allDay = false) || hasOwnReminders(listId, allDay = true)
@@ -30,7 +30,7 @@ data class NewTaskDates(val start: Instant?, val due: Instant?, val isAllDay: Bo
/**
* The dates a new task opens with, given its [dueDate] (a preset or the default)
* and the default [start]. Without a due date only [DefaultStartDate.TODAY]
* applies, as a timed start at [now]. A start never lands after the due date.
* applies, as a timed start at [now]. A start stays between today and the due date.
*/
fun newTaskDates(dueDate: LocalDate?, start: DefaultStartDate, today: LocalDate, now: Instant): NewTaskDates {
if (dueDate == null) {
@@ -42,6 +42,6 @@ fun newTaskDates(dueDate: LocalDate?, start: DefaultStartDate, today: LocalDate,
DefaultStartDate.DUE_DATE -> dueDate
DefaultStartDate.DAY_BEFORE_DUE -> dueDate.minusDays(1)
DefaultStartDate.WEEK_BEFORE_DUE -> dueDate.minusWeeks(1)
}?.let { minOf(it, dueDate) }
}?.let { minOf(maxOf(it, today), dueDate) }
return NewTaskDates(start = startDate?.let(::allDayInstantOf), due = allDayInstantOf(dueDate), isAllDay = true)
}
@@ -68,6 +68,27 @@ fun dueWindowOf(
}
}
/** The window a task due on [date] falls in, seen on [today]. */
fun dueWindowOf(date: LocalDate, today: LocalDate): DueWindow = when {
date < today -> DueWindow.OVERDUE
date == today -> DueWindow.TODAY
else -> DueWindow.UPCOMING
}
/** Whether a task due on [date] shows in a view of these windows; none admits any date. */
fun Set<DueWindow>.admitsDue(date: LocalDate, today: LocalDate): Boolean =
isEmpty() || dueWindowOf(date, today) in this
/** The due windows this smart list shows; empty for the lists that don't go by due date. */
val SmartList.dueWindows: Set<DueWindow>
get() = when (this) {
SmartList.TODAY -> setOf(DueWindow.TODAY)
SmartList.UPCOMING -> setOf(DueWindow.UPCOMING)
SmartList.OVERDUE -> setOf(DueWindow.OVERDUE)
SmartList.NO_DATE -> setOf(DueWindow.NO_DATE)
SmartList.ALL, SmartList.COMPLETED -> emptySet()
}
/** [at], one of this task's dates, as a local instant: an all-day date is read as its calendar date. */
fun Task.wallClock(at: Instant?, zone: ZoneId = ZoneId.systemDefault()): Instant? =
at?.let { if (isAllDay) it.rebasedForAllDay(allDay = false, zone = zone) else it }
@@ -78,16 +99,7 @@ data class NewTaskPreset(
val tags: List<String> = emptyList(),
val dueDate: LocalDate? = null,
val priority: Priority = Priority.NONE,
/** The filter's due windows; empty admits any due date. */
val dueWindows: Set<DueWindow> = emptySet(),
) {
/** Whether a task due on [date] still lands in the filter. */
fun admitsDue(date: LocalDate, today: LocalDate): Boolean = dueWindows.isEmpty() || when {
date < today -> DueWindow.OVERDUE
date == today -> DueWindow.TODAY
else -> DueWindow.UPCOMING
} in dueWindows
}
)
/**
* The preset for a new task in this filter. Due prefers today, then tomorrow;
@@ -106,5 +118,4 @@ fun SavedFilter.newTaskPreset(today: LocalDate): NewTaskPreset = NewTaskPreset(
else -> null
},
priority = minPriority,
dueWindows = due,
)
@@ -23,6 +23,8 @@ import java.time.ZoneId
import de.jeanlucmakiola.agendula.domain.populatedFields
import de.jeanlucmakiola.agendula.domain.htmlToPlainText
import de.jeanlucmakiola.agendula.domain.looksLikeHtml
import de.jeanlucmakiola.agendula.domain.admitsDue
import de.jeanlucmakiola.agendula.domain.dueWindows
import de.jeanlucmakiola.agendula.domain.newTaskDates
import de.jeanlucmakiola.agendula.domain.newTaskPreset
import de.jeanlucmakiola.agendula.domain.normalizeTags
@@ -157,14 +159,18 @@ class TaskEditViewModel @Inject constructor(
/** The reminders the user picked before clearing the task's last date, until a date comes back. */
private val setAside = mutableListOf<SetAside>()
/** Start a fresh task, optionally pre-selecting a list / parent. */
/**
* Start a fresh task, optionally pre-selecting a list / parent. A saved filter
* or [presetSmartList] it is added from presets what keeps it in that view, and
* the default due date only applies where the task would still show there.
*/
fun bindNew(
presetListId: Long? = null,
parentId: Long? = null,
initialTitle: String? = null,
presetTag: String? = null,
presetFilterId: Long? = null,
presetDueToday: Boolean = false,
presetSmartList: SmartList? = null,
) {
if (bound) return
bound = true
@@ -176,12 +182,13 @@ class TaskEditViewModel @Inject constructor(
this@TaskEditViewModel.settings = settings
defaultFields = settings.defaultEditFields
val lists = runCatching { repository.taskLists().first() }.getOrElse { emptyList() }
val preset = presetFilterId?.let { settingsPrefs.savedFilter(it).first() }
?.newTaskPreset(LocalDate.now())
val today = LocalDate.now()
val presetDue = preset?.dueDate ?: today.takeIf { presetDueToday }
val filter = presetFilterId?.let { settingsPrefs.savedFilter(it).first() }
val preset = filter?.newTaskPreset(today)
val presetDue = preset?.dueDate ?: today.takeIf { presetSmartList == SmartList.TODAY }
val viewWindows = filter?.due ?: presetSmartList?.dueWindows.orEmpty()
val dueDate = presetDue
?: settings.defaultDueDate.dateFrom(today)?.takeIf { preset?.admitsDue(it, today) ?: true }
?: settings.defaultDueDate.dateFrom(today)?.takeIf { viewWindows.admitsDue(it, today) }
val dates = newTaskDates(dueDate, settings.defaultStartDate, today, Clock.System.now())
// A read-only or vanished default — or a widget's preset list, if it was
// since deleted or turned read-only — would give the task a list it
@@ -80,7 +80,12 @@ fun AgendulaNavHost(
nav.navigate(Dest.Settings.buildAccount(navRequest.accountId)) { launchSingleTop = true }
is NavRequest.NewTask ->
nav.navigate(
Dest.TaskEdit.buildNew(presetListId = navRequest.listId, title = navRequest.title, dueToday = navRequest.dueToday),
Dest.TaskEdit.buildNew(
presetListId = navRequest.listId,
title = navRequest.title,
filterId = navRequest.filterId,
smartList = navRequest.smartList,
),
)
is NavRequest.OpenSmart -> nav.navigate(Dest.TaskList.build(TaskFilter.Smart(navRequest.list))) {
launchSingleTop = true
@@ -166,7 +171,7 @@ fun AgendulaNavHost(
presetListId = (filter as? TaskFilter.OfList)?.listId,
tag = (filter as? TaskFilter.Tagged)?.tag,
filterId = (filter as? TaskFilter.Saved)?.id,
dueToday = filter == TaskFilter.Smart(SmartList.TODAY),
smartList = (filter as? TaskFilter.Smart)?.list,
),
)
},
@@ -238,7 +243,8 @@ fun AgendulaNavHost(
val title = args?.getString(Dest.TaskEdit.ARG_TITLE)
val tag = args?.getString(Dest.TaskEdit.ARG_TAG)
val filterId = args?.getLong(Dest.TaskEdit.ARG_FILTER_ID)?.takeIf { it != Dest.NO_ID }
val dueToday = args?.getBoolean(Dest.TaskEdit.ARG_DUE_TODAY) ?: false
val smartList = args?.getString(Dest.TaskEdit.ARG_SMART_LIST)
?.let { name -> SmartList.entries.firstOrNull { it.name == name } }
val vm: TaskEditViewModel = hiltViewModel()
LaunchedEffect(taskId, presetListId, parentId) {
if (taskId != Dest.NO_ID) {
@@ -250,7 +256,7 @@ fun AgendulaNavHost(
initialTitle = title,
presetTag = tag,
presetFilterId = filterId,
presetDueToday = dueToday,
presetSmartList = smartList,
)
}
}
@@ -119,7 +119,7 @@ object Dest {
/**
* The create / edit form. `taskId` set ([NO_ID] otherwise) ⇒ edit; for a new
* task, `presetListId` / `parentId` optionally seed the form, and `filterId`
* (or `dueToday`, for the Today list) fills it so the task lands in that view.
* (or `smartList`) fills it so the task lands in that view.
*/
object TaskEdit {
const val ARG_TASK_ID = "taskId"
@@ -129,7 +129,7 @@ object Dest {
const val ARG_TITLE = "title"
const val ARG_TAG = "tag"
const val ARG_FILTER_ID = "filterId"
const val ARG_DUE_TODAY = "dueToday"
const val ARG_SMART_LIST = "smartList"
const val route =
"taskEdit?$ARG_TASK_ID={$ARG_TASK_ID}" +
"&$ARG_PRESET_LIST_ID={$ARG_PRESET_LIST_ID}" +
@@ -138,7 +138,7 @@ object Dest {
"&$ARG_TITLE={$ARG_TITLE}" +
"&$ARG_TAG={$ARG_TAG}" +
"&$ARG_FILTER_ID={$ARG_FILTER_ID}" +
"&$ARG_DUE_TODAY={$ARG_DUE_TODAY}"
"&$ARG_SMART_LIST={$ARG_SMART_LIST}"
val arguments = listOf(
navArgument(ARG_TASK_ID) { type = NavType.LongType; defaultValue = NO_ID },
@@ -148,7 +148,7 @@ object Dest {
navArgument(ARG_TITLE) { type = NavType.StringType; nullable = true; defaultValue = null },
navArgument(ARG_TAG) { type = NavType.StringType; nullable = true; defaultValue = null },
navArgument(ARG_FILTER_ID) { type = NavType.LongType; defaultValue = NO_ID },
navArgument(ARG_DUE_TODAY) { type = NavType.BoolType; defaultValue = false },
navArgument(ARG_SMART_LIST) { type = NavType.StringType; nullable = true; defaultValue = null },
)
fun buildEdit(taskId: Long, occurrenceStart: Long? = null): String =
@@ -161,7 +161,7 @@ object Dest {
title: String? = null,
tag: String? = null,
filterId: Long? = null,
dueToday: Boolean = false,
smartList: SmartList? = null,
): String {
val params = buildList {
if (presetListId != null) add("$ARG_PRESET_LIST_ID=$presetListId")
@@ -169,7 +169,7 @@ object Dest {
if (!title.isNullOrBlank()) add("$ARG_TITLE=${android.net.Uri.encode(title)}")
if (!tag.isNullOrBlank()) add("$ARG_TAG=${android.net.Uri.encode(tag)}")
if (filterId != null) add("$ARG_FILTER_ID=$filterId")
if (dueToday) add("$ARG_DUE_TODAY=true")
if (smartList != null) add("$ARG_SMART_LIST=${smartList.name}")
}
return if (params.isEmpty()) "taskEdit" else "taskEdit?${params.joinToString("&")}"
}
@@ -28,13 +28,14 @@ sealed interface NavRequest {
/**
* A new task, optionally titled with text shared from another app, or
* preset to a list — a widget's own add button, when it shows one list —
* or due today, from a Today widget.
* or to the saved filter or smart list a widget shows.
* [returnHome] leaves the app once the form is closed (#93).
*/
data class NewTask(
val title: String? = null,
val listId: Long? = null,
val dueToday: Boolean = false,
val filterId: Long? = null,
val smartList: SmartList? = null,
val returnHome: Boolean = false,
) : NavRequest
@@ -40,7 +40,9 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import java.time.LocalDate
import javax.inject.Inject
import kotlin.time.Clock
sealed interface TaskListUiState {
data object Loading : TaskListUiState
@@ -314,10 +316,15 @@ class TaskListViewModel @Inject constructor(
}
}
/** Inline "add task" — title only, into [listId]. */
/** 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(TaskForm(title = title, listId = listId)) }
val dates = settingsPrefs.settings.first().defaultNewTaskDates(LocalDate.now(), Clock.System.now())
runCatching {
repository.createTask(
TaskForm(title = title, listId = listId, start = dates.start, due = dates.due, isAllDay = dates.isAllDay),
)
}
}
private val _listWriteFailure = MutableStateFlow<ListWriteFailure?>(null)
@@ -308,7 +308,12 @@ private fun TaskWidgetHeader(title: String, filter: TaskFilter, singleListId: Lo
icon = ImageProvider(GlanceR.drawable.floret_ic_widget_add),
contentDescription = context.getString(R.string.new_task),
action = actionStartActivity(
MainActivity.newTaskIntent(context, singleListId, dueToday = filter == TaskFilter.Smart(SmartList.TODAY)),
MainActivity.newTaskIntent(
context,
singleListId,
smartList = (filter as? TaskFilter.Smart)?.list,
filterId = (filter as? TaskFilter.Saved)?.id,
),
),
)
}
@@ -40,6 +40,7 @@ import androidx.glance.text.TextAlign
import androidx.glance.text.TextStyle
import de.jeanlucmakiola.agendula.MainActivity
import de.jeanlucmakiola.agendula.R
import de.jeanlucmakiola.agendula.domain.SmartList
import de.jeanlucmakiola.agendula.ui.theme.AgendulaDarkFallback
import de.jeanlucmakiola.agendula.ui.theme.AgendulaLightFallback
import de.jeanlucmakiola.floret.glance.FloretGlanceTheme
@@ -100,7 +101,7 @@ private fun TodayWidgetBody(data: WidgetStatsData?, background: WidgetBackground
WidgetHeaderAction(
icon = ImageProvider(GlanceR.drawable.floret_ic_widget_add),
contentDescription = context.getString(R.string.new_task),
action = actionStartActivity(MainActivity.newTaskIntent(context, dueToday = true)),
action = actionStartActivity(MainActivity.newTaskIntent(context, smartList = SmartList.TODAY)),
)
}
Spacer(GlanceModifier.height(HeaderGap))
@@ -30,6 +30,13 @@ class NewTaskDefaultsTest {
.isEqualTo(allDayInstantOf(due.minusDays(1)))
}
@Test
fun `a start counted back from the due date never lands before today`() {
val tomorrow = today.plusDays(1)
assertThat(newTaskDates(tomorrow, DefaultStartDate.WEEK_BEFORE_DUE, today, now).start)
.isEqualTo(allDayInstantOf(today))
}
@Test
fun `a start today never lands after the due date`() {
val pastDue = today.minusDays(3)
@@ -118,14 +118,16 @@ class SavedFilterTest {
}
@Test
fun `a default due date applies only where the filter would still show the task`() {
fun `a due date is admitted only where the view would still show the task`() {
val day = LocalDate.of(2026, 10, 10)
assertThat(empty.newTaskPreset(day).admitsDue(day.plusDays(3), day)).isTrue()
val todayOrUndated = empty.copy(due = setOf(DueWindow.TODAY, DueWindow.NO_DATE)).newTaskPreset(day)
assertThat(empty.due.admitsDue(day.plusDays(3), day)).isTrue()
val todayOrUndated = setOf(DueWindow.TODAY, DueWindow.NO_DATE)
assertThat(todayOrUndated.admitsDue(day, day)).isTrue()
assertThat(todayOrUndated.admitsDue(day.plusDays(1), day)).isFalse()
val overdue = empty.copy(due = setOf(DueWindow.OVERDUE)).newTaskPreset(day)
assertThat(overdue.admitsDue(day, day)).isFalse()
assertThat(overdue.admitsDue(day.minusDays(1), day)).isTrue()
assertThat(setOf(DueWindow.OVERDUE).admitsDue(day.minusDays(1), day)).isTrue()
assertThat(SmartList.NO_DATE.dueWindows.admitsDue(day, day)).isFalse()
assertThat(SmartList.UPCOMING.dueWindows.admitsDue(day, day)).isFalse()
assertThat(SmartList.UPCOMING.dueWindows.admitsDue(day.plusDays(1), day)).isTrue()
assertThat(SmartList.ALL.dueWindows.admitsDue(day, day)).isTrue()
}
}
@@ -13,6 +13,7 @@ import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
import de.jeanlucmakiola.agendula.domain.DefaultDueDate
import de.jeanlucmakiola.agendula.domain.DefaultStartDate
import de.jeanlucmakiola.agendula.domain.Priority
import de.jeanlucmakiola.agendula.domain.SmartList
import de.jeanlucmakiola.agendula.domain.allDayInstantOf
import de.jeanlucmakiola.agendula.domain.Task
import de.jeanlucmakiola.agendula.domain.TaskDetail
@@ -68,22 +69,31 @@ class TaskEditViewModelTest {
setDefaultDueDate(DefaultDueDate.TOMORROW)
setDefaultStartDate(DefaultStartDate.DAY_BEFORE_DUE)
}
val vm = TaskEditViewModel(repository, prefs).apply { bindNew() }
val today = LocalDate.now()
val vm = TaskEditViewModel(repository, prefs)
val today = aroundToday { vm.bindNew() }
val state = vm.state.value
assertThat(state.due).isEqualTo(allDayInstantOf(today.plusDays(1)))
assertThat(state.start).isEqualTo(allDayInstantOf(today))
assertThat(state.due).isIn(today.map { allDayInstantOf(it.plusDays(1)) })
assertThat(state.start).isIn(today.map { allDayInstantOf(it) })
assertThat(state.isAllDay).isTrue()
assertThat(state.isDirty).isFalse()
}
@Test
fun `an explicit due today wins over the default due`() = runTest {
fun `the Today list's due wins over the default due`() = runTest {
val prefs = settingsPrefs().apply { setDefaultDueDate(DefaultDueDate.NEXT_WEEK) }
val vm = TaskEditViewModel(repository, prefs).apply { bindNew(presetDueToday = true) }
val vm = TaskEditViewModel(repository, prefs)
val today = aroundToday { vm.bindNew(presetSmartList = SmartList.TODAY) }
assertThat(vm.state.value.due).isEqualTo(allDayInstantOf(LocalDate.now()))
assertThat(vm.state.value.due).isIn(today.map { allDayInstantOf(it) })
}
@Test
fun `no default due for a task added where it would not show`() = runTest {
val prefs = settingsPrefs().apply { setDefaultDueDate(DefaultDueDate.TOMORROW) }
val vm = TaskEditViewModel(repository, prefs).apply { bindNew(presetSmartList = SmartList.NO_DATE) }
assertThat(vm.state.value.due).isNull()
}
@Test
@@ -249,6 +259,13 @@ class TaskEditViewModelTest {
override fun appLabel(packageName: String): String? = null
}
/** The day before and after [block], which differ only when it runs across midnight. */
private inline fun aroundToday(block: () -> Unit): Set<LocalDate> {
val before = LocalDate.now()
block()
return setOf(before, LocalDate.now())
}
private fun settingsPrefs(initial: Preferences = emptyPreferences()) =
SettingsPrefs(MemoryDataStore(initial), ProviderResolver(NoProviders))