Monthly repeats by weekday position (#520)
This commit is contained in:
1 parent
851df26c95
commit
7fa30ab155
8 files changed
+399
-30
No files matched your search
@@ -3,23 +3,26 @@ package de.jeanlucmakiola.calendula.domain
|
||||
import de.jeanlucmakiola.calendula.domain.ics.parseBasicDate
|
||||
import de.jeanlucmakiola.calendula.domain.ics.parseBasicDateTime
|
||||
import de.jeanlucmakiola.calendula.domain.ics.toUtcBasic
|
||||
import kotlinx.datetime.DateTimeUnit
|
||||
import kotlinx.datetime.DayOfWeek
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.LocalTime
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.isoDayNumber
|
||||
import kotlinx.datetime.minus
|
||||
import kotlinx.datetime.number
|
||||
import kotlinx.datetime.plus
|
||||
import kotlinx.datetime.toInstant
|
||||
import kotlinx.datetime.toLocalDateTime
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* The recurrence shapes the simple picker can express (v1.3): a frequency,
|
||||
* an interval, weekly weekday picks, and an optional end. Anything beyond
|
||||
* that (ordinal BYDAY like "2TH", BYMONTHDAY, EXDATE rules, …) stays a raw
|
||||
* RRULE string the picker shows as "custom" and leaves untouched unless the
|
||||
* user replaces it.
|
||||
* an interval, weekly weekday picks, a monthly weekday position (#520) and an
|
||||
* optional end. Anything beyond that (several ordinal days, BYMONTHDAY,
|
||||
* BYSETPOS, …) stays a raw RRULE string the picker shows as "custom" and
|
||||
* leaves untouched unless the user replaces it.
|
||||
*/
|
||||
data class SimpleRecurrence(
|
||||
val freq: RecurrenceFreq,
|
||||
@@ -30,8 +33,66 @@ data class SimpleRecurrence(
|
||||
* no BYDAY part — the provider derives the day from DTSTART.
|
||||
*/
|
||||
val byDays: Set<DayOfWeek> = emptySet(),
|
||||
/**
|
||||
* Monthly only: repeat on the n-th (or last) weekday of the month
|
||||
* (RRULE `BYDAY=2TU`). Null repeats on DTSTART's day of the month.
|
||||
*/
|
||||
val monthlyWeekday: MonthlyWeekday? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* "The [ordinal]-th [day] of the month": [ordinal] is 1..5, or [LAST] (-1).
|
||||
* A fifth weekday only exists in some months; those without one are skipped.
|
||||
*/
|
||||
data class MonthlyWeekday(val ordinal: Int, val day: DayOfWeek) {
|
||||
init {
|
||||
require(ordinal in 1..MAX_ORDINAL || ordinal == LAST) { "ordinal $ordinal" }
|
||||
}
|
||||
|
||||
/** The RRULE BYDAY token, e.g. "2TU" or "-1SA". */
|
||||
val token: String get() = "$ordinal${RRULE_DAY_CODES.getValue(day)}"
|
||||
|
||||
/** This weekday's date in [year]/[month], or null when the month has no such day. */
|
||||
fun dateIn(year: Int, month: Int): LocalDate? {
|
||||
val first = LocalDate(year, month, 1)
|
||||
val offset = (day.isoDayNumber - first.dayOfWeek.isoDayNumber + 7) % 7
|
||||
val firstMatch = first.day + offset
|
||||
val lastDay = first.plus(1, DateTimeUnit.MONTH).minus(1, DateTimeUnit.DAY).day
|
||||
val dom = if (ordinal == LAST) {
|
||||
firstMatch + (lastDay - firstMatch) / 7 * 7
|
||||
} else {
|
||||
firstMatch + (ordinal - 1) * 7
|
||||
}
|
||||
return if (dom <= lastDay) LocalDate(year, month, dom) else null
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val LAST = -1
|
||||
const val MAX_ORDINAL = 5
|
||||
|
||||
/** Parse a single BYDAY item with an ordinal ("2TU", "+3MO", "-1SA"); else null. */
|
||||
fun parse(token: String): MonthlyWeekday? {
|
||||
val match = TOKEN.matchEntire(token.trim().uppercase()) ?: return null
|
||||
val ordinal = match.groupValues[1].removePrefix("+").toIntOrNull() ?: return null
|
||||
if (ordinal !in 1..MAX_ORDINAL && ordinal != LAST) return null
|
||||
val day = rruleDay(match.groupValues[2]) ?: return null
|
||||
return MonthlyWeekday(ordinal, day)
|
||||
}
|
||||
|
||||
/**
|
||||
* The weekday positions [date] stands at, to offer as prefills: its
|
||||
* ordinal within the month, plus "last" when it falls in the final
|
||||
* seven days.
|
||||
*/
|
||||
fun optionsFor(date: LocalDate): List<MonthlyWeekday> = listOfNotNull(
|
||||
MonthlyWeekday((date.day - 1) / 7 + 1, date.dayOfWeek),
|
||||
MonthlyWeekday(LAST, date.dayOfWeek).takeIf { date.plus(7, DateTimeUnit.DAY).month != date.month },
|
||||
)
|
||||
|
||||
private val TOKEN = Regex("""([+-]?\d)(MO|TU|WE|TH|FR|SA|SU)""")
|
||||
}
|
||||
}
|
||||
|
||||
enum class RecurrenceFreq {
|
||||
Daily,
|
||||
Weekly,
|
||||
@@ -69,12 +130,18 @@ fun parseSimpleRecurrence(
|
||||
?: return null
|
||||
val interval = parts["INTERVAL"]?.let { it.toIntOrNull()?.takeIf { n -> n >= 1 } ?: return null } ?: 1
|
||||
|
||||
// BYDAY is simple only as plain weekday picks on a weekly rule; ordinal
|
||||
// forms ("2TH" = second Thursday) and BYDAY on other frequencies are not.
|
||||
val byDays = parts["BYDAY"]?.let { raw ->
|
||||
if (freq != RecurrenceFreq.Weekly) return null
|
||||
raw.split(',').map { token -> rruleDay(token.trim()) ?: return null }.toSet()
|
||||
} ?: emptySet()
|
||||
// BYDAY is simple as plain weekday picks on a weekly rule, or as one
|
||||
// ordinal weekday ("2TH" = second Thursday) on a monthly rule.
|
||||
var byDays = emptySet<DayOfWeek>()
|
||||
var monthlyWeekday: MonthlyWeekday? = null
|
||||
parts["BYDAY"]?.let { raw ->
|
||||
when (freq) {
|
||||
RecurrenceFreq.Weekly ->
|
||||
byDays = raw.split(',').map { token -> rruleDay(token.trim()) ?: return null }.toSet()
|
||||
RecurrenceFreq.Monthly -> monthlyWeekday = MonthlyWeekday.parse(raw) ?: return null
|
||||
else -> return null
|
||||
}
|
||||
}
|
||||
|
||||
val until = parts["UNTIL"]
|
||||
val count = parts["COUNT"]
|
||||
@@ -84,7 +151,7 @@ fun parseSimpleRecurrence(
|
||||
count != null -> RecurrenceEnd.Count(count.toIntOrNull()?.takeIf { it >= 1 } ?: return null)
|
||||
else -> RecurrenceEnd.Never
|
||||
}
|
||||
return SimpleRecurrence(freq, interval, end, byDays)
|
||||
return SimpleRecurrence(freq, interval, end, byDays, monthlyWeekday)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,6 +180,10 @@ fun SimpleRecurrence.toRRule(zone: TimeZone = TimeZone.currentSystemDefault()):
|
||||
.joinToString(",") { RRULE_DAY_CODES.getValue(it) },
|
||||
)
|
||||
}
|
||||
if (freq == RecurrenceFreq.Monthly && monthlyWeekday != null) {
|
||||
append(";BYDAY=")
|
||||
append(monthlyWeekday.token)
|
||||
}
|
||||
when (val e = end) {
|
||||
RecurrenceEnd.Never -> Unit
|
||||
is RecurrenceEnd.Until -> {
|
||||
@@ -148,7 +219,7 @@ internal val RRULE_DAY_CODES: Map<DayOfWeek, String> = mapOf(
|
||||
)
|
||||
|
||||
/** Exact two-letter BYDAY token → weekday; ordinal forms ("2TH") return null. */
|
||||
private fun rruleDay(token: String): DayOfWeek? =
|
||||
internal fun rruleDay(token: String): DayOfWeek? =
|
||||
RRULE_DAY_CODES.entries.firstOrNull { it.value == token.uppercase() }?.key
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,16 +54,23 @@ private fun SimpleRecurrence.occurrencesInPeriod(period: Int, start: LocalDate):
|
||||
when (freq) {
|
||||
RecurrenceFreq.Daily -> listOf(start.plus(period * interval, DateTimeUnit.DAY))
|
||||
RecurrenceFreq.Weekly -> weeklyOccurrences(period, start)
|
||||
RecurrenceFreq.Monthly -> {
|
||||
val month = start.plus(period * interval, DateTimeUnit.MONTH)
|
||||
// plus() clamps into the shorter month, but the rule skips such a
|
||||
// period — so a clamped date means "not this month".
|
||||
listOfNotNull(dateOrNull(month.year, month.month.number, start.day))
|
||||
}
|
||||
RecurrenceFreq.Monthly -> monthlyOccurrences(period, start)
|
||||
RecurrenceFreq.Yearly ->
|
||||
listOfNotNull(dateOrNull(start.year + period * interval, start.month.number, start.day))
|
||||
}
|
||||
|
||||
/** One monthly period: the weekday position if set, else the start's day of the month. */
|
||||
private fun SimpleRecurrence.monthlyOccurrences(period: Int, start: LocalDate): List<LocalDate> {
|
||||
monthlyWeekday?.let { pos ->
|
||||
val month = LocalDate(start.year, start.month, 1).plus(period * interval, DateTimeUnit.MONTH)
|
||||
return listOfNotNull(pos.dateIn(month.year, month.month.number))
|
||||
}
|
||||
val month = start.plus(period * interval, DateTimeUnit.MONTH)
|
||||
// plus() clamps into the shorter month, but the rule skips such a
|
||||
// period — so a clamped date means "not this month".
|
||||
return listOfNotNull(dateOrNull(month.year, month.month.number, start.day))
|
||||
}
|
||||
|
||||
/**
|
||||
* One weekly block: every picked weekday inside the week that begins
|
||||
* `period * interval` weeks after the start's own week, in weekday order. With
|
||||
|
||||
@@ -2,18 +2,21 @@ package de.jeanlucmakiola.calendula.ui.common
|
||||
|
||||
import android.icu.text.ListFormatter
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalResources
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import androidx.compose.ui.text.SpanStyle
|
||||
import androidx.compose.ui.text.buildAnnotatedString
|
||||
import androidx.compose.ui.text.font.FontStyle
|
||||
import de.jeanlucmakiola.calendula.R
|
||||
import de.jeanlucmakiola.calendula.domain.MonthlyWeekday
|
||||
import de.jeanlucmakiola.calendula.domain.SimpleRecurrence
|
||||
import de.jeanlucmakiola.calendula.domain.occurrencesSpanYears
|
||||
import de.jeanlucmakiola.calendula.domain.rruleParts
|
||||
import de.jeanlucmakiola.calendula.domain.ics.parseBasicDate
|
||||
import de.jeanlucmakiola.calendula.domain.upcomingOccurrences
|
||||
import de.jeanlucmakiola.floret.locale.localizedDateFormatter
|
||||
import kotlinx.datetime.toJavaDayOfWeek
|
||||
import kotlinx.datetime.toJavaLocalDate
|
||||
import java.time.DayOfWeek
|
||||
import java.time.LocalDate
|
||||
@@ -28,12 +31,23 @@ import java.util.Locale
|
||||
/**
|
||||
* Humanise an RFC 5545 RRULE into a localized phrase, e.g.
|
||||
* "Every week on Tue and Thu until 31 Dec 2026" or "Every day, 10 times".
|
||||
* Falls back to a generic label for rules we don't render in full (ordinal
|
||||
* monthly/yearly BYDAY, etc.). Shared by the detail screen and the edit
|
||||
* Falls back to a generic label for rules we don't render in full (yearly
|
||||
* or multi-day ordinal BYDAY, etc.). Shared by the detail screen and the edit
|
||||
* form's repeat card.
|
||||
*/
|
||||
@Composable
|
||||
fun recurrenceText(rrule: String, locale: Locale): AnnotatedString {
|
||||
val resources = LocalResources.current
|
||||
return describeRecurrence(rrule, locale) { id, args -> resources.getString(id, *args) }
|
||||
}
|
||||
|
||||
/** [recurrenceText] without Compose; [string] resolves a string resource with format args. */
|
||||
internal fun describeRecurrence(
|
||||
rrule: String,
|
||||
locale: Locale,
|
||||
string: (id: Int, args: Array<out Any>) -> String,
|
||||
): AnnotatedString {
|
||||
fun stringResource(id: Int, vararg args: Any): String = string(id, args)
|
||||
val parts = rruleParts(rrule).mapNotNull { part -> part.key?.let { it to part.value } }.toMap()
|
||||
|
||||
val freq = parts["FREQ"]?.uppercase()
|
||||
@@ -50,13 +64,20 @@ fun recurrenceText(rrule: String, locale: Locale): AnnotatedString {
|
||||
else -> return AnnotatedString(stringResource(R.string.event_detail_recurring))
|
||||
}
|
||||
|
||||
// Weekly + BYDAY → "<base> on <days>"; other BYDAY forms keep just the base.
|
||||
// Weekly + BYDAY → "<base> on <days>"; monthly + one ordinal BYDAY →
|
||||
// "<base> on the second <day>"; other BYDAY forms keep just the base.
|
||||
// The day names + their joined block are tracked so only the names (not the
|
||||
// commas/conjunction) can be italicised in the final string.
|
||||
val byDay = parts["BYDAY"]
|
||||
var dayNames: List<String>? = null
|
||||
var joinedDays: String? = null
|
||||
val main = if (freq == "WEEKLY" && !byDay.isNullOrBlank()) {
|
||||
val monthlyWeekday = if (freq == "MONTHLY") byDay?.let(MonthlyWeekday::parse) else null
|
||||
val main = if (monthlyWeekday != null) {
|
||||
val name = monthlyWeekday.day.toJavaDayOfWeek().getDisplayName(JavaTextStyle.FULL, locale)
|
||||
dayNames = listOf(name)
|
||||
joinedDays = name
|
||||
stringResource(monthlyWeekdaySummary(monthlyWeekday.ordinal), base, name)
|
||||
} else if (freq == "WEEKLY" && !byDay.isNullOrBlank()) {
|
||||
val days = byDay.split(',').mapNotNull { rruleDayName(it.trim(), locale) }
|
||||
if (days.isNotEmpty()) {
|
||||
val joined = ListFormatter.getInstance(locale).format(days)
|
||||
@@ -129,6 +150,16 @@ fun nextOccurrencesText(
|
||||
/** Enough dates to show a rhythm (and a skipped month), few enough for one line. */
|
||||
private const val NEXT_OCCURRENCE_COUNT = 3
|
||||
|
||||
/** Summary string for a monthly weekday position (1..5 or [MonthlyWeekday.LAST]). */
|
||||
internal fun monthlyWeekdaySummary(ordinal: Int): Int = when (ordinal) {
|
||||
1 -> R.string.recurrence_monthly_weekday_first
|
||||
2 -> R.string.recurrence_monthly_weekday_second
|
||||
3 -> R.string.recurrence_monthly_weekday_third
|
||||
4 -> R.string.recurrence_monthly_weekday_fourth
|
||||
5 -> R.string.recurrence_monthly_weekday_fifth
|
||||
else -> R.string.recurrence_monthly_weekday_last
|
||||
}
|
||||
|
||||
/** Map an RRULE BYDAY token (e.g. "TU" or "2TH") to a localized short weekday name. */
|
||||
private fun rruleDayName(token: String, locale: Locale): String? {
|
||||
val dow = when (token.takeLast(2).uppercase()) {
|
||||
|
||||
@@ -138,6 +138,7 @@ import de.jeanlucmakiola.calendula.domain.zoneDescriptor
|
||||
import de.jeanlucmakiola.calendula.domain.timeZoneOptionOf
|
||||
import de.jeanlucmakiola.calendula.domain.EventFormProblem
|
||||
import de.jeanlucmakiola.calendula.domain.RecurrenceEnd
|
||||
import de.jeanlucmakiola.calendula.domain.MonthlyWeekday
|
||||
import de.jeanlucmakiola.calendula.domain.RecurrenceFreq
|
||||
import de.jeanlucmakiola.calendula.domain.SimpleRecurrence
|
||||
import de.jeanlucmakiola.calendula.domain.parseSimpleRecurrence
|
||||
@@ -1711,10 +1712,11 @@ private enum class RecurrenceEndMode { Never, Until, Count }
|
||||
/**
|
||||
* Recurrence picker, two steps like the reminder picker: the plain
|
||||
* frequencies as a tappable list (one tap applies and closes), with "Custom"
|
||||
* switching to an interval-plus-unit editor, weekday toggles (weekly only)
|
||||
* and an end condition — only that step needs an OK button. A rule the
|
||||
* simple shape can't express (ordinal BYDAY etc.) stays untouched unless the
|
||||
* user picks something here.
|
||||
* switching to an interval-plus-unit editor, weekday toggles (weekly only),
|
||||
* day of month vs. weekday position (monthly only) and an end condition —
|
||||
* only that step needs an OK button. A rule the simple shape can't express
|
||||
* (several ordinal weekdays, BYSETPOS etc.) stays untouched unless the user
|
||||
* picks something here.
|
||||
*
|
||||
* Both steps show the rule as *dates* as well as words: every preset row
|
||||
* carries the next few occurrences it would produce from [startDate], and the
|
||||
@@ -1731,7 +1733,7 @@ private fun RecurrencePickerDialog(
|
||||
val startDay = startDate.dayOfWeek
|
||||
val parsed = remember(current) { current?.let(::parseSimpleRecurrence) }
|
||||
val isPlainPreset = parsed != null && parsed.interval == 1 &&
|
||||
parsed.end == RecurrenceEnd.Never && parsed.byDays.isEmpty()
|
||||
parsed.end == RecurrenceEnd.Never && parsed.byDays.isEmpty() && parsed.monthlyWeekday == null
|
||||
var customMode by rememberSaveable { mutableStateOf(false) }
|
||||
var intervalText by rememberSaveable { mutableStateOf((parsed?.interval ?: 1).toString()) }
|
||||
var freq by rememberSaveable { mutableStateOf(parsed?.freq ?: RecurrenceFreq.Weekly) }
|
||||
@@ -1741,6 +1743,13 @@ private fun RecurrencePickerDialog(
|
||||
(parsed?.byDays?.takeIf { it.isNotEmpty() } ?: setOf(startDay)).toMask(),
|
||||
)
|
||||
}
|
||||
// Monthly: the BYDAY token of a weekday position ("2TU"), null for "on day N".
|
||||
var monthlyWeekdayToken by rememberSaveable { mutableStateOf(parsed?.monthlyWeekday?.token) }
|
||||
val monthlyOptions = remember(startDate, parsed) {
|
||||
val fromStart = MonthlyWeekday.optionsFor(startDate)
|
||||
val kept = parsed?.monthlyWeekday?.takeIf { it !in fromStart }
|
||||
listOf<MonthlyWeekday?>(null) + fromStart + listOfNotNull(kept)
|
||||
}
|
||||
var endMode by rememberSaveable {
|
||||
mutableStateOf(
|
||||
when (parsed?.end) {
|
||||
@@ -1788,6 +1797,11 @@ private fun RecurrencePickerDialog(
|
||||
interval = interval,
|
||||
end = customEnd,
|
||||
byDays = if (freq == RecurrenceFreq.Weekly) daysMask.toDaySet() else emptySet(),
|
||||
monthlyWeekday = if (freq == RecurrenceFreq.Monthly) {
|
||||
monthlyWeekdayToken?.let(MonthlyWeekday::parse)
|
||||
} else {
|
||||
null
|
||||
},
|
||||
)
|
||||
} else {
|
||||
null
|
||||
@@ -1944,6 +1958,32 @@ private fun RecurrencePickerDialog(
|
||||
}
|
||||
}
|
||||
|
||||
// Monthly: same day of the month, or a weekday position derived
|
||||
// from the start date (plus a kept imported one).
|
||||
AnimatedVisibility(
|
||||
visible = freq == RecurrenceFreq.Monthly,
|
||||
enter = expandEnter(),
|
||||
exit = collapseExit(),
|
||||
) {
|
||||
Column(modifier = Modifier.padding(top = 16.dp)) {
|
||||
monthlyOptions.forEachIndexed { index, option ->
|
||||
GroupedRow(
|
||||
title = if (option == null) {
|
||||
stringResource(R.string.event_edit_recurrence_monthly_day, startDate.day)
|
||||
} else {
|
||||
stringResource(
|
||||
monthlyWeekdayLabel(option.ordinal),
|
||||
option.day.toJavaDayOfWeek().getDisplayName(JavaTextStyle.FULL, locale),
|
||||
)
|
||||
},
|
||||
position = positionOf(index, monthlyOptions.size),
|
||||
checked = option?.token == monthlyWeekdayToken,
|
||||
onClick = { monthlyWeekdayToken = option?.token },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ends: a connected never / on-a-date / after-N group, the count
|
||||
// field folding into the bottom of the run when chosen.
|
||||
Column(modifier = Modifier.padding(top = 16.dp)) {
|
||||
@@ -2096,6 +2136,15 @@ private fun recurrencePresetLabel(freq: RecurrenceFreq): Int = when (freq) {
|
||||
RecurrenceFreq.Yearly -> R.string.recurrence_yearly
|
||||
}
|
||||
|
||||
private fun monthlyWeekdayLabel(ordinal: Int): Int = when (ordinal) {
|
||||
1 -> R.string.event_edit_recurrence_monthly_weekday_first
|
||||
2 -> R.string.event_edit_recurrence_monthly_weekday_second
|
||||
3 -> R.string.event_edit_recurrence_monthly_weekday_third
|
||||
4 -> R.string.event_edit_recurrence_monthly_weekday_fourth
|
||||
5 -> R.string.event_edit_recurrence_monthly_weekday_fifth
|
||||
else -> R.string.event_edit_recurrence_monthly_weekday_last
|
||||
}
|
||||
|
||||
private fun recurrenceUnitLabel(freq: RecurrenceFreq): Int = when (freq) {
|
||||
RecurrenceFreq.Daily -> R.string.recurrence_unit_days
|
||||
RecurrenceFreq.Weekly -> R.string.recurrence_unit_weeks
|
||||
|
||||
@@ -221,6 +221,15 @@
|
||||
<string name="event_edit_recurrence_end_until">On a date</string>
|
||||
<string name="event_edit_recurrence_end_count">After a number of times</string>
|
||||
<string name="event_edit_recurrence_times">times</string>
|
||||
<!-- Monthly custom rule: repeat on the start date's day of the month, e.g. "On day 14" (#520) -->
|
||||
<string name="event_edit_recurrence_monthly_day">On day %1$d</string>
|
||||
<!-- Monthly custom rule by weekday position; %1$s is a full weekday name, e.g. "On the second Tuesday" (#520) -->
|
||||
<string name="event_edit_recurrence_monthly_weekday_first">On the first %1$s</string>
|
||||
<string name="event_edit_recurrence_monthly_weekday_second">On the second %1$s</string>
|
||||
<string name="event_edit_recurrence_monthly_weekday_third">On the third %1$s</string>
|
||||
<string name="event_edit_recurrence_monthly_weekday_fourth">On the fourth %1$s</string>
|
||||
<string name="event_edit_recurrence_monthly_weekday_fifth">On the fifth %1$s</string>
|
||||
<string name="event_edit_recurrence_monthly_weekday_last">On the last %1$s</string>
|
||||
<string name="event_edit_error_recurrence_ends_before_start">Repeats end before the event starts</string>
|
||||
<string name="event_availability_busy">Busy</string>
|
||||
<string name="event_access_default">Default</string>
|
||||
@@ -247,6 +256,13 @@
|
||||
<string name="recurrence_every_n_months">Every %1$d months</string>
|
||||
<string name="recurrence_every_n_years">Every %1$d years</string>
|
||||
<string name="recurrence_on_days">%1$s on %2$s</string>
|
||||
<!-- %1$s is the frequency phrase ("Every month", "Every 2 months"), %2$s a full weekday name (#520) -->
|
||||
<string name="recurrence_monthly_weekday_first">%1$s on the first %2$s</string>
|
||||
<string name="recurrence_monthly_weekday_second">%1$s on the second %2$s</string>
|
||||
<string name="recurrence_monthly_weekday_third">%1$s on the third %2$s</string>
|
||||
<string name="recurrence_monthly_weekday_fourth">%1$s on the fourth %2$s</string>
|
||||
<string name="recurrence_monthly_weekday_fifth">%1$s on the fifth %2$s</string>
|
||||
<string name="recurrence_monthly_weekday_last">%1$s on the last %2$s</string>
|
||||
<string name="recurrence_with_until">%1$s until %2$s</string>
|
||||
<string name="recurrence_with_count">%1$s, %2$d times</string>
|
||||
<string name="event_detail_not_found">This event no longer exists.</string>
|
||||
|
||||
@@ -93,9 +93,15 @@ class EventFormTest {
|
||||
|
||||
@Test
|
||||
fun `complex rrules are not validated against the start`() {
|
||||
// The picker can't have produced this ("second Monday" ordinal BYDAY);
|
||||
// it is preserved verbatim and never flagged.
|
||||
assertThat(form().copy(rrule = "FREQ=MONTHLY;BYDAY=2MO;UNTIL=20200101").problems(berlin)).isEmpty()
|
||||
// The picker can't have produced this (two ordinal weekdays); it is
|
||||
// preserved verbatim and never flagged.
|
||||
assertThat(form().copy(rrule = "FREQ=MONTHLY;BYDAY=2MO,4MO;UNTIL=20200101").problems(berlin)).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a monthly weekday position rule is validated like any simple rule`() {
|
||||
assertThat(form().copy(rrule = "FREQ=MONTHLY;BYDAY=2MO;UNTIL=20200101").problems(berlin))
|
||||
.containsExactly(EventFormProblem.RecurrenceEndsBeforeStart)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package de.jeanlucmakiola.calendula.domain
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import kotlinx.datetime.DayOfWeek
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.TimeZone
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
/** Monthly repeats by weekday position (#520). */
|
||||
class MonthlyWeekdayTest {
|
||||
|
||||
private val utc = TimeZone.UTC
|
||||
|
||||
@Test
|
||||
fun `ordinal BYDAY on a monthly rule parses into a weekday position`() {
|
||||
(1..5).forEach { n ->
|
||||
assertThat(parseSimpleRecurrence("FREQ=MONTHLY;BYDAY=${n}TU", utc))
|
||||
.isEqualTo(
|
||||
SimpleRecurrence(
|
||||
RecurrenceFreq.Monthly,
|
||||
monthlyWeekday = MonthlyWeekday(n, DayOfWeek.TUESDAY),
|
||||
),
|
||||
)
|
||||
}
|
||||
assertThat(parseSimpleRecurrence("FREQ=MONTHLY;BYDAY=-1SA", utc)?.monthlyWeekday)
|
||||
.isEqualTo(MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.SATURDAY))
|
||||
assertThat(parseSimpleRecurrence("FREQ=MONTHLY;BYDAY=+2tu", utc)?.monthlyWeekday)
|
||||
.isEqualTo(MonthlyWeekday(2, DayOfWeek.TUESDAY))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `round-trips 1 to 5 and last with interval and end`() {
|
||||
val positions = (1..5).map(Int::toString) + "-1"
|
||||
val tails = listOf("", ";COUNT=6", ";UNTIL=20301231T235959Z")
|
||||
positions.forEach { n ->
|
||||
tails.forEach { tail ->
|
||||
listOf("FREQ=MONTHLY;BYDAY=${n}SA$tail", "FREQ=MONTHLY;INTERVAL=3;BYDAY=${n}SA$tail")
|
||||
.forEach { rule ->
|
||||
assertThat(parseSimpleRecurrence(rule, utc)!!.toRRule(utc)).isEqualTo(rule)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `interval and end survive alongside the weekday position`() {
|
||||
val parsed = parseSimpleRecurrence("FREQ=MONTHLY;INTERVAL=2;BYDAY=2TU;COUNT=12", utc)!!
|
||||
assertThat(parsed.interval).isEqualTo(2)
|
||||
assertThat(parsed.end).isEqualTo(RecurrenceEnd.Count(12))
|
||||
|
||||
val until = parseSimpleRecurrence("FREQ=MONTHLY;BYDAY=-1FR;UNTIL=20271231T235959Z", utc)!!
|
||||
assertThat(until.end).isEqualTo(RecurrenceEnd.Until(LocalDate(2027, 12, 31)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `rules the editor cannot represent stay unparsed`() {
|
||||
listOf(
|
||||
"FREQ=MONTHLY;BYDAY=2TU,4TU",
|
||||
"FREQ=MONTHLY;BYDAY=MO",
|
||||
"FREQ=MONTHLY;BYDAY=6TU",
|
||||
"FREQ=MONTHLY;BYDAY=-2TU",
|
||||
"FREQ=MONTHLY;BYDAY=0TU",
|
||||
"FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1",
|
||||
"FREQ=MONTHLY;BYDAY=2TU;BYMONTH=3",
|
||||
"FREQ=YEARLY;BYDAY=2TU",
|
||||
"FREQ=WEEKLY;BYDAY=2TU",
|
||||
).forEach { assertThat(parseSimpleRecurrence(it, utc)).isNull() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the weekday position is only written on a monthly rule`() {
|
||||
val rule = SimpleRecurrence(
|
||||
RecurrenceFreq.Weekly,
|
||||
monthlyWeekday = MonthlyWeekday(2, DayOfWeek.TUESDAY),
|
||||
)
|
||||
assertThat(rule.toRRule(utc)).isEqualTo("FREQ=WEEKLY")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefill offers the start date's ordinal`() {
|
||||
// 10 Nov 2026 is the second Tuesday, not in the last week.
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 11, 10)))
|
||||
.containsExactly(MonthlyWeekday(2, DayOfWeek.TUESDAY))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefill adds last when the start is in the month's final seven days`() {
|
||||
// 24 Feb 2026: fourth Tuesday and also the last one.
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 2, 24)))
|
||||
.containsExactly(
|
||||
MonthlyWeekday(4, DayOfWeek.TUESDAY),
|
||||
MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.TUESDAY),
|
||||
).inOrder()
|
||||
// 31 Oct 2026: fifth Saturday.
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 10, 31)))
|
||||
.containsExactly(
|
||||
MonthlyWeekday(5, DayOfWeek.SATURDAY),
|
||||
MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.SATURDAY),
|
||||
).inOrder()
|
||||
// 24 Nov 2026 is the fourth and last Tuesday; 23 Nov is a fourth but not last Monday.
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 11, 24)))
|
||||
.containsExactly(
|
||||
MonthlyWeekday(4, DayOfWeek.TUESDAY),
|
||||
MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.TUESDAY),
|
||||
).inOrder()
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 11, 23)))
|
||||
.containsExactly(MonthlyWeekday(4, DayOfWeek.MONDAY))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `dateIn resolves positions and skips missing fifths`() {
|
||||
val sat5 = MonthlyWeekday(5, DayOfWeek.SATURDAY)
|
||||
assertThat(sat5.dateIn(2026, 10)).isEqualTo(LocalDate(2026, 10, 31))
|
||||
assertThat(sat5.dateIn(2026, 11)).isNull()
|
||||
assertThat(MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.SATURDAY).dateIn(2026, 11))
|
||||
.isEqualTo(LocalDate(2026, 11, 28))
|
||||
assertThat(MonthlyWeekday(1, DayOfWeek.SUNDAY).dateIn(2026, 11))
|
||||
.isEqualTo(LocalDate(2026, 11, 1))
|
||||
assertThat(MonthlyWeekday(2, DayOfWeek.TUESDAY).dateIn(2026, 12))
|
||||
.isEqualTo(LocalDate(2026, 12, 8))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `occurrences follow the weekday position across months`() {
|
||||
val rule = SimpleRecurrence(
|
||||
RecurrenceFreq.Monthly,
|
||||
monthlyWeekday = MonthlyWeekday(5, DayOfWeek.SATURDAY),
|
||||
)
|
||||
// Fifth Saturdays after 31 Oct 2026: Jan 30 2027, May 29 2027.
|
||||
assertThat(rule.upcomingOccurrences(LocalDate(2026, 10, 31), limit = 3))
|
||||
.containsExactly(
|
||||
LocalDate(2026, 10, 31),
|
||||
LocalDate(2027, 1, 30),
|
||||
LocalDate(2027, 5, 29),
|
||||
).inOrder()
|
||||
|
||||
val lastEveryOther = SimpleRecurrence(
|
||||
RecurrenceFreq.Monthly,
|
||||
interval = 2,
|
||||
monthlyWeekday = MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.FRIDAY),
|
||||
end = RecurrenceEnd.Count(2),
|
||||
)
|
||||
assertThat(lastEveryOther.upcomingOccurrences(LocalDate(2026, 11, 27), limit = 5))
|
||||
.containsExactly(LocalDate(2026, 11, 27), LocalDate(2027, 1, 29)).inOrder()
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package de.jeanlucmakiola.calendula.ui.common
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.calendula.R
|
||||
import java.util.Locale
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
import org.junit.jupiter.api.Test
|
||||
@@ -67,3 +69,44 @@ class RecurrenceTextTest {
|
||||
assertThat(untilLocalDate("20261340", berlin)).isNull()
|
||||
}
|
||||
}
|
||||
|
||||
/** Summary phrases for monthly weekday positions (#520). */
|
||||
class MonthlyWeekdaySummaryTest {
|
||||
|
||||
private val english = mapOf(
|
||||
R.string.recurrence_monthly to "Every month",
|
||||
R.string.recurrence_every_n_months to "Every %1\$d months",
|
||||
R.string.recurrence_with_count to "%1\$s, %2\$d times",
|
||||
R.string.recurrence_monthly_weekday_first to "%1\$s on the first %2\$s",
|
||||
R.string.recurrence_monthly_weekday_second to "%1\$s on the second %2\$s",
|
||||
R.string.recurrence_monthly_weekday_third to "%1\$s on the third %2\$s",
|
||||
R.string.recurrence_monthly_weekday_fourth to "%1\$s on the fourth %2\$s",
|
||||
R.string.recurrence_monthly_weekday_fifth to "%1\$s on the fifth %2\$s",
|
||||
R.string.recurrence_monthly_weekday_last to "%1\$s on the last %2\$s",
|
||||
)
|
||||
|
||||
private fun describe(rule: String) = describeRecurrence(rule, Locale.ENGLISH) { id, args ->
|
||||
english.getValue(id).format(Locale.ENGLISH, *args)
|
||||
}.text
|
||||
|
||||
@Test
|
||||
fun `each position reads as its ordinal`() {
|
||||
assertThat(describe("FREQ=MONTHLY;BYDAY=1MO")).isEqualTo("Every month on the first Monday")
|
||||
assertThat(describe("FREQ=MONTHLY;BYDAY=2TU")).isEqualTo("Every month on the second Tuesday")
|
||||
assertThat(describe("FREQ=MONTHLY;BYDAY=3WE")).isEqualTo("Every month on the third Wednesday")
|
||||
assertThat(describe("FREQ=MONTHLY;BYDAY=4TH")).isEqualTo("Every month on the fourth Thursday")
|
||||
assertThat(describe("FREQ=MONTHLY;BYDAY=5SA")).isEqualTo("Every month on the fifth Saturday")
|
||||
assertThat(describe("FREQ=MONTHLY;BYDAY=-1SU")).isEqualTo("Every month on the last Sunday")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `interval and count wrap the position phrase`() {
|
||||
assertThat(describe("FREQ=MONTHLY;INTERVAL=2;BYDAY=-1FR;COUNT=4"))
|
||||
.isEqualTo("Every 2 months on the last Friday, 4 times")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `multi-day ordinal BYDAY keeps the plain frequency`() {
|
||||
assertThat(describe("FREQ=MONTHLY;BYDAY=2TU,4TU")).isEqualTo("Every month")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user