Compare commits

...
Author SHA1 Message Date
makiolaj 70a20b07dc Week numbering setting: Automatic, ISO, US (#493) 2026-11-30 19:22:45 +01:00
10 changed files with 190 additions and 10 deletions

No files matched your search

@@ -49,6 +49,7 @@ import de.jeanlucmakiola.calendula.ui.common.LocalTimelineZoom
import de.jeanlucmakiola.calendula.ui.common.rememberTimelineZoom
import de.jeanlucmakiola.calendula.ui.common.LocalSoftenColors
import de.jeanlucmakiola.calendula.ui.common.LocalUse24HourFormat
import de.jeanlucmakiola.calendula.ui.common.LocalWeekNumbering
import de.jeanlucmakiola.calendula.ui.WidgetNavRequest
import de.jeanlucmakiola.calendula.ui.common.CalendarView
import de.jeanlucmakiola.calendula.ui.detail.EventDetailViewModel.Companion.NO_OCCURRENCE_TIME
@@ -248,6 +249,7 @@ class MainActivity : AppCompatActivity() {
LocalTimelineZoom provides timelineZoom,
LocalMultiDayCount provides settings.multiDayCount,
LocalSoftenColors provides settings.softenColors,
LocalWeekNumbering provides settings.weekNumbering,
) {
RootScreen(
modifier = Modifier.fillMaxSize(),
@@ -28,6 +28,7 @@ internal object SettingsBackupSchema {
SettingsKeys.pastEventDisplay,
SettingsKeys.dimCompletedEvents,
SettingsKeys.showWeekNumbers,
SettingsKeys.weekNumbering,
SettingsKeys.monthShowStartTimes,
SettingsKeys.monthViewStyle,
SettingsKeys.timelineScale,
@@ -90,6 +90,20 @@ sealed interface WeekStartPref {
*/
enum class TimeFormatPref { AUTO, TWELVE_HOUR, TWENTY_FOUR_HOUR }
/**
* How week numbers are counted (#493). AUTO follows the device region; ISO and
* US pin ISO 8601 or the US system. Only the number: rows still start on the
* day [WeekStartPref] picks.
*/
enum class WeekNumberingPref { AUTO, ISO, US }
/** The [WeekFields] that count weeks under this preference; AUTO reads [region]. */
fun WeekNumberingPref.weekFields(region: Locale): WeekFields = when (this) {
WeekNumberingPref.AUTO -> WeekFields.of(region)
WeekNumberingPref.ISO -> WeekFields.ISO
WeekNumberingPref.US -> WeekFields.SUNDAY_START
}
/**
* How the Agenda screen treats events that have already finished today: [SHOW]
* leaves them as-is (the historical behaviour), [DIM] fades them, [HIDE] drops
@@ -171,6 +185,7 @@ class SettingsPrefs @Inject constructor(
val pastEventDisplay: Flow<PastEventDisplay> = prefs.flow(SettingsKeys.pastEventDisplay)
val dimCompletedEvents: Flow<Boolean> = prefs.flow(SettingsKeys.dimCompletedEvents)
val showWeekNumbers: Flow<Boolean> = prefs.flow(SettingsKeys.showWeekNumbers)
val weekNumbering: Flow<WeekNumberingPref> = prefs.flow(SettingsKeys.weekNumbering)
val monthShowStartTimes: Flow<Boolean> = prefs.flow(SettingsKeys.monthShowStartTimes)
val monthViewStyle: Flow<MonthViewStyle> = prefs.flow(SettingsKeys.monthViewStyle)
val timelineScale: Flow<TimelineScale> = prefs.flow(SettingsKeys.timelineScale)
@@ -269,6 +284,7 @@ class SettingsPrefs @Inject constructor(
suspend fun setPastEventDisplay(mode: PastEventDisplay) = prefs.set(SettingsKeys.pastEventDisplay, mode)
suspend fun setDimCompletedEvents(enabled: Boolean) = prefs.set(SettingsKeys.dimCompletedEvents, enabled)
suspend fun setShowWeekNumbers(enabled: Boolean) = prefs.set(SettingsKeys.showWeekNumbers, enabled)
suspend fun setWeekNumbering(pref: WeekNumberingPref) = prefs.set(SettingsKeys.weekNumbering, pref)
suspend fun setMonthShowStartTimes(enabled: Boolean) = prefs.set(SettingsKeys.monthShowStartTimes, enabled)
suspend fun setMonthViewStyle(style: MonthViewStyle) = prefs.set(SettingsKeys.monthViewStyle, style)
suspend fun setTimelineScale(scale: TimelineScale) = prefs.set(SettingsKeys.timelineScale, scale)
@@ -708,11 +724,14 @@ object SettingsKeys {
val dimCompletedEvents: Pref<Boolean> = booleanPref("dim_completed_events", false)
/**
* Whether the Month grid shows the ISO week number in a left gutter (#25).
* Whether the Month grid shows the week number in a left gutter (#25).
* The Week view shows its number unconditionally.
*/
val showWeekNumbers: Pref<Boolean> = booleanPref("show_week_numbers", false)
/** How every week number in the app is counted (#493). */
val weekNumbering: Pref<WeekNumberingPref> = enumPref("week_numbering", WeekNumberingPref.AUTO)
/** Whether Month chips may show the start time before the title when wide enough (#219, #476). */
val monthShowStartTimes: Pref<Boolean> = booleanPref("month_show_start_times", true)
@@ -3,19 +3,34 @@ package de.jeanlucmakiola.calendula.ui.common
import android.content.res.Resources
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import de.jeanlucmakiola.calendula.data.prefs.WeekNumberingPref
import de.jeanlucmakiola.calendula.data.prefs.weekFields
import de.jeanlucmakiola.floret.time.rowWeekNumber
import kotlinx.datetime.LocalDate
import kotlinx.datetime.toJavaLocalDate
import java.time.temporal.WeekFields
import java.util.Locale
/** The week-numbering setting (#493), provided once at the app root. */
val LocalWeekNumbering = staticCompositionLocalOf { WeekNumberingPref.AUTO }
/**
* The week number of the grid row starting on [rowStart], numbered by the
* device region's rules (#493). The region, not the app language: an English
* UI in the Netherlands still numbers weeks the ISO way. Shared by the Month
* gutter and the Week badge so the two always agree.
* The week number of the 7-day row starting on [rowStart], counted by
* [numbering] (AUTO reads [region]). The row takes the week holding most of its
* days: that of its mid-week day in the numbering system (Thursday for ISO,
* Wednesday for US), so any "Week starts on" choice gets a consistent number.
*/
fun rowWeekNumber(rowStart: LocalDate, numbering: WeekNumberingPref, region: Locale): Int =
rowStart.toJavaLocalDate().rowWeekNumber(numbering.weekFields(region))
/**
* [rowWeekNumber] for the composition's numbering setting. AUTO follows the
* device region, not the app language: an English UI in the Netherlands still
* numbers weeks the ISO way. Shared by the Month gutter and the Week badge.
*/
@Composable
internal fun rememberRowWeekNumber(rowStart: LocalDate): Int {
val fields = remember { WeekFields.of(Resources.getSystem().configuration.locales[0]) }
return remember(rowStart, fields) { rowStart.toJavaLocalDate().rowWeekNumber(fields) }
val numbering = LocalWeekNumbering.current
val region = remember { Resources.getSystem().configuration.locales[0] }
return remember(rowStart, numbering, region) { rowWeekNumber(rowStart, numbering, region) }
}
@@ -2757,8 +2757,8 @@ private fun monthChipDragModifier(
* Left-gutter calendar-week cell (#25): a full-height tonal pill mirroring the
* day cells' geometry, set apart by the secondaryContainer tint (matching the
* Week view's badge), with the week number centred like a day number. It is
* numbered by the device region from the row's mid-week day, as in the Week
* view, so it is right for any first weekday (#493).
* numbered by the week-numbering setting from the row's mid-week day, as in the
* Week view, so it is right for any first weekday (#493).
*/
@Composable
private fun WeekNumberGutter(weekStart: LocalDate, modifier: Modifier = Modifier) {
@@ -9,6 +9,7 @@ import de.jeanlucmakiola.calendula.data.prefs.PastEventDisplay
import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs
import de.jeanlucmakiola.floret.prefs.ThemeMode
import de.jeanlucmakiola.calendula.data.prefs.TimeFormatPref
import de.jeanlucmakiola.calendula.data.prefs.WeekNumberingPref
import de.jeanlucmakiola.calendula.data.prefs.WeekStartPref
import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.domain.EventFormField
@@ -46,6 +47,8 @@ data class SettingsUiState(
val dimCompletedEvents: Boolean = false,
/** Whether the Month grid shows calendar-week numbers in a left gutter (#25). */
val showWeekNumbers: Boolean = false,
/** How week numbers are counted in every view (#493). */
val weekNumbering: WeekNumberingPref = WeekNumberingPref.AUTO,
/** Whether Month chips may show start times before their titles (#476). */
val monthShowStartTimes: Boolean = true,
/** Whether the jump-to-today control sits in the top bar instead of the FAB (#60). */
@@ -42,6 +42,7 @@ import de.jeanlucmakiola.floret.prefs.get
import de.jeanlucmakiola.calendula.data.prefs.SpecialDatesStalledReason
import de.jeanlucmakiola.floret.prefs.ThemeMode
import de.jeanlucmakiola.calendula.data.prefs.TimeFormatPref
import de.jeanlucmakiola.calendula.data.prefs.WeekNumberingPref
import de.jeanlucmakiola.calendula.data.prefs.WeekStartPref
import de.jeanlucmakiola.calendula.data.reminders.ReminderChannelStatus
import de.jeanlucmakiola.calendula.data.reminders.ReminderChannels
@@ -142,6 +143,7 @@ class SettingsViewModel @Inject constructor(
pastEventDisplay = this[SettingsKeys.pastEventDisplay],
dimCompletedEvents = this[SettingsKeys.dimCompletedEvents],
showWeekNumbers = this[SettingsKeys.showWeekNumbers],
weekNumbering = this[SettingsKeys.weekNumbering],
monthShowStartTimes = this[SettingsKeys.monthShowStartTimes],
todayButtonInToolbar = this[SettingsKeys.todayButtonInToolbar],
dragToReschedule = this[SettingsKeys.dragToReschedule],
@@ -345,6 +347,8 @@ class SettingsViewModel @Inject constructor(
fun setShowWeekNumbers(enabled: Boolean) = save { setShowWeekNumbers(enabled) }
fun setWeekNumbering(pref: WeekNumberingPref) = save { setWeekNumbering(pref) }
fun setMonthShowStartTimes(enabled: Boolean) = save { setMonthShowStartTimes(enabled) }
fun setTodayButtonInToolbar(enabled: Boolean) = save { setTodayButtonInToolbar(enabled) }
@@ -1,5 +1,6 @@
package de.jeanlucmakiola.calendula.ui.settings
import android.content.res.Resources
import android.text.format.DateFormat
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Box
@@ -28,7 +29,9 @@ import de.jeanlucmakiola.calendula.R
import de.jeanlucmakiola.calendula.data.prefs.PastEventDisplay
import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs
import de.jeanlucmakiola.calendula.data.prefs.TimeFormatPref
import de.jeanlucmakiola.calendula.data.prefs.WeekNumberingPref
import de.jeanlucmakiola.calendula.data.prefs.WeekStartPref
import de.jeanlucmakiola.calendula.data.time.currentDate
import de.jeanlucmakiola.calendula.data.prefs.resolveFirstDay
import de.jeanlucmakiola.calendula.ui.common.AgendaRangePicker
import de.jeanlucmakiola.calendula.ui.common.CalendarView
@@ -43,6 +46,8 @@ import de.jeanlucmakiola.calendula.ui.common.formatTimeOfDay
import de.jeanlucmakiola.calendula.ui.common.icon
import de.jeanlucmakiola.calendula.ui.common.label
import de.jeanlucmakiola.calendula.ui.common.labelRes
import de.jeanlucmakiola.calendula.ui.common.rowWeekNumber
import de.jeanlucmakiola.calendula.ui.common.startOfWeek
import de.jeanlucmakiola.calendula.ui.month.labelRes
import de.jeanlucmakiola.floret.components.CollapsingScaffold
import de.jeanlucmakiola.floret.components.GroupedHint
@@ -77,6 +82,7 @@ internal fun ViewsScreen(
var showMonthStyle by remember { mutableStateOf(false) }
var showDefaultView by remember { mutableStateOf(false) }
var showWeekStart by remember { mutableStateOf(false) }
var showWeekNumbering by remember { mutableStateOf(false) }
var showTimeFormat by remember { mutableStateOf(false) }
var showTapOpens by remember { mutableStateOf(false) }
var showPastEvents by remember { mutableStateOf(false) }
@@ -109,6 +115,12 @@ internal fun ViewsScreen(
position = Position.Middle,
onClick = { showWeekStart = true },
)
GroupedRow(
title = stringResource(R.string.settings_week_numbering),
summary = weekNumberingLabel(state.weekNumbering),
position = Position.Middle,
onClick = { showWeekNumbering = true },
)
GroupedRow(
title = stringResource(R.string.settings_time_format),
summary = timeFormatLabel(state.timeFormat),
@@ -339,6 +351,25 @@ internal fun ViewsScreen(
onDismiss = { showWeekStart = false },
)
}
if (showWeekNumbering) {
val firstDay = state.weekStart.resolveFirstDay(currentLocale())
val rowStart = remember(firstDay) { currentDate().startOfWeek(firstDay) }
val region = remember { Resources.getSystem().configuration.locales[0] }
OptionPicker(
title = stringResource(R.string.settings_week_numbering),
header = { GroupedHint(stringResource(R.string.settings_week_numbering_hint), verticalPadding = 8.dp) },
predictiveBack = true,
options = WeekNumberingPref.entries,
selected = state.weekNumbering,
label = { weekNumberingLabel(it) },
// Each option shows the number it gives this week, so the difference is visible.
summary = {
stringResource(R.string.settings_week_numbering_this_week, rowWeekNumber(rowStart, it, region))
},
onSelect = viewModel::setWeekNumbering,
onDismiss = { showWeekNumbering = false },
)
}
if (showTimeFormat) {
val locale = currentLocale()
val systemIs24Hour = DateFormat.is24HourFormat(LocalContext.current)
@@ -507,6 +538,15 @@ private fun ViewRow(
)
}
@Composable
private fun weekNumberingLabel(pref: WeekNumberingPref): String = stringResource(
when (pref) {
WeekNumberingPref.AUTO -> R.string.settings_week_numbering_auto
WeekNumberingPref.ISO -> R.string.settings_week_numbering_iso
WeekNumberingPref.US -> R.string.settings_week_numbering_us
},
)
/** Picker options: "Follow system" first, then Monday…Sunday in ISO order. */
private val WEEK_START_OPTIONS: List<WeekStartPref> =
listOf(WeekStartPref.Auto) + DayOfWeek.entries.map { WeekStartPref.Day(it) }
+7
View File
@@ -614,6 +614,12 @@
<string name="settings_week_start_auto_summary">Currently %1$s</string>
<string name="settings_week_numbers">Week numbers</string>
<string name="settings_week_numbers_summary">Show calendar-week numbers in month view</string>
<string name="settings_week_numbering">Week numbering</string>
<string name="settings_week_numbering_auto">Automatic</string>
<string name="settings_week_numbering_iso">ISO 8601</string>
<string name="settings_week_numbering_us">US</string>
<!-- %1$d is the number this option gives the current week, e.g. "This week: 49". -->
<string name="settings_week_numbering_this_week">This week: %1$d</string>
<string name="settings_month_start_times">Start times</string>
<string name="settings_month_start_times_summary">Show the start time before an event\'s title when there is room</string>
<string name="settings_today_toolbar">Today button in toolbar</string>
@@ -879,6 +885,7 @@
<!-- Picker descriptions (#69) -->
<string name="settings_default_view_hint">The view Calendula opens on when you start it.</string>
<string name="settings_week_start_hint">The day every week begins on, in all views and widgets.</string>
<string name="settings_week_numbering_hint">How week numbers are counted. Automatic follows your region.</string>
<string name="settings_time_format_hint">How times are written throughout the app. Automatic follows your system setting.</string>
<string name="settings_past_events_hint">What the agenda does with events that have already ended.</string>
<string name="settings_dynamic_color_summary">Take the app\'s colours from your wallpaper.</string>
@@ -0,0 +1,89 @@
package de.jeanlucmakiola.calendula.ui.common
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.calendula.data.prefs.WeekNumberingPref
import de.jeanlucmakiola.calendula.data.prefs.weekFields
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.LocalDate
import kotlinx.datetime.minus
import kotlinx.datetime.plus
import kotlinx.datetime.toJavaLocalDate
import org.junit.jupiter.api.Test
import java.util.Locale
class WeekNumberTest {
private val portugal = Locale.forLanguageTag("pt-PT")
private val germany = Locale.GERMANY
@Test
fun `1 January 2026 is week 1 under ISO for every row start`() {
val rows = (0..6).map { LocalDate(2026, 1, 1).minusDays(it) }
rows.forEach { start ->
assertThat(rowWeekNumber(start, WeekNumberingPref.ISO, portugal)).isEqualTo(1)
}
}
@Test
fun `automatic numbering in Portugal puts 1 January 2026 in week 53`() {
assertThat(rowWeekNumber(LocalDate(2025, 12, 28), WeekNumberingPref.AUTO, portugal)).isEqualTo(53)
assertThat(rowWeekNumber(LocalDate(2025, 12, 29), WeekNumberingPref.AUTO, portugal)).isEqualTo(53)
}
@Test
fun `automatic numbering in Germany matches ISO`() {
assertThat(WeekNumberingPref.AUTO.weekFields(germany)).isEqualTo(WeekNumberingPref.ISO.weekFields(germany))
}
@Test
fun `US numbering puts the week holding 1 January first`() {
assertThat(rowWeekNumber(LocalDate(2025, 12, 28), WeekNumberingPref.US, germany)).isEqualTo(1)
assertThat(rowWeekNumber(LocalDate(2026, 12, 27), WeekNumberingPref.US, germany)).isEqualTo(1)
assertThat(rowWeekNumber(LocalDate(2026, 12, 27), WeekNumberingPref.ISO, germany)).isEqualTo(53)
}
@Test
fun `US numbering with Monday rows`() {
// Mon 2025-12-29 .. Sun 2026-01-04: its Wednesday is in the US week holding 1 January.
assertThat(rowWeekNumber(LocalDate(2025, 12, 29), WeekNumberingPref.US, germany)).isEqualTo(1)
assertThat(rowWeekNumber(LocalDate(2026, 11, 30), WeekNumberingPref.US, germany)).isEqualTo(49)
}
@Test
fun `numbering ignores the region unless automatic`() {
val start = LocalDate(2025, 12, 28)
assertThat(rowWeekNumber(start, WeekNumberingPref.ISO, portugal))
.isEqualTo(rowWeekNumber(start, WeekNumberingPref.ISO, Locale.US))
assertThat(rowWeekNumber(start, WeekNumberingPref.US, portugal))
.isEqualTo(rowWeekNumber(start, WeekNumberingPref.US, Locale.US))
}
@Test
fun `ISO numbering with Sunday and Saturday rows takes the week holding most of the row`() {
// Sun 01-04 .. Sat 01-10: six days in ISO week 2.
assertThat(rowWeekNumber(LocalDate(2026, 1, 4), WeekNumberingPref.ISO, germany)).isEqualTo(2)
// Sat 01-03 .. Fri 01-09: Monday to Friday are ISO week 2.
assertThat(rowWeekNumber(LocalDate(2026, 1, 3), WeekNumberingPref.ISO, germany)).isEqualTo(2)
// Fri 01-02 .. Thu 01-08: Friday to Sunday are week 1, Monday to Thursday week 2.
assertThat(rowWeekNumber(LocalDate(2026, 1, 2), WeekNumberingPref.ISO, germany)).isEqualTo(2)
}
@Test
fun `every row gets the number of the week holding at least four of its days`() {
val regions = listOf(germany, Locale.US, portugal)
var start = LocalDate(2019, 12, 20)
while (start < LocalDate(2028, 1, 10)) {
for (numbering in WeekNumberingPref.entries) for (region in regions) {
val fields = numbering.weekFields(region)
val days = (0..6).map { start.plusDays(it).toJavaLocalDate() }
val majority = days.groupingBy { it.get(fields.weekOfWeekBasedYear()) }.eachCount()
.filterValues { it >= 4 }.keys.single()
assertThat(rowWeekNumber(start, numbering, region)).isEqualTo(majority)
}
start = start.plusDays(1)
}
}
private fun LocalDate.minusDays(days: Int) = minus(days, DateTimeUnit.DAY)
private fun LocalDate.plusDays(days: Int) = plus(days, DateTimeUnit.DAY)
}