diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/DBHelper.kt index 15619f22a..aa116cd4f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/DBHelper.kt @@ -7,25 +7,48 @@ import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteQueryBuilder import android.text.TextUtils +import com.simplemobiletools.calendar.extensions.getIntValue +import com.simplemobiletools.calendar.extensions.getStringValue import com.simplemobiletools.calendar.models.Event import org.joda.time.DateTime import java.util.* 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 { mDb = writableDatabase } override fun onCreate(db: SQLiteDatabase) { - db.execSQL("CREATE TABLE " + MAIN_TABLE_NAME + " (" + - COL_ID + " INTEGER PRIMARY KEY, " + - COL_START_TS + " INTEGER," + - COL_END_TS + " INTEGER," + - COL_TITLE + " TEXT," + - COL_DESCRIPTION + " TEXT," + - COL_REMINDER_MINUTES + " INTEGER" + - ")") + db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_START_TS INTEGER, $COL_END_TS INTEGER, $COL_TITLE TEXT, " + + "$COL_DESCRIPTION TEXT, $COL_REMINDER_MINUTES INTEGER)") createMetaTable(db) } @@ -41,14 +64,8 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n } private fun createMetaTable(db: SQLiteDatabase) { - db.execSQL("CREATE TABLE " + META_TABLE_NAME + " (" + - COL_ID + " INTEGER PRIMARY KEY, " + - COL_EVENT_ID + " INTEGER UNIQUE, " + - COL_REPEAT_START + " INTEGER, " + - COL_REPEAT_INTERVAL + " INTEGER, " + - COL_REPEAT_MONTH + " INTEGER, " + - COL_REPEAT_DAY + " INTEGER" + - ")") + db.execSQL("CREATE TABLE $META_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $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) { @@ -60,55 +77,53 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n mDb.insert(META_TABLE_NAME, null, metaValues) } - if (mCallback != null) - mCallback!!.eventInserted(event) + mCallback?.eventInserted(event) } fun update(event: Event) { val selectionArgs = arrayOf(event.id.toString()) val values = fillContentValues(event) - val selection = COL_ID + " = ?" + val selection = "$COL_ID = ?" mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) if (event.repeatInterval == 0) { - val metaSelection = COL_EVENT_ID + " = ?" + val metaSelection = "$COL_EVENT_ID = ?" mDb.delete(META_TABLE_NAME, metaSelection, selectionArgs) } else { val metaValues = fillMetaValues(event) 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 { - val values = ContentValues() - values.put(COL_START_TS, event.startTS) - values.put(COL_END_TS, event.endTS) - values.put(COL_TITLE, event.title) - values.put(COL_DESCRIPTION, event.description) - values.put(COL_REMINDER_MINUTES, event.reminderMinutes) - return values + return ContentValues().apply { + put(COL_START_TS, event.startTS) + put(COL_END_TS, event.endTS) + put(COL_TITLE, event.title) + put(COL_DESCRIPTION, event.description) + put(COL_REMINDER_MINUTES, event.reminderMinutes) + } } private fun fillMetaValues(event: Event): ContentValues { 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) - if (repeatInterval == Constants.MONTH || repeatInterval == Constants.YEAR) { - values.put(COL_REPEAT_DAY, dateTime.dayOfMonth) - } + return ContentValues().apply { + put(COL_EVENT_ID, event.id) + put(COL_REPEAT_START, event.startTS) + put(COL_REPEAT_INTERVAL, repeatInterval) - if (repeatInterval == Constants.YEAR) { - values.put(COL_REPEAT_MONTH, dateTime.monthOfYear) - } + if (repeatInterval == Constants.MONTH || repeatInterval == Constants.YEAR) { + put(COL_REPEAT_DAY, dateTime.dayOfMonth) + } - return values + if (repeatInterval == Constants.YEAR) { + put(COL_REPEAT_MONTH, dateTime.monthOfYear) + } + } } fun deleteEvents(ids: Array) { @@ -119,8 +134,7 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n val metaSelection = "$COL_EVENT_ID IN ($args)" mDb.delete(META_TABLE_NAME, metaSelection, null) - if (mCallback != null) - mCallback!!.eventsDeleted(ids.size) + mCallback?.eventsDeleted(ids.size) } fun getEvent(id: Int): Event? { @@ -147,8 +161,7 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n val cursor = getEventsCursor(selection, selectionArgs) events.addAll(fillEvents(cursor)) - if (mCallback != null) - mCallback!!.gotEvents(events) + mCallback?.gotEvents(events) } private fun getEventsFor(ts: Int): List { @@ -158,18 +171,17 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n val dateTime = Formatter.getDateTimeFromTS(ts) // get daily and weekly events - var selection = "(" + COL_REPEAT_INTERVAL + " = " + Constants.DAY + " OR " + COL_REPEAT_INTERVAL + " = " + Constants.WEEK + - ") AND (" + dayEnd + " - " + COL_REPEAT_START + ") % " + COL_REPEAT_INTERVAL + " BETWEEN 0 AND " + dayExclusive + var selection = "($COL_REPEAT_INTERVAL = ${Constants.DAY} OR $COL_REPEAT_INTERVAL = ${Constants.WEEK}) AND " + + "($dayEnd - $COL_REPEAT_START) % $COL_REPEAT_INTERVAL BETWEEN 0 AND $dayExclusive" newEvents.addAll(getEvents(selection, ts)) // get monthly events - selection = COL_REPEAT_INTERVAL + " = " + Constants.MONTH + " AND " + COL_REPEAT_DAY + " = " + dateTime.dayOfMonth + - " AND " + COL_REPEAT_START + " <= " + dayEnd + selection = "$COL_REPEAT_INTERVAL = ${Constants.MONTH} AND $COL_REPEAT_DAY = ${dateTime.dayOfMonth} AND $COL_REPEAT_START <= $dayEnd" newEvents.addAll(getEvents(selection, ts)) // get yearly events - selection = COL_REPEAT_INTERVAL + " = " + Constants.YEAR + " AND " + COL_REPEAT_MONTH + " = " + dateTime.monthOfYear + - " AND " + COL_REPEAT_DAY + " = " + dateTime.dayOfMonth + " AND " + COL_REPEAT_START + " <= " + dayEnd + selection = "$COL_REPEAT_INTERVAL = ${Constants.YEAR} AND $COL_REPEAT_MONTH = ${dateTime.monthOfYear}" + + " AND $COL_REPEAT_DAY = ${dateTime.dayOfMonth} AND $COL_REPEAT_START <= $dayEnd" newEvents.addAll(getEvents(selection, ts)) 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 currStart = Formatter.getDateTimeFromTS(e.startTS) val newStart: DateTime - if (e.repeatInterval == Constants.DAY) { - newStart = currStart.plusDays(periods) - } else if (e.repeatInterval == Constants.WEEK) { - newStart = currStart.plusWeeks(periods) - } else if (e.repeatInterval == Constants.MONTH) { - newStart = currStart.plusMonths(periods) - } else { - newStart = currStart.plusYears(periods) + newStart = when (e.repeatInterval) { + Constants.DAY -> currStart.plusDays(periods) + Constants.WEEK -> currStart.plusWeeks(periods) + Constants.MONTH -> currStart.plusMonths(periods) + else -> currStart.plusYears(periods) } val newStartTS = (newStart.millis / 1000).toInt() @@ -233,13 +242,13 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n if (cursor.moveToFirst()) { do { - val id = cursor.getInt(cursor.getColumnIndex(COL_ID)) - val startTS = cursor.getInt(cursor.getColumnIndex(COL_START_TS)) - val endTS = cursor.getInt(cursor.getColumnIndex(COL_END_TS)) - val reminderMinutes = cursor.getInt(cursor.getColumnIndex(COL_REMINDER_MINUTES)) - val repeatInterval = cursor.getInt(cursor.getColumnIndex(COL_REPEAT_INTERVAL)) - val title = cursor.getString(cursor.getColumnIndex(COL_TITLE)) - val description = cursor.getString(cursor.getColumnIndex(COL_DESCRIPTION)) + val id = cursor.getIntValue(COL_ID) + val startTS = cursor.getIntValue(COL_START_TS) + val endTS = cursor.getIntValue(COL_END_TS) + val reminderMinutes = cursor.getIntValue(COL_REMINDER_MINUTES) + val repeatInterval = cursor.getIntValue(COL_REPEAT_INTERVAL) + val title = cursor.getStringValue(COL_TITLE) + val description = cursor.getStringValue(COL_DESCRIPTION) events.add(Event(id, startTS, endTS, title, description, reminderMinutes, repeatInterval)) } while (cursor.moveToNext()) } @@ -256,32 +265,4 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n fun gotEvents(events: MutableList) } - - 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) - } - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Cursor.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Cursor.kt new file mode 100644 index 000000000..9edb567b2 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Cursor.kt @@ -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))