mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-17 04:10:45 +01:00
some DBHelper refactoring
This commit is contained in:
parent
a552665960
commit
df3939f742
@ -7,25 +7,48 @@ import android.database.sqlite.SQLiteDatabase
|
|||||||
import android.database.sqlite.SQLiteOpenHelper
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
import android.database.sqlite.SQLiteQueryBuilder
|
import android.database.sqlite.SQLiteQueryBuilder
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
|
import com.simplemobiletools.calendar.extensions.getIntValue
|
||||||
|
import com.simplemobiletools.calendar.extensions.getStringValue
|
||||||
import com.simplemobiletools.calendar.models.Event
|
import com.simplemobiletools.calendar.models.Event
|
||||||
import org.joda.time.DateTime
|
import org.joda.time.DateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, null, DBHelper.DB_VERSION) {
|
class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, null, DBHelper.DB_VERSION) {
|
||||||
|
private val MAIN_TABLE_NAME = "events"
|
||||||
|
private val COL_ID = "id"
|
||||||
|
private val COL_START_TS = "start_ts"
|
||||||
|
private val COL_END_TS = "end_ts"
|
||||||
|
private val COL_TITLE = "title"
|
||||||
|
private val COL_DESCRIPTION = "description"
|
||||||
|
private val COL_REMINDER_MINUTES = "reminder_minutes"
|
||||||
|
|
||||||
|
private val META_TABLE_NAME = "events_meta"
|
||||||
|
private val COL_EVENT_ID = "event_id"
|
||||||
|
private val COL_REPEAT_START = "repeat_start"
|
||||||
|
private val COL_REPEAT_INTERVAL = "repeat_interval"
|
||||||
|
private val COL_REPEAT_MONTH = "repeat_month"
|
||||||
|
private val COL_REPEAT_DAY = "repeat_day"
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val DB_NAME = "events.db"
|
||||||
|
private val DB_VERSION = 3
|
||||||
|
|
||||||
|
private var mCallback: DBOperationsListener? = null
|
||||||
|
lateinit private var mDb: SQLiteDatabase
|
||||||
|
|
||||||
|
fun newInstance(context: Context, callback: DBOperationsListener): DBHelper {
|
||||||
|
mCallback = callback
|
||||||
|
return DBHelper(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
mDb = writableDatabase
|
mDb = writableDatabase
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreate(db: SQLiteDatabase) {
|
override fun onCreate(db: SQLiteDatabase) {
|
||||||
db.execSQL("CREATE TABLE " + MAIN_TABLE_NAME + " (" +
|
db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_START_TS INTEGER, $COL_END_TS INTEGER, $COL_TITLE TEXT, " +
|
||||||
COL_ID + " INTEGER PRIMARY KEY, " +
|
"$COL_DESCRIPTION TEXT, $COL_REMINDER_MINUTES INTEGER)")
|
||||||
COL_START_TS + " INTEGER," +
|
|
||||||
COL_END_TS + " INTEGER," +
|
|
||||||
COL_TITLE + " TEXT," +
|
|
||||||
COL_DESCRIPTION + " TEXT," +
|
|
||||||
COL_REMINDER_MINUTES + " INTEGER" +
|
|
||||||
")")
|
|
||||||
|
|
||||||
createMetaTable(db)
|
createMetaTable(db)
|
||||||
}
|
}
|
||||||
@ -41,14 +64,8 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createMetaTable(db: SQLiteDatabase) {
|
private fun createMetaTable(db: SQLiteDatabase) {
|
||||||
db.execSQL("CREATE TABLE " + META_TABLE_NAME + " (" +
|
db.execSQL("CREATE TABLE $META_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_EVENT_ID INTEGER UNIQUE, $COL_REPEAT_START INTEGER, " +
|
||||||
COL_ID + " INTEGER PRIMARY KEY, " +
|
"$COL_REPEAT_INTERVAL INTEGER, $COL_REPEAT_MONTH INTEGER, $COL_REPEAT_DAY INTEGER)")
|
||||||
COL_EVENT_ID + " INTEGER UNIQUE, " +
|
|
||||||
COL_REPEAT_START + " INTEGER, " +
|
|
||||||
COL_REPEAT_INTERVAL + " INTEGER, " +
|
|
||||||
COL_REPEAT_MONTH + " INTEGER, " +
|
|
||||||
COL_REPEAT_DAY + " INTEGER" +
|
|
||||||
")")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun insert(event: Event) {
|
fun insert(event: Event) {
|
||||||
@ -60,55 +77,53 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||||||
mDb.insert(META_TABLE_NAME, null, metaValues)
|
mDb.insert(META_TABLE_NAME, null, metaValues)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mCallback != null)
|
mCallback?.eventInserted(event)
|
||||||
mCallback!!.eventInserted(event)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun update(event: Event) {
|
fun update(event: Event) {
|
||||||
val selectionArgs = arrayOf(event.id.toString())
|
val selectionArgs = arrayOf(event.id.toString())
|
||||||
val values = fillContentValues(event)
|
val values = fillContentValues(event)
|
||||||
val selection = COL_ID + " = ?"
|
val selection = "$COL_ID = ?"
|
||||||
mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs)
|
mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs)
|
||||||
|
|
||||||
if (event.repeatInterval == 0) {
|
if (event.repeatInterval == 0) {
|
||||||
val metaSelection = COL_EVENT_ID + " = ?"
|
val metaSelection = "$COL_EVENT_ID = ?"
|
||||||
mDb.delete(META_TABLE_NAME, metaSelection, selectionArgs)
|
mDb.delete(META_TABLE_NAME, metaSelection, selectionArgs)
|
||||||
} else {
|
} else {
|
||||||
val metaValues = fillMetaValues(event)
|
val metaValues = fillMetaValues(event)
|
||||||
mDb.insertWithOnConflict(META_TABLE_NAME, null, metaValues, SQLiteDatabase.CONFLICT_REPLACE)
|
mDb.insertWithOnConflict(META_TABLE_NAME, null, metaValues, SQLiteDatabase.CONFLICT_REPLACE)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mCallback != null)
|
mCallback?.eventUpdated(event)
|
||||||
mCallback!!.eventUpdated(event)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fillContentValues(event: Event): ContentValues {
|
private fun fillContentValues(event: Event): ContentValues {
|
||||||
val values = ContentValues()
|
return ContentValues().apply {
|
||||||
values.put(COL_START_TS, event.startTS)
|
put(COL_START_TS, event.startTS)
|
||||||
values.put(COL_END_TS, event.endTS)
|
put(COL_END_TS, event.endTS)
|
||||||
values.put(COL_TITLE, event.title)
|
put(COL_TITLE, event.title)
|
||||||
values.put(COL_DESCRIPTION, event.description)
|
put(COL_DESCRIPTION, event.description)
|
||||||
values.put(COL_REMINDER_MINUTES, event.reminderMinutes)
|
put(COL_REMINDER_MINUTES, event.reminderMinutes)
|
||||||
return values
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fillMetaValues(event: Event): ContentValues {
|
private fun fillMetaValues(event: Event): ContentValues {
|
||||||
val repeatInterval = event.repeatInterval
|
val repeatInterval = event.repeatInterval
|
||||||
val values = ContentValues()
|
|
||||||
values.put(COL_EVENT_ID, event.id)
|
|
||||||
values.put(COL_REPEAT_START, event.startTS)
|
|
||||||
values.put(COL_REPEAT_INTERVAL, repeatInterval)
|
|
||||||
val dateTime = Formatter.getDateTimeFromTS(event.startTS)
|
val dateTime = Formatter.getDateTimeFromTS(event.startTS)
|
||||||
|
|
||||||
if (repeatInterval == Constants.MONTH || repeatInterval == Constants.YEAR) {
|
return ContentValues().apply {
|
||||||
values.put(COL_REPEAT_DAY, dateTime.dayOfMonth)
|
put(COL_EVENT_ID, event.id)
|
||||||
}
|
put(COL_REPEAT_START, event.startTS)
|
||||||
|
put(COL_REPEAT_INTERVAL, repeatInterval)
|
||||||
|
|
||||||
if (repeatInterval == Constants.YEAR) {
|
if (repeatInterval == Constants.MONTH || repeatInterval == Constants.YEAR) {
|
||||||
values.put(COL_REPEAT_MONTH, dateTime.monthOfYear)
|
put(COL_REPEAT_DAY, dateTime.dayOfMonth)
|
||||||
}
|
}
|
||||||
|
|
||||||
return values
|
if (repeatInterval == Constants.YEAR) {
|
||||||
|
put(COL_REPEAT_MONTH, dateTime.monthOfYear)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteEvents(ids: Array<String>) {
|
fun deleteEvents(ids: Array<String>) {
|
||||||
@ -119,8 +134,7 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||||||
val metaSelection = "$COL_EVENT_ID IN ($args)"
|
val metaSelection = "$COL_EVENT_ID IN ($args)"
|
||||||
mDb.delete(META_TABLE_NAME, metaSelection, null)
|
mDb.delete(META_TABLE_NAME, metaSelection, null)
|
||||||
|
|
||||||
if (mCallback != null)
|
mCallback?.eventsDeleted(ids.size)
|
||||||
mCallback!!.eventsDeleted(ids.size)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEvent(id: Int): Event? {
|
fun getEvent(id: Int): Event? {
|
||||||
@ -147,8 +161,7 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||||||
val cursor = getEventsCursor(selection, selectionArgs)
|
val cursor = getEventsCursor(selection, selectionArgs)
|
||||||
events.addAll(fillEvents(cursor))
|
events.addAll(fillEvents(cursor))
|
||||||
|
|
||||||
if (mCallback != null)
|
mCallback?.gotEvents(events)
|
||||||
mCallback!!.gotEvents(events)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getEventsFor(ts: Int): List<Event> {
|
private fun getEventsFor(ts: Int): List<Event> {
|
||||||
@ -158,18 +171,17 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||||||
val dateTime = Formatter.getDateTimeFromTS(ts)
|
val dateTime = Formatter.getDateTimeFromTS(ts)
|
||||||
|
|
||||||
// get daily and weekly events
|
// get daily and weekly events
|
||||||
var selection = "(" + COL_REPEAT_INTERVAL + " = " + Constants.DAY + " OR " + COL_REPEAT_INTERVAL + " = " + Constants.WEEK +
|
var selection = "($COL_REPEAT_INTERVAL = ${Constants.DAY} OR $COL_REPEAT_INTERVAL = ${Constants.WEEK}) AND " +
|
||||||
") AND (" + dayEnd + " - " + COL_REPEAT_START + ") % " + COL_REPEAT_INTERVAL + " BETWEEN 0 AND " + dayExclusive
|
"($dayEnd - $COL_REPEAT_START) % $COL_REPEAT_INTERVAL BETWEEN 0 AND $dayExclusive"
|
||||||
newEvents.addAll(getEvents(selection, ts))
|
newEvents.addAll(getEvents(selection, ts))
|
||||||
|
|
||||||
// get monthly events
|
// get monthly events
|
||||||
selection = COL_REPEAT_INTERVAL + " = " + Constants.MONTH + " AND " + COL_REPEAT_DAY + " = " + dateTime.dayOfMonth +
|
selection = "$COL_REPEAT_INTERVAL = ${Constants.MONTH} AND $COL_REPEAT_DAY = ${dateTime.dayOfMonth} AND $COL_REPEAT_START <= $dayEnd"
|
||||||
" AND " + COL_REPEAT_START + " <= " + dayEnd
|
|
||||||
newEvents.addAll(getEvents(selection, ts))
|
newEvents.addAll(getEvents(selection, ts))
|
||||||
|
|
||||||
// get yearly events
|
// get yearly events
|
||||||
selection = COL_REPEAT_INTERVAL + " = " + Constants.YEAR + " AND " + COL_REPEAT_MONTH + " = " + dateTime.monthOfYear +
|
selection = "$COL_REPEAT_INTERVAL = ${Constants.YEAR} AND $COL_REPEAT_MONTH = ${dateTime.monthOfYear}" +
|
||||||
" AND " + COL_REPEAT_DAY + " = " + dateTime.dayOfMonth + " AND " + COL_REPEAT_START + " <= " + dayEnd
|
" AND $COL_REPEAT_DAY = ${dateTime.dayOfMonth} AND $COL_REPEAT_START <= $dayEnd"
|
||||||
newEvents.addAll(getEvents(selection, ts))
|
newEvents.addAll(getEvents(selection, ts))
|
||||||
|
|
||||||
return newEvents
|
return newEvents
|
||||||
@ -193,14 +205,11 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||||||
val periods = (ts - e.startTS + Constants.DAY) / e.repeatInterval
|
val periods = (ts - e.startTS + Constants.DAY) / e.repeatInterval
|
||||||
val currStart = Formatter.getDateTimeFromTS(e.startTS)
|
val currStart = Formatter.getDateTimeFromTS(e.startTS)
|
||||||
val newStart: DateTime
|
val newStart: DateTime
|
||||||
if (e.repeatInterval == Constants.DAY) {
|
newStart = when (e.repeatInterval) {
|
||||||
newStart = currStart.plusDays(periods)
|
Constants.DAY -> currStart.plusDays(periods)
|
||||||
} else if (e.repeatInterval == Constants.WEEK) {
|
Constants.WEEK -> currStart.plusWeeks(periods)
|
||||||
newStart = currStart.plusWeeks(periods)
|
Constants.MONTH -> currStart.plusMonths(periods)
|
||||||
} else if (e.repeatInterval == Constants.MONTH) {
|
else -> currStart.plusYears(periods)
|
||||||
newStart = currStart.plusMonths(periods)
|
|
||||||
} else {
|
|
||||||
newStart = currStart.plusYears(periods)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val newStartTS = (newStart.millis / 1000).toInt()
|
val newStartTS = (newStart.millis / 1000).toInt()
|
||||||
@ -233,13 +242,13 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||||||
|
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
do {
|
do {
|
||||||
val id = cursor.getInt(cursor.getColumnIndex(COL_ID))
|
val id = cursor.getIntValue(COL_ID)
|
||||||
val startTS = cursor.getInt(cursor.getColumnIndex(COL_START_TS))
|
val startTS = cursor.getIntValue(COL_START_TS)
|
||||||
val endTS = cursor.getInt(cursor.getColumnIndex(COL_END_TS))
|
val endTS = cursor.getIntValue(COL_END_TS)
|
||||||
val reminderMinutes = cursor.getInt(cursor.getColumnIndex(COL_REMINDER_MINUTES))
|
val reminderMinutes = cursor.getIntValue(COL_REMINDER_MINUTES)
|
||||||
val repeatInterval = cursor.getInt(cursor.getColumnIndex(COL_REPEAT_INTERVAL))
|
val repeatInterval = cursor.getIntValue(COL_REPEAT_INTERVAL)
|
||||||
val title = cursor.getString(cursor.getColumnIndex(COL_TITLE))
|
val title = cursor.getStringValue(COL_TITLE)
|
||||||
val description = cursor.getString(cursor.getColumnIndex(COL_DESCRIPTION))
|
val description = cursor.getStringValue(COL_DESCRIPTION)
|
||||||
events.add(Event(id, startTS, endTS, title, description, reminderMinutes, repeatInterval))
|
events.add(Event(id, startTS, endTS, title, description, reminderMinutes, repeatInterval))
|
||||||
} while (cursor.moveToNext())
|
} while (cursor.moveToNext())
|
||||||
}
|
}
|
||||||
@ -256,32 +265,4 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||||||
|
|
||||||
fun gotEvents(events: MutableList<Event>)
|
fun gotEvents(events: MutableList<Event>)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
|
||||||
lateinit private var mDb: SQLiteDatabase
|
|
||||||
private var mCallback: DBOperationsListener? = null
|
|
||||||
|
|
||||||
private val DB_NAME = "events.db"
|
|
||||||
private val DB_VERSION = 3
|
|
||||||
|
|
||||||
private val MAIN_TABLE_NAME = "events"
|
|
||||||
private val COL_ID = "id"
|
|
||||||
private val COL_START_TS = "start_ts"
|
|
||||||
private val COL_END_TS = "end_ts"
|
|
||||||
private val COL_TITLE = "title"
|
|
||||||
private val COL_DESCRIPTION = "description"
|
|
||||||
private val COL_REMINDER_MINUTES = "reminder_minutes"
|
|
||||||
|
|
||||||
private val META_TABLE_NAME = "events_meta"
|
|
||||||
private val COL_EVENT_ID = "event_id"
|
|
||||||
private val COL_REPEAT_START = "repeat_start"
|
|
||||||
private val COL_REPEAT_INTERVAL = "repeat_interval"
|
|
||||||
private val COL_REPEAT_MONTH = "repeat_month"
|
|
||||||
private val COL_REPEAT_DAY = "repeat_day"
|
|
||||||
|
|
||||||
fun newInstance(context: Context, callback: DBOperationsListener): DBHelper {
|
|
||||||
mCallback = callback
|
|
||||||
return DBHelper(context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.simplemobiletools.calendar.extensions
|
||||||
|
|
||||||
|
import android.database.Cursor
|
||||||
|
|
||||||
|
fun Cursor.getStringValue(key: String) = getString(getColumnIndex(key))
|
||||||
|
|
||||||
|
fun Cursor.getIntValue(key: String) = getInt(getColumnIndex(key))
|
Loading…
x
Reference in New Issue
Block a user