allow repeating on Every 4th Sunday even if a month has 4 sundays only

This commit is contained in:
tibbi 2017-11-25 23:02:15 +01:00
parent 72a2b574fd
commit a89072e244
4 changed files with 28 additions and 19 deletions

View File

@ -229,7 +229,15 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
} else if (mRepeatInterval.isXMonthlyRepetition()) {
val items = arrayListOf(
RadioItem(REPEAT_MONTH_SAME_DAY, getString(R.string.repeat_on_the_same_day)),
RadioItem(REPEAT_MONTH_EVERY_XTH_DAY, getRepeatXthDayString(true)))
RadioItem(REPEAT_MONTH_ORDER_WEEKDAY, getRepeatXthDayString(true, REPEAT_MONTH_ORDER_WEEKDAY)))
// split Every Last Sunday and Every Fourth Sunday of the month, if the month has 4 sundays
if (isLastWeekDayOfMonth()) {
val order = (mEventStartDateTime.dayOfMonth - 1) / 7 + 1
if (order == 4) {
items.add(RadioItem(REPEAT_MONTH_LAST_WEEKDAY, getRepeatXthDayString(true, REPEAT_MONTH_LAST_WEEKDAY)))
}
}
if (isLastDayOfTheMonth()) {
items.add(RadioItem(REPEAT_MONTH_LAST_DAY, getString(R.string.repeat_on_the_last_day)))
@ -243,10 +251,12 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
private fun isLastDayOfTheMonth() = mEventStartDateTime.dayOfMonth == mEventStartDateTime.dayOfMonth().withMaximumValue().dayOfMonth
private fun getRepeatXthDayString(includeBase: Boolean): String {
private fun isLastWeekDayOfMonth() = mEventStartDateTime.monthOfYear != mEventStartDateTime.plusDays(7).monthOfYear
private fun getRepeatXthDayString(includeBase: Boolean, repeatRule: Int): String {
val dayOfWeek = mEventStartDateTime.dayOfWeek
val base = getBaseString(dayOfWeek)
val order = getOrderString()
val order = getOrderString(repeatRule)
val dayString = getDayString(dayOfWeek)
return if (includeBase) {
"$base $order $dayString"
@ -266,13 +276,9 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
private fun isMaleGender(day: Int) = day == 1 || day == 2 || day == 4 || day == 5
private fun getOrderString(): String {
private fun getOrderString(repeatRule: Int): String {
val dayOfMonth = mEventStartDateTime.dayOfMonth
var order = (dayOfMonth - 1) / 7 + 1
if (mEventStartDateTime.monthOfYear != mEventStartDateTime.plusDays(7).monthOfYear) {
order = -1
}
val order = if (repeatRule == REPEAT_MONTH_LAST_WEEKDAY) -1 else (dayOfMonth - 1) / 7 + 1
val isMale = isMaleGender(mEventStartDateTime.dayOfWeek)
return getString(when (order) {
1 -> if (isMale) R.string.first_m else R.string.first_f
@ -307,7 +313,8 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
if (mRepeatInterval.isXWeeklyRepetition()) {
event_repetition_rule.text = if (mRepeatRule == EVERY_DAY) getString(R.string.every_day) else getSelectedDaysString()
} else if (mRepeatInterval.isXMonthlyRepetition()) {
event_repetition_rule_label.text = getString(if (mRepeatRule == REPEAT_MONTH_EVERY_XTH_DAY) R.string.repeat else R.string.repeat_on)
val repeatString = if (mRepeatRule == REPEAT_MONTH_ORDER_WEEKDAY || mRepeatRule == REPEAT_MONTH_LAST_WEEKDAY) R.string.repeat else R.string.repeat_on
event_repetition_rule_label.text = getString(repeatString)
event_repetition_rule.text = getMonthlyRepetitionRuleText()
}
}
@ -335,7 +342,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
private fun getMonthlyRepetitionRuleText() = when (mRepeatRule) {
REPEAT_MONTH_SAME_DAY -> getString(R.string.the_same_day)
REPEAT_MONTH_LAST_DAY -> getString(R.string.the_last_day)
else -> getRepeatXthDayString(false)
else -> getRepeatXthDayString(false, mRepeatRule)
}
private fun showEventTypeDialog() {

View File

@ -64,9 +64,10 @@ val SUNDAY = 64
val EVERY_DAY = 127
// repeat_rule for monthly repetition
val REPEAT_MONTH_SAME_DAY = 1
val REPEAT_MONTH_EVERY_XTH_DAY = 2
val REPEAT_MONTH_LAST_DAY = 3
val REPEAT_MONTH_SAME_DAY = 1 // ie 25th every month
val REPEAT_MONTH_ORDER_WEEKDAY = 2 // ie every 4th sunday, even if a month has 4 sundays only (will stay 4th even at months with 5)
val REPEAT_MONTH_LAST_DAY = 3 // ie every last day of the month
val REPEAT_MONTH_LAST_WEEKDAY = 4 // ie every last sunday
// special event flags
val FLAG_ALL_DAY = 1

View File

@ -41,7 +41,7 @@ class Parser {
if (repeatInterval.isXWeeklyRepetition()) {
repeatRule = handleRepeatRule(value)
} else if (repeatInterval.isXMonthlyRepetition()) {
repeatRule = REPEAT_MONTH_EVERY_XTH_DAY
repeatRule = REPEAT_MONTH_ORDER_WEEKDAY
}
} else if (key == BYMONTHDAY && value.toInt() == -1) {
repeatRule = REPEAT_MONTH_LAST_DAY
@ -133,7 +133,7 @@ class Parser {
}
event.repeatInterval.isXMonthlyRepetition() -> when {
event.repeatRule == REPEAT_MONTH_LAST_DAY -> ";$BYMONTHDAY=-1"
event.repeatRule == REPEAT_MONTH_EVERY_XTH_DAY -> {
event.repeatRule == REPEAT_MONTH_ORDER_WEEKDAY -> {
val start = Formatter.getDateTimeFromTS(event.startTS)
val dayOfMonth = start.dayOfMonth
val order = (dayOfMonth - 1) / 7 + 1

View File

@ -28,7 +28,8 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var
repeatInterval % YEAR == 0 -> currStart.plusYears(repeatInterval / YEAR)
repeatInterval % MONTH == 0 -> when (repeatRule) {
REPEAT_MONTH_SAME_DAY -> addMonthsWithSameDay(currStart, original)
REPEAT_MONTH_EVERY_XTH_DAY -> addXthDayInterval(currStart, original)
REPEAT_MONTH_ORDER_WEEKDAY -> addXthDayInterval(currStart, original, false)
REPEAT_MONTH_LAST_WEEKDAY -> addXthDayInterval(currStart, original, true)
else -> currStart.plusMonths(repeatInterval / MONTH).dayOfMonth().withMaximumValue()
}
repeatInterval % WEEK == 0 -> {
@ -60,7 +61,7 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var
}
// handle monthly repetitions like Third Monday
private fun addXthDayInterval(currStart: DateTime, original: Event): DateTime {
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)
@ -69,7 +70,7 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var
firstProperDay = properMonth.dayOfMonth
// check if it should be for example Fourth Monday, or Last Monday
if (order == 3 || order == 4) {
if (forceLastWeekday && (order == 3 || order == 4)) {
val originalDateTime = Formatter.getDateTimeFromTS(original.startTS)
val isLastWeekday = originalDateTime.monthOfYear != originalDateTime.plusDays(7).monthOfYear
if (isLastWeekday)