fix #1201, support 5th weekday recurring events

This commit is contained in:
Kazuhiro Ito 2021-03-09 17:40:18 +09:00
parent 4ba310db2d
commit 217649c9f4
3 changed files with 17 additions and 30 deletions

View File

@ -644,17 +644,9 @@ class EventActivity : SimpleActivity() {
private fun getAvailableMonthlyRepetitionRules(): ArrayList<RadioItem> {
val items = arrayListOf(RadioItem(REPEAT_SAME_DAY, getString(R.string.repeat_on_the_same_day_monthly)))
// split Every Last Sunday and Every Fourth Sunday of the month, if the month has 4 sundays
items.add(RadioItem(REPEAT_ORDER_WEEKDAY, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY)))
if (isLastWeekDayOfMonth()) {
val order = (mEventStartDateTime.dayOfMonth - 1) / 7 + 1
if (order == 4) {
items.add(RadioItem(REPEAT_ORDER_WEEKDAY, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY)))
items.add(RadioItem(REPEAT_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY_USE_LAST)))
} else if (order == 5) {
items.add(RadioItem(REPEAT_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY_USE_LAST)))
}
} else {
items.add(RadioItem(REPEAT_ORDER_WEEKDAY, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY)))
items.add(RadioItem(REPEAT_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY_USE_LAST)))
}
if (isLastDayOfTheMonth()) {
@ -666,16 +658,9 @@ class EventActivity : SimpleActivity() {
private fun getAvailableYearlyRepetitionRules(): ArrayList<RadioItem> {
val items = arrayListOf(RadioItem(REPEAT_SAME_DAY, getString(R.string.repeat_on_the_same_day_yearly)))
items.add(RadioItem(REPEAT_ORDER_WEEKDAY, getRepeatXthDayInMonthString(true, REPEAT_ORDER_WEEKDAY)))
if (isLastWeekDayOfMonth()) {
val order = (mEventStartDateTime.dayOfMonth - 1) / 7 + 1
if (order == 4) {
items.add(RadioItem(REPEAT_ORDER_WEEKDAY, getRepeatXthDayInMonthString(true, REPEAT_ORDER_WEEKDAY)))
items.add(RadioItem(REPEAT_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayInMonthString(true, REPEAT_ORDER_WEEKDAY_USE_LAST)))
} else if (order == 5) {
items.add(RadioItem(REPEAT_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayInMonthString(true, REPEAT_ORDER_WEEKDAY_USE_LAST)))
}
} else {
items.add(RadioItem(REPEAT_ORDER_WEEKDAY, getRepeatXthDayInMonthString(true, REPEAT_ORDER_WEEKDAY)))
items.add(RadioItem(REPEAT_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayInMonthString(true, REPEAT_ORDER_WEEKDAY_USE_LAST)))
}
return items
@ -711,7 +696,7 @@ class EventActivity : SimpleActivity() {
private fun getOrderString(repeatRule: Int): String {
val dayOfMonth = mEventStartDateTime.dayOfMonth
var order = (dayOfMonth - 1) / 7 + 1
if (order == 4 && isLastWeekDayOfMonth() && repeatRule == REPEAT_ORDER_WEEKDAY_USE_LAST) {
if (isLastWeekDayOfMonth() && repeatRule == REPEAT_ORDER_WEEKDAY_USE_LAST) {
order = -1
}
@ -721,6 +706,7 @@ class EventActivity : SimpleActivity() {
2 -> if (isMale) R.string.second_m else R.string.second_f
3 -> if (isMale) R.string.third_m else R.string.third_f
4 -> if (isMale) R.string.fourth_m else R.string.fourth_f
5 -> if (isMale) R.string.fifth_m else R.string.fifth_f
else -> if (isMale) R.string.last_m else R.string.last_f
})
}

View File

@ -99,10 +99,8 @@ data class Event(
private fun addXthDayInterval(currStart: DateTime, original: Event, forceLastWeekday: Boolean): DateTime {
val day = currStart.dayOfWeek
var order = (currStart.dayOfMonth - 1) / 7
val properMonth = currStart.withDayOfMonth(7).plusMonths(repeatInterval / MONTH).withDayOfWeek(day)
var firstProperDay = properMonth.dayOfMonth % 7
if (firstProperDay == 0)
firstProperDay = properMonth.dayOfMonth
var properMonth = currStart.withDayOfMonth(7).plusMonths(repeatInterval / MONTH).withDayOfWeek(day)
var wantedDay: Int
// check if it should be for example Fourth Monday, or Last Monday
if (forceLastWeekday && (order == 3 || order == 4)) {
@ -112,13 +110,14 @@ data class Event(
order = -1
}
val daysCnt = properMonth.dayOfMonth().maximumValue
var wantedDay = firstProperDay + order * 7
if (wantedDay > daysCnt)
wantedDay -= 7
if (order == -1) {
wantedDay = firstProperDay + ((daysCnt - firstProperDay) / 7) * 7
wantedDay = properMonth.dayOfMonth + ((properMonth.dayOfMonth().maximumValue - properMonth.dayOfMonth) / 7) * 7
} else {
wantedDay = properMonth.dayOfMonth + (order - (properMonth.dayOfMonth - 1) / 7) * 7
while (properMonth.dayOfMonth().maximumValue < wantedDay) {
properMonth = properMonth.withDayOfMonth(7).plusMonths(repeatInterval / MONTH).withDayOfWeek(day)
wantedDay = properMonth.dayOfMonth + (order - (properMonth.dayOfMonth - 1) / 7) * 7
}
}
return properMonth.withDayOfMonth(wantedDay)

View File

@ -71,6 +71,7 @@
<string name="second_m">second</string>
<string name="third_m">third</string>
<string name="fourth_m">fourth</string>
<string name="fifth_m">fifth</string>
<string name="last_m">last</string>
<!-- alternative versions for some languages, use the same translations if you are not sure what this means -->
@ -81,6 +82,7 @@
<string name="second_f">second</string>
<string name="third_f">third</string>
<string name="fourth_f">fourth</string>
<string name="fifth_f">fifth</string>
<string name="last_f">last</string>
<!-- Birthdays -->