fix #206, allow repeating events every 30 days

This commit is contained in:
tibbi 2017-09-24 00:07:57 +02:00
parent add8e6f116
commit dd4c525880
5 changed files with 69 additions and 46 deletions

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.calendar.activities
import android.os.Bundle
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.dialogs.CustomEventReminderDialog
import com.simplemobiletools.calendar.dialogs.CustomEventRepeatIntervalDialog
@ -18,10 +17,6 @@ import java.util.TreeSet
import kotlin.collections.ArrayList
open class SimpleActivity : BaseSimpleActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
protected fun showEventReminderDialog(curMinutes: Int, callback: (minutes: Int) -> Unit) {
hideKeyboard()
val minutes = TreeSet<Int>()
@ -34,8 +29,7 @@ open class SimpleActivity : BaseSimpleActivity() {
}
val items = ArrayList<RadioItem>(minutes.size + 1)
minutes.mapIndexedTo(items, {
index, value ->
minutes.mapIndexedTo(items, { index, value ->
RadioItem(index, getFormattedMinutes(value), value)
})
@ -71,8 +65,7 @@ open class SimpleActivity : BaseSimpleActivity() {
}
val items = ArrayList<RadioItem>(seconds.size + 1)
seconds.mapIndexedTo(items, {
index, value ->
seconds.mapIndexedTo(items, { index, value ->
RadioItem(index, getRepetitionText(value), value)
})

View File

@ -5,12 +5,16 @@ import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import android.view.WindowManager
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.helpers.DAY
import com.simplemobiletools.calendar.helpers.MONTH
import com.simplemobiletools.calendar.helpers.WEEK
import com.simplemobiletools.calendar.helpers.YEAR
import com.simplemobiletools.commons.extensions.hideKeyboard
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.value
import kotlinx.android.synthetic.main.dialog_custom_event_repeat_interval.view.*
class CustomEventRepeatIntervalDialog(val activity: Activity, val callback: (seconds: Int) -> Unit) : AlertDialog.Builder(activity) {
class CustomEventRepeatIntervalDialog(val activity: Activity, val callback: (seconds: Int) -> Unit) {
var dialog: AlertDialog
var view = activity.layoutInflater.inflate(R.layout.dialog_custom_event_repeat_interval, null) as ViewGroup
@ -30,15 +34,15 @@ class CustomEventRepeatIntervalDialog(val activity: Activity, val callback: (sec
val value = view.dialog_custom_repeat_interval_value.value
val multiplier = getMultiplier(view.dialog_radio_view.checkedRadioButtonId)
val days = Integer.valueOf(if (value.isEmpty()) "0" else value)
callback.invoke(days * multiplier * 24 * 60 * 60)
callback(days * multiplier)
activity.hideKeyboard()
dialog.dismiss()
}
private fun getMultiplier(id: Int) = when (id) {
R.id.dialog_radio_weeks -> 7
R.id.dialog_radio_months -> 30
R.id.dialog_radio_years -> 365
else -> 1
R.id.dialog_radio_weeks -> WEEK
R.id.dialog_radio_months -> MONTH
R.id.dialog_radio_years -> YEAR
else -> DAY
}
}

View File

@ -126,39 +126,35 @@ fun Context.getFormattedMinutes(minutes: Int, showBefore: Boolean = true) = when
else -> {
if (minutes % 525600 == 0)
resources.getQuantityString(R.plurals.years, minutes / 525600, minutes / 525600)
if (minutes % 43200 == 0)
resources.getQuantityString(R.plurals.months, minutes / 43200, minutes / 43200)
else if (minutes % 10080 == 0)
resources.getQuantityString(R.plurals.weeks, minutes / 10080, minutes / 10080)
else if (minutes % 1440 == 0)
resources.getQuantityString(R.plurals.days, minutes / 1440, minutes / 1440)
else if (minutes % 60 == 0) {
val base = if (showBefore) R.plurals.hours_before else R.plurals.by_hours
resources.getQuantityString(base, minutes / 60, minutes / 60)
} else {
val base = if (showBefore) R.plurals.minutes_before else R.plurals.by_minutes
resources.getQuantityString(base, minutes, minutes)
when {
minutes % 43200 == 0 -> resources.getQuantityString(R.plurals.months, minutes / 43200, minutes / 43200)
minutes % 10080 == 0 -> resources.getQuantityString(R.plurals.weeks, minutes / 10080, minutes / 10080)
minutes % 1440 == 0 -> resources.getQuantityString(R.plurals.days, minutes / 1440, minutes / 1440)
minutes % 60 == 0 -> {
val base = if (showBefore) R.plurals.hours_before else R.plurals.by_hours
resources.getQuantityString(base, minutes / 60, minutes / 60)
}
else -> {
val base = if (showBefore) R.plurals.minutes_before else R.plurals.by_minutes
resources.getQuantityString(base, minutes, minutes)
}
}
}
}
fun Context.getRepetitionText(seconds: Int): String {
val days = seconds / 60 / 60 / 24
return when (days) {
0 -> getString(R.string.no_repetition)
1 -> getString(R.string.daily)
7 -> getString(R.string.weekly)
30 -> getString(R.string.monthly)
365 -> getString(R.string.yearly)
else -> {
if (days % 365 == 0)
resources.getQuantityString(R.plurals.years, days / 365, days / 365)
else if (days % 30 == 0)
resources.getQuantityString(R.plurals.months, days / 30, days / 30)
else if (days % 7 == 0)
resources.getQuantityString(R.plurals.weeks, days / 7, days / 7)
else
resources.getQuantityString(R.plurals.days, days, days)
fun Context.getRepetitionText(seconds: Int) = when (seconds) {
0 -> getString(R.string.no_repetition)
DAY -> getString(R.string.daily)
WEEK -> getString(R.string.weekly)
MONTH -> getString(R.string.monthly)
YEAR -> getString(R.string.yearly)
else -> {
when {
seconds % YEAR == 0 -> resources.getQuantityString(R.plurals.years, seconds / YEAR, seconds / YEAR)
seconds % MONTH == 0 -> resources.getQuantityString(R.plurals.months, seconds / MONTH, seconds / MONTH)
seconds % WEEK == 0 -> resources.getQuantityString(R.plurals.weeks, seconds / WEEK, seconds / WEEK)
else -> resources.getQuantityString(R.plurals.days, seconds / DAY, seconds / DAY)
}
}
}

View File

@ -23,7 +23,7 @@ val REMINDER_OFF = -1
val DAY = 86400
val WEEK = 604800
val MONTH = 2592000 // exact value not taken into account, Joda is used for adding months and years
val MONTH = 2592001 // exact value not taken into account, Joda is used for adding months and years
val YEAR = 31536000
val DAY_MINUTES = 24 * 60

View File

@ -64,7 +64,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private val mDb: SQLiteDatabase = writableDatabase
companion object {
private val DB_VERSION = 17
private val DB_VERSION = 18
val DB_NAME = "events.db"
val REGULAR_EVENT_TYPE_ID = 1
var dbInstance: DBHelper? = null
@ -166,6 +166,10 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
db.execSQL("ALTER TABLE $TYPES_TABLE_NAME ADD COLUMN $COL_TYPE_CALDAV_DISPLAY_NAME TEXT DEFAULT ''")
db.execSQL("ALTER TABLE $TYPES_TABLE_NAME ADD COLUMN $COL_TYPE_CALDAV_EMAIL TEXT DEFAULT ''")
}
if (oldVersion < 18) {
updateOldMonthlyEvents(db)
}
}
private fun createMetaTable(db: SQLiteDatabase) {
@ -441,6 +445,32 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return fillEvents(cursor).filter { it.importId.isNotEmpty() }
}
private fun updateOldMonthlyEvents(db: SQLiteDatabase) {
val OLD_MONTH = 2592000
val projection = arrayOf(COL_ID, COL_REPEAT_INTERVAL)
val selection = "$COL_REPEAT_INTERVAL != 0 AND $COL_REPEAT_INTERVAL % $OLD_MONTH == 0"
var cursor: Cursor? = null
try {
cursor = db.query(META_TABLE_NAME, projection, selection, null, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(COL_ID)
val repeatInterval = cursor.getIntValue(COL_REPEAT_INTERVAL)
val multiplies = repeatInterval / OLD_MONTH
val values = ContentValues().apply {
put(COL_REPEAT_INTERVAL, multiplies * MONTH)
}
val updateSelection = "$COL_ID = $id"
db.update(META_TABLE_NAME, values, updateSelection, null)
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
}
fun addEventRepeatException(parentEventId: Int, occurrenceTS: Int) {
fillExceptionValues(parentEventId, occurrenceTS) {
mDb.insert(EXCEPTIONS_TABLE_NAME, null, it)