mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-07 05:55:26 +01:00
fix #206, allow repeating events every 30 days
This commit is contained in:
parent
add8e6f116
commit
dd4c525880
@ -1,6 +1,5 @@
|
|||||||
package com.simplemobiletools.calendar.activities
|
package com.simplemobiletools.calendar.activities
|
||||||
|
|
||||||
import android.os.Bundle
|
|
||||||
import com.simplemobiletools.calendar.R
|
import com.simplemobiletools.calendar.R
|
||||||
import com.simplemobiletools.calendar.dialogs.CustomEventReminderDialog
|
import com.simplemobiletools.calendar.dialogs.CustomEventReminderDialog
|
||||||
import com.simplemobiletools.calendar.dialogs.CustomEventRepeatIntervalDialog
|
import com.simplemobiletools.calendar.dialogs.CustomEventRepeatIntervalDialog
|
||||||
@ -18,10 +17,6 @@ import java.util.TreeSet
|
|||||||
import kotlin.collections.ArrayList
|
import kotlin.collections.ArrayList
|
||||||
|
|
||||||
open class SimpleActivity : BaseSimpleActivity() {
|
open class SimpleActivity : BaseSimpleActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
|
||||||
super.onCreate(savedInstanceState)
|
|
||||||
}
|
|
||||||
|
|
||||||
protected fun showEventReminderDialog(curMinutes: Int, callback: (minutes: Int) -> Unit) {
|
protected fun showEventReminderDialog(curMinutes: Int, callback: (minutes: Int) -> Unit) {
|
||||||
hideKeyboard()
|
hideKeyboard()
|
||||||
val minutes = TreeSet<Int>()
|
val minutes = TreeSet<Int>()
|
||||||
@ -34,8 +29,7 @@ open class SimpleActivity : BaseSimpleActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val items = ArrayList<RadioItem>(minutes.size + 1)
|
val items = ArrayList<RadioItem>(minutes.size + 1)
|
||||||
minutes.mapIndexedTo(items, {
|
minutes.mapIndexedTo(items, { index, value ->
|
||||||
index, value ->
|
|
||||||
RadioItem(index, getFormattedMinutes(value), value)
|
RadioItem(index, getFormattedMinutes(value), value)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -71,8 +65,7 @@ open class SimpleActivity : BaseSimpleActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val items = ArrayList<RadioItem>(seconds.size + 1)
|
val items = ArrayList<RadioItem>(seconds.size + 1)
|
||||||
seconds.mapIndexedTo(items, {
|
seconds.mapIndexedTo(items, { index, value ->
|
||||||
index, value ->
|
|
||||||
RadioItem(index, getRepetitionText(value), value)
|
RadioItem(index, getRepetitionText(value), value)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -5,12 +5,16 @@ import android.support.v7.app.AlertDialog
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import com.simplemobiletools.calendar.R
|
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.hideKeyboard
|
||||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||||
import com.simplemobiletools.commons.extensions.value
|
import com.simplemobiletools.commons.extensions.value
|
||||||
import kotlinx.android.synthetic.main.dialog_custom_event_repeat_interval.view.*
|
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 dialog: AlertDialog
|
||||||
var view = activity.layoutInflater.inflate(R.layout.dialog_custom_event_repeat_interval, null) as ViewGroup
|
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 value = view.dialog_custom_repeat_interval_value.value
|
||||||
val multiplier = getMultiplier(view.dialog_radio_view.checkedRadioButtonId)
|
val multiplier = getMultiplier(view.dialog_radio_view.checkedRadioButtonId)
|
||||||
val days = Integer.valueOf(if (value.isEmpty()) "0" else value)
|
val days = Integer.valueOf(if (value.isEmpty()) "0" else value)
|
||||||
callback.invoke(days * multiplier * 24 * 60 * 60)
|
callback(days * multiplier)
|
||||||
activity.hideKeyboard()
|
activity.hideKeyboard()
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getMultiplier(id: Int) = when (id) {
|
private fun getMultiplier(id: Int) = when (id) {
|
||||||
R.id.dialog_radio_weeks -> 7
|
R.id.dialog_radio_weeks -> WEEK
|
||||||
R.id.dialog_radio_months -> 30
|
R.id.dialog_radio_months -> MONTH
|
||||||
R.id.dialog_radio_years -> 365
|
R.id.dialog_radio_years -> YEAR
|
||||||
else -> 1
|
else -> DAY
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,39 +126,35 @@ fun Context.getFormattedMinutes(minutes: Int, showBefore: Boolean = true) = when
|
|||||||
else -> {
|
else -> {
|
||||||
if (minutes % 525600 == 0)
|
if (minutes % 525600 == 0)
|
||||||
resources.getQuantityString(R.plurals.years, minutes / 525600, minutes / 525600)
|
resources.getQuantityString(R.plurals.years, minutes / 525600, minutes / 525600)
|
||||||
if (minutes % 43200 == 0)
|
|
||||||
resources.getQuantityString(R.plurals.months, minutes / 43200, minutes / 43200)
|
when {
|
||||||
else if (minutes % 10080 == 0)
|
minutes % 43200 == 0 -> resources.getQuantityString(R.plurals.months, minutes / 43200, minutes / 43200)
|
||||||
resources.getQuantityString(R.plurals.weeks, minutes / 10080, minutes / 10080)
|
minutes % 10080 == 0 -> resources.getQuantityString(R.plurals.weeks, minutes / 10080, minutes / 10080)
|
||||||
else if (minutes % 1440 == 0)
|
minutes % 1440 == 0 -> resources.getQuantityString(R.plurals.days, minutes / 1440, minutes / 1440)
|
||||||
resources.getQuantityString(R.plurals.days, minutes / 1440, minutes / 1440)
|
minutes % 60 == 0 -> {
|
||||||
else if (minutes % 60 == 0) {
|
val base = if (showBefore) R.plurals.hours_before else R.plurals.by_hours
|
||||||
val base = if (showBefore) R.plurals.hours_before else R.plurals.by_hours
|
resources.getQuantityString(base, minutes / 60, minutes / 60)
|
||||||
resources.getQuantityString(base, minutes / 60, minutes / 60)
|
}
|
||||||
} else {
|
else -> {
|
||||||
val base = if (showBefore) R.plurals.minutes_before else R.plurals.by_minutes
|
val base = if (showBefore) R.plurals.minutes_before else R.plurals.by_minutes
|
||||||
resources.getQuantityString(base, minutes, minutes)
|
resources.getQuantityString(base, minutes, minutes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getRepetitionText(seconds: Int): String {
|
fun Context.getRepetitionText(seconds: Int) = when (seconds) {
|
||||||
val days = seconds / 60 / 60 / 24
|
0 -> getString(R.string.no_repetition)
|
||||||
return when (days) {
|
DAY -> getString(R.string.daily)
|
||||||
0 -> getString(R.string.no_repetition)
|
WEEK -> getString(R.string.weekly)
|
||||||
1 -> getString(R.string.daily)
|
MONTH -> getString(R.string.monthly)
|
||||||
7 -> getString(R.string.weekly)
|
YEAR -> getString(R.string.yearly)
|
||||||
30 -> getString(R.string.monthly)
|
else -> {
|
||||||
365 -> getString(R.string.yearly)
|
when {
|
||||||
else -> {
|
seconds % YEAR == 0 -> resources.getQuantityString(R.plurals.years, seconds / YEAR, seconds / YEAR)
|
||||||
if (days % 365 == 0)
|
seconds % MONTH == 0 -> resources.getQuantityString(R.plurals.months, seconds / MONTH, seconds / MONTH)
|
||||||
resources.getQuantityString(R.plurals.years, days / 365, days / 365)
|
seconds % WEEK == 0 -> resources.getQuantityString(R.plurals.weeks, seconds / WEEK, seconds / WEEK)
|
||||||
else if (days % 30 == 0)
|
else -> resources.getQuantityString(R.plurals.days, seconds / DAY, seconds / DAY)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ val REMINDER_OFF = -1
|
|||||||
|
|
||||||
val DAY = 86400
|
val DAY = 86400
|
||||||
val WEEK = 604800
|
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 YEAR = 31536000
|
||||||
|
|
||||||
val DAY_MINUTES = 24 * 60
|
val DAY_MINUTES = 24 * 60
|
||||||
|
@ -64,7 +64,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||||||
private val mDb: SQLiteDatabase = writableDatabase
|
private val mDb: SQLiteDatabase = writableDatabase
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val DB_VERSION = 17
|
private val DB_VERSION = 18
|
||||||
val DB_NAME = "events.db"
|
val DB_NAME = "events.db"
|
||||||
val REGULAR_EVENT_TYPE_ID = 1
|
val REGULAR_EVENT_TYPE_ID = 1
|
||||||
var dbInstance: DBHelper? = null
|
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_DISPLAY_NAME TEXT DEFAULT ''")
|
||||||
db.execSQL("ALTER TABLE $TYPES_TABLE_NAME ADD COLUMN $COL_TYPE_CALDAV_EMAIL 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) {
|
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() }
|
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) {
|
fun addEventRepeatException(parentEventId: Int, occurrenceTS: Int) {
|
||||||
fillExceptionValues(parentEventId, occurrenceTS) {
|
fillExceptionValues(parentEventId, occurrenceTS) {
|
||||||
mDb.insert(EXCEPTIONS_TABLE_NAME, null, it)
|
mDb.insert(EXCEPTIONS_TABLE_NAME, null, it)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user