Share one weekday-and-date format between the date picker and the day groups (#71)

This commit is contained in:
makiolaj committed 2026-11-20 18:07:11 +01:00
1 parent 9cb5ad2942
commit 12dd6b2dec
3 files changed
+30 -19

No files matched your search

@@ -0,0 +1,24 @@
package de.jeanlucmakiola.agendula.ui.common
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import de.jeanlucmakiola.floret.locale.currentLocale
import de.jeanlucmakiola.floret.locale.localizedDateFormatter
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale
/** Weekday and date, with the year only when it isn't today's; [long] spells weekday and month out. */
class DayFormat(locale: Locale, long: Boolean = false) {
private val thisYear: DateTimeFormatter = localizedDateFormatter(locale, if (long) "EEEEMMMMd" else "EEEdMMM")
private val otherYear: DateTimeFormatter = localizedDateFormatter(locale, if (long) "EEEEMMMMdy" else "EEEdMMMyyyy")
fun format(date: LocalDate, today: LocalDate): String = date.format(if (date.year == today.year) thisYear else otherYear)
}
/** A [DayFormat] in the current locale. */
@Composable
fun rememberDayFormat(long: Boolean = false): DayFormat {
val locale = currentLocale()
return remember(locale, long) { DayFormat(locale, long) }
}
@@ -21,18 +21,16 @@ import de.jeanlucmakiola.agendula.ui.common.DatePickerAlert
import de.jeanlucmakiola.agendula.ui.common.LocalUse24HourFormat
import de.jeanlucmakiola.agendula.ui.common.TimePickerAlert
import de.jeanlucmakiola.agendula.ui.common.formatMinuteOfDay
import de.jeanlucmakiola.agendula.ui.common.rememberDayFormat
import de.jeanlucmakiola.agendula.ui.common.toLocalTime
import de.jeanlucmakiola.floret.components.FullScreenPicker
import de.jeanlucmakiola.floret.components.GroupedRow
import de.jeanlucmakiola.floret.components.GroupedSectionHeader
import de.jeanlucmakiola.floret.components.positionOf
import de.jeanlucmakiola.floret.locale.currentLocale
import de.jeanlucmakiola.floret.locale.localizedDateFormatter
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.util.Locale
import kotlin.time.Instant
/**
@@ -72,8 +70,8 @@ internal fun DateTimePickerSheet(
val today = LocalDate.now()
val is24Hour = LocalUse24HourFormat.current
val locale = currentLocale()
val dayFormats = remember(locale) { DayFormats(locale) }
fun dayLabel(d: LocalDate) = d.format(if (d.year == today.year) dayFormats.thisYear else dayFormats.otherYear)
val dayFormat = rememberDayFormat()
fun dayLabel(d: LocalDate) = dayFormat.format(d, today)
fun timeLabel(t: LocalTime) = formatMinuteOfDay(t.hour * 60 + t.minute, is24Hour, locale)
fun stagedTime(d: LocalDate) = defaultPickerTime(d, LocalDateTime.now(), notBefore, notAfter)
@@ -187,12 +185,6 @@ internal fun DateTimePickerSheet(
}
}
/** Weekday + day + month, with the year only when it isn't the current one. */
private class DayFormats(locale: Locale) {
val thisYear: DateTimeFormatter = localizedDateFormatter(locale, "EEEdMMM")
val otherYear: DateTimeFormatter = localizedDateFormatter(locale, "EEEdMMMyyyy")
}
private val QuickDate.labelRes: Int
get() = when (this) {
QuickDate.Today -> R.string.picker_date_today
@@ -113,8 +113,6 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import de.jeanlucmakiola.agendula.R
import de.jeanlucmakiola.floret.locale.currentLocale
import de.jeanlucmakiola.floret.locale.localizedDateFormatter
import de.jeanlucmakiola.agendula.domain.Priority
import de.jeanlucmakiola.agendula.domain.SmartList
import de.jeanlucmakiola.agendula.domain.Task
@@ -127,6 +125,7 @@ import de.jeanlucmakiola.agendula.domain.TaskGrouping
import de.jeanlucmakiola.agendula.domain.TaskGroups
import de.jeanlucmakiola.agendula.ui.common.LocalUse24HourFormat
import de.jeanlucmakiola.agendula.ui.common.SyncPullBox
import de.jeanlucmakiola.agendula.ui.common.rememberDayFormat
import de.jeanlucmakiola.agendula.ui.common.ignoringEdgeSwipes
import de.jeanlucmakiola.agendula.ui.common.priorityAccent
import de.jeanlucmakiola.agendula.ui.lists.ListEditorSheet
@@ -1493,13 +1492,9 @@ private fun dayLabel(date: LocalDate, todayStart: kotlin.time.Instant): String {
val today = remember(todayStart) {
java.time.Instant.ofEpochMilli(todayStart.toEpochMilliseconds()).atZone(ZoneId.systemDefault()).toLocalDate()
}
val format = rememberDayFormat(long = true)
if (date == today.plusDays(1)) return stringResource(R.string.group_tomorrow)
val locale = currentLocale()
val sameYear = date.year == today.year
val formatter = remember(locale, sameYear) {
localizedDateFormatter(locale, if (sameYear) "EEEEMMMMd" else "EEEEMMMMdy")
}
return date.format(formatter)
return format.format(date, today)
}
private fun groupingLabels(grouping: TaskGrouping): Pair<Int, Int> = when (grouping) {