updating some constants in preparation for advanced yearly repetition rules

This commit is contained in:
tibbi
2018-05-10 10:29:35 +02:00
parent c2d120b99d
commit 59486f98d0
6 changed files with 43 additions and 34 deletions

View File

@@ -221,7 +221,7 @@ class EventActivity : SimpleActivity() {
if (mRepeatInterval.isXWeeklyRepetition()) { if (mRepeatInterval.isXWeeklyRepetition()) {
setRepeatRule(Math.pow(2.0, (mEventStartDateTime.dayOfWeek - 1).toDouble()).toInt()) setRepeatRule(Math.pow(2.0, (mEventStartDateTime.dayOfWeek - 1).toDouble()).toInt())
} else if (mRepeatInterval.isXMonthlyRepetition()) { } else if (mRepeatInterval.isXMonthlyRepetition()) {
setRepeatRule(REPEAT_MONTH_SAME_DAY) setRepeatRule(REPEAT_SAME_DAY)
} }
} }
@@ -229,7 +229,7 @@ class EventActivity : SimpleActivity() {
event_repetition_limit_holder.beGoneIf(limit == 0) event_repetition_limit_holder.beGoneIf(limit == 0)
checkRepetitionLimitText() checkRepetitionLimitText()
event_repetition_rule_holder.beVisibleIf(mRepeatInterval.isXWeeklyRepetition() || mRepeatInterval.isXMonthlyRepetition()) event_repetition_rule_holder.beVisibleIf(mRepeatInterval.isXWeeklyRepetition() || mRepeatInterval.isXMonthlyRepetition() || mRepeatInterval.isXYearlyRepetition())
checkRepetitionRuleText() checkRepetitionRuleText()
} }
@@ -270,23 +270,23 @@ class EventActivity : SimpleActivity() {
setRepeatRule(it) setRepeatRule(it)
} }
} else if (mRepeatInterval.isXMonthlyRepetition()) { } else if (mRepeatInterval.isXMonthlyRepetition()) {
val items = arrayListOf(RadioItem(REPEAT_MONTH_SAME_DAY, getString(R.string.repeat_on_the_same_day))) val items = arrayListOf(RadioItem(REPEAT_SAME_DAY, getString(R.string.repeat_on_the_same_day)))
// split Every Last Sunday and Every Fourth Sunday of the month, if the month has 4 sundays // split Every Last Sunday and Every Fourth Sunday of the month, if the month has 4 sundays
if (isLastWeekDayOfMonth()) { if (isLastWeekDayOfMonth()) {
val order = (mEventStartDateTime.dayOfMonth - 1) / 7 + 1 val order = (mEventStartDateTime.dayOfMonth - 1) / 7 + 1
if (order == 4) { if (order == 4) {
items.add(RadioItem(REPEAT_MONTH_ORDER_WEEKDAY, getRepeatXthDayString(true, REPEAT_MONTH_ORDER_WEEKDAY))) items.add(RadioItem(REPEAT_ORDER_WEEKDAY, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY)))
items.add(RadioItem(REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayString(true, REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST))) items.add(RadioItem(REPEAT_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY_USE_LAST)))
} else if (order == 5) { } else if (order == 5) {
items.add(RadioItem(REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayString(true, REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST))) items.add(RadioItem(REPEAT_ORDER_WEEKDAY_USE_LAST, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY_USE_LAST)))
} }
} else { } else {
items.add(RadioItem(REPEAT_MONTH_ORDER_WEEKDAY, getRepeatXthDayString(true, REPEAT_MONTH_ORDER_WEEKDAY))) items.add(RadioItem(REPEAT_ORDER_WEEKDAY, getRepeatXthDayString(true, REPEAT_ORDER_WEEKDAY)))
} }
if (isLastDayOfTheMonth()) { if (isLastDayOfTheMonth()) {
items.add(RadioItem(REPEAT_MONTH_LAST_DAY, getString(R.string.repeat_on_the_last_day))) items.add(RadioItem(REPEAT_LAST_DAY, getString(R.string.repeat_on_the_last_day)))
} }
RadioGroupDialog(this, items, mRepeatRule) { RadioGroupDialog(this, items, mRepeatRule) {
@@ -325,7 +325,7 @@ class EventActivity : SimpleActivity() {
private fun getOrderString(repeatRule: Int): String { private fun getOrderString(repeatRule: Int): String {
val dayOfMonth = mEventStartDateTime.dayOfMonth val dayOfMonth = mEventStartDateTime.dayOfMonth
var order = (dayOfMonth - 1) / 7 + 1 var order = (dayOfMonth - 1) / 7 + 1
if (order == 4 && isLastWeekDayOfMonth() && repeatRule == REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST) { if (order == 4 && isLastWeekDayOfMonth() && repeatRule == REPEAT_ORDER_WEEKDAY_USE_LAST) {
order = -1 order = -1
} }
@@ -360,20 +360,26 @@ class EventActivity : SimpleActivity() {
} }
private fun checkRepetitionRuleText() { private fun checkRepetitionRuleText() {
if (mRepeatInterval.isXWeeklyRepetition()) { when {
event_repetition_rule.text = if (mRepeatRule == EVERY_DAY_BIT) getString(R.string.every_day) else getSelectedDaysString(mRepeatRule) mRepeatInterval.isXWeeklyRepetition() -> {
} else if (mRepeatInterval.isXMonthlyRepetition()) { event_repetition_rule.text = if (mRepeatRule == EVERY_DAY_BIT) getString(R.string.every_day) else getSelectedDaysString(mRepeatRule)
val repeatString = if (mRepeatRule == REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST || mRepeatRule == REPEAT_MONTH_ORDER_WEEKDAY) }
R.string.repeat else R.string.repeat_on mRepeatInterval.isXMonthlyRepetition() -> {
val repeatString = if (mRepeatRule == REPEAT_ORDER_WEEKDAY_USE_LAST || mRepeatRule == REPEAT_ORDER_WEEKDAY)
R.string.repeat else R.string.repeat_on
event_repetition_rule_label.text = getString(repeatString) event_repetition_rule_label.text = getString(repeatString)
event_repetition_rule.text = getMonthlyRepetitionRuleText() event_repetition_rule.text = getMonthlyRepetitionRuleText()
}
mRepeatInterval.isXYearlyRepetition() -> {
}
} }
} }
private fun getMonthlyRepetitionRuleText() = when (mRepeatRule) { private fun getMonthlyRepetitionRuleText() = when (mRepeatRule) {
REPEAT_MONTH_SAME_DAY -> getString(R.string.the_same_day) REPEAT_SAME_DAY -> getString(R.string.the_same_day)
REPEAT_MONTH_LAST_DAY -> getString(R.string.the_last_day) REPEAT_LAST_DAY -> getString(R.string.the_last_day)
else -> getRepeatXthDayString(false, mRepeatRule) else -> getRepeatXthDayString(false, mRepeatRule)
} }
@@ -804,8 +810,8 @@ class EventActivity : SimpleActivity() {
setRepeatRule(Math.pow(2.0, (mEventStartDateTime.dayOfWeek - 1).toDouble()).toInt()) setRepeatRule(Math.pow(2.0, (mEventStartDateTime.dayOfWeek - 1).toDouble()).toInt())
} }
} else if (mRepeatInterval.isXMonthlyRepetition()) { } else if (mRepeatInterval.isXMonthlyRepetition()) {
if (mRepeatRule == REPEAT_MONTH_LAST_DAY && !isLastDayOfTheMonth()) if (mRepeatRule == REPEAT_LAST_DAY && !isLastDayOfTheMonth())
mRepeatRule = REPEAT_MONTH_SAME_DAY mRepeatRule = REPEAT_SAME_DAY
checkRepetitionRuleText() checkRepetitionRuleText()
} }
} }

View File

@@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.extensions
import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.MONTH import com.simplemobiletools.calendar.helpers.MONTH
import com.simplemobiletools.calendar.helpers.WEEK import com.simplemobiletools.calendar.helpers.WEEK
import com.simplemobiletools.calendar.helpers.YEAR
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
fun Int.isTsOnProperDay(event: Event): Boolean { fun Int.isTsOnProperDay(event: Event): Boolean {
@@ -14,3 +15,5 @@ fun Int.isTsOnProperDay(event: Event): Boolean {
fun Int.isXWeeklyRepetition() = this != 0 && this % WEEK == 0 fun Int.isXWeeklyRepetition() = this != 0 && this % WEEK == 0
fun Int.isXMonthlyRepetition() = this != 0 && this % MONTH == 0 fun Int.isXMonthlyRepetition() = this != 0 && this % MONTH == 0
fun Int.isXYearlyRepetition() = this != 0 && this % YEAR == 0

View File

@@ -54,10 +54,10 @@ const val LOOP_REMINDERS = "loop_reminders"
const val DIM_PAST_EVENTS = "dim_past_events" const val DIM_PAST_EVENTS = "dim_past_events"
// repeat_rule for monthly repetition // repeat_rule for monthly repetition
const val REPEAT_MONTH_SAME_DAY = 1 // ie 25th every month const val REPEAT_SAME_DAY = 1 // i.e. 25th every month, or 3rd june (if yearly repetition)
const val REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST = 2 // ie every xth sunday. 4th if a month has 4 sundays, 5th if 5 const val REPEAT_ORDER_WEEKDAY_USE_LAST = 2 // i.e. every last sunday. 4th if a month has 4 sundays, 5th if 5 (or last sunday in june, if yearly)
const val REPEAT_MONTH_LAST_DAY = 3 // ie every last day of the month const val REPEAT_LAST_DAY = 3 // i.e. every last day of the month
const val REPEAT_MONTH_ORDER_WEEKDAY = 4 // ie every 4th sunday, even if a month has 4 sundays only (will stay 4th even at months with 5) const val REPEAT_ORDER_WEEKDAY = 4 // i.e. every 4th sunday, even if a month has 4 sundays only (will stay 4th even at months with 5)
// special event flags // special event flags
const val FLAG_ALL_DAY = 1 const val FLAG_ALL_DAY = 1

View File

@@ -915,7 +915,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} }
if (repeatInterval > 0 && repeatInterval % MONTH == 0 && repeatRule == 0) { if (repeatInterval > 0 && repeatInterval % MONTH == 0 && repeatRule == 0) {
repeatRule = REPEAT_MONTH_SAME_DAY repeatRule = REPEAT_SAME_DAY
} }
val isPastEvent = endTS < getNowSeconds() val isPastEvent = endTS < getNowSeconds()
@@ -1033,7 +1033,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val start = cursor.getIntValue(COL_REPEAT_START) val start = cursor.getIntValue(COL_REPEAT_START)
var rule = Math.pow(2.0, (Formatter.getDateTimeFromTS(start).dayOfWeek - 1).toDouble()).toInt() var rule = Math.pow(2.0, (Formatter.getDateTimeFromTS(start).dayOfWeek - 1).toDouble()).toInt()
if (interval == MONTH) { if (interval == MONTH) {
rule = REPEAT_MONTH_SAME_DAY rule = REPEAT_SAME_DAY
} }
val values = ContentValues() val values = ContentValues()

View File

@@ -30,7 +30,7 @@ class Parser {
val start = Formatter.getDateTimeFromTS(startTS) val start = Formatter.getDateTimeFromTS(startTS)
repeatRule = Math.pow(2.0, (start.dayOfWeek - 1).toDouble()).toInt() repeatRule = Math.pow(2.0, (start.dayOfWeek - 1).toDouble()).toInt()
} else if (value == MONTHLY) { } else if (value == MONTHLY) {
repeatRule = REPEAT_MONTH_SAME_DAY repeatRule = REPEAT_SAME_DAY
} }
} else if (key == COUNT) { } else if (key == COUNT) {
repeatLimit = -value.toInt() repeatLimit = -value.toInt()
@@ -42,10 +42,10 @@ class Parser {
if (repeatInterval.isXWeeklyRepetition()) { if (repeatInterval.isXWeeklyRepetition()) {
repeatRule = handleRepeatRule(value) repeatRule = handleRepeatRule(value)
} else if (repeatInterval.isXMonthlyRepetition()) { } else if (repeatInterval.isXMonthlyRepetition()) {
repeatRule = if (value.startsWith("-1")) REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST else REPEAT_MONTH_ORDER_WEEKDAY repeatRule = if (value.startsWith("-1")) REPEAT_ORDER_WEEKDAY_USE_LAST else REPEAT_ORDER_WEEKDAY
} }
} else if (key == BYMONTHDAY && value.toInt() == -1) { } else if (key == BYMONTHDAY && value.toInt() == -1) {
repeatRule = REPEAT_MONTH_LAST_DAY repeatRule = REPEAT_LAST_DAY
} }
} }
return RepeatRule(repeatInterval, repeatRule, repeatLimit) return RepeatRule(repeatInterval, repeatRule, repeatLimit)
@@ -133,8 +133,8 @@ class Parser {
";$BYDAY=$days" ";$BYDAY=$days"
} }
event.repeatInterval.isXMonthlyRepetition() -> when (event.repeatRule) { event.repeatInterval.isXMonthlyRepetition() -> when (event.repeatRule) {
REPEAT_MONTH_LAST_DAY -> ";$BYMONTHDAY=-1" REPEAT_LAST_DAY -> ";$BYMONTHDAY=-1"
REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST, REPEAT_MONTH_ORDER_WEEKDAY -> { REPEAT_ORDER_WEEKDAY_USE_LAST, REPEAT_ORDER_WEEKDAY -> {
val start = Formatter.getDateTimeFromTS(event.startTS) val start = Formatter.getDateTimeFromTS(event.startTS)
val dayOfMonth = start.dayOfMonth val dayOfMonth = start.dayOfMonth
val isLastWeekday = start.monthOfYear != start.plusDays(7).monthOfYear val isLastWeekday = start.monthOfYear != start.plusDays(7).monthOfYear

View File

@@ -31,9 +31,9 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var
val newStart = when { val newStart = when {
repeatInterval % YEAR == 0 -> currStart.plusYears(repeatInterval / YEAR) repeatInterval % YEAR == 0 -> currStart.plusYears(repeatInterval / YEAR)
repeatInterval % MONTH == 0 -> when (repeatRule) { repeatInterval % MONTH == 0 -> when (repeatRule) {
REPEAT_MONTH_SAME_DAY -> addMonthsWithSameDay(currStart, original) REPEAT_SAME_DAY -> addMonthsWithSameDay(currStart, original)
REPEAT_MONTH_ORDER_WEEKDAY_USE_LAST -> addXthDayInterval(currStart, original, true) REPEAT_ORDER_WEEKDAY_USE_LAST -> addXthDayInterval(currStart, original, true)
REPEAT_MONTH_ORDER_WEEKDAY -> addXthDayInterval(currStart, original, false) REPEAT_ORDER_WEEKDAY -> addXthDayInterval(currStart, original, false)
else -> currStart.plusMonths(repeatInterval / MONTH).dayOfMonth().withMaximumValue() else -> currStart.plusMonths(repeatInterval / MONTH).dayOfMonth().withMaximumValue()
} }
repeatInterval % WEEK == 0 -> { repeatInterval % WEEK == 0 -> {