Read a due without a time on its own date in the Up Next widget (#101)

This commit is contained in:
makiolaj committed 2026-11-20 18:07:11 +01:00
1 parent 12dd6b2dec
commit efa71cdb3c
2 files changed
+23 -3

No files matched your search

@@ -44,6 +44,7 @@ import androidx.glance.text.FontWeight
import androidx.glance.text.Text
import androidx.glance.text.TextDecoration
import androidx.glance.text.TextStyle
import de.jeanlucmakiola.agendula.domain.calendarDate
import de.jeanlucmakiola.agendula.MainActivity
import de.jeanlucmakiola.agendula.R
import de.jeanlucmakiola.agendula.ui.theme.AgendulaDarkFallback
@@ -63,10 +64,10 @@ import de.jeanlucmakiola.floret.time.formatTime
import de.jeanlucmakiola.floret.glance.R as GlanceR
import kotlinx.coroutines.flow.first
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import java.time.format.DateTimeFormatter
import kotlin.time.Clock
import kotlin.time.Instant
import kotlin.time.toJavaInstant
/**
* "Up next" home-screen widget: the single open task with the nearest due
@@ -356,8 +357,9 @@ private fun completeTask(row: WidgetTaskRow): Action =
internal fun upNextHero(context: Context, row: WidgetTaskRow, use24Hour: Boolean): String {
val due = row.due ?: return ""
val locale = context.resources.configuration.locales[0]
// A date without a time is the UTC midnight of its date; the device's zone would read the day before west of UTC.
val day = DateTimeFormatter.ofPattern(DateFormat.getBestDateTimePattern(locale, "EEEd"), locale)
.format(due.toJavaInstant().atZone(ZoneId.systemDefault()))
.format(due.calendarDate(row.isAllDay))
if (row.isAllDay) {
return when (row.dueKind) {
DueKind.TODAY -> context.getString(R.string.smart_today)
@@ -377,7 +379,14 @@ internal fun upNextHero(context: Context, row: WidgetTaskRow, use24Hour: Boolean
*/
internal fun upNextLabel(context: Context, row: WidgetTaskRow, now: Instant): String {
val due = row.due ?: return ""
if (row.isAllDay && !row.isOverdue) return if (row.dueKind == DueKind.LATER) daysUntil(context, due, now) else ""
if (row.isAllDay) {
val days = daysFromToday(due, now)
return when {
days < 0 -> context.resources.getQuantityString(R.plurals.widget_up_next_overdue_days, (-days).toInt(), (-days).toInt())
row.dueKind == DueKind.LATER -> context.resources.getQuantityString(R.plurals.widget_up_next_in_days, days.toInt(), days.toInt())
else -> ""
}
}
if (row.isOverdue) {
val hours = (now - due).inWholeHours
return when {
@@ -403,6 +412,10 @@ private fun daysUntil(context: Context, due: Instant, now: Instant): String {
return context.resources.getQuantityString(R.plurals.widget_up_next_in_days, days, days)
}
/** Whole days from today in [zone] to the date of [due], a date without a time: negative once it has passed. */
internal fun daysFromToday(due: Instant, now: Instant, zone: ZoneId = ZoneId.systemDefault()): Long =
ChronoUnit.DAYS.between(now.calendarDate(allDay = false, zone = zone), due.calendarDate(allDay = true))
private const val HOURS_PER_DAY = 24L
private const val MAX_LATER_ROWS = 10
private val MinTallHeight = 150.dp
@@ -109,6 +109,13 @@ class WidgetStatsTest {
assertThat(upcoming(open, now, today, newYork).map { it.taskId }).containsExactly(2L, 1L).inOrder()
}
@Test
fun `a date without a time counts days from its own date, not from UTC midnight`() {
// Noon in New York; UTC midnight of the 23rd is still the 22nd there.
assertThat(daysFromToday(allDayInstantOf(LocalDate.of(2026, 7, 23)), now, newYork)).isEqualTo(3L)
assertThat(daysFromToday(allDayInstantOf(LocalDate.of(2026, 7, 19)), now, newYork)).isEqualTo(-1L)
}
@Test
fun `upcoming is empty when nothing is dated`() {
assertThat(upcoming(listOf(testTask(due = null)), now, today, newYork)).isEmpty()