From 771f66ac8eebe5944e4ead5232385892abd3e6db Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 14 Nov 2018 22:17:29 +0100 Subject: [PATCH] moving a few more functions into Room --- .../calendar/pro/activities/EventActivity.kt | 6 ++--- .../pro/adapters/ManageEventTypesAdapter.kt | 4 ++-- .../calendar/pro/databases/EventsDatabase.kt | 6 ++--- .../pro/dialogs/ImportEventsDialog.kt | 8 +++---- .../calendar/pro/extensions/Context.kt | 2 +- .../calendar/pro/helpers/Config.kt | 2 +- .../calendar/pro/helpers/Constants.kt | 1 + .../calendar/pro/helpers/DBHelper.kt | 22 ------------------- .../calendar/pro/helpers/EventsHelper.kt | 16 ++++++++++++-- .../calendar/pro/helpers/IcsImporter.kt | 4 ++-- .../calendar/pro/interfaces/EventsDao.kt | 4 ++++ .../calendar/pro/models/Event.kt | 2 +- 12 files changed, 36 insertions(+), 41 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 228e68318..2ded3cc41 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -47,7 +47,7 @@ class EventActivity : SimpleActivity() { private var mRepeatInterval = 0 private var mRepeatLimit = 0 private var mRepeatRule = 0 - private var mEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID + private var mEventTypeId = REGULAR_EVENT_TYPE_ID private var mDialogTheme = 0 private var mEventOccurrenceTS = 0 private var mEventCalendarId = STORED_LOCALLY_ONLY @@ -82,7 +82,7 @@ class EventActivity : SimpleActivity() { private fun gotEvent(savedInstanceState: Bundle?, localEventType: EventType?, event: Event?) { if (localEventType == null || localEventType.caldavCalendarId != 0) { - config.lastUsedLocalEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID + config.lastUsedLocalEventTypeId = REGULAR_EVENT_TYPE_ID } mEventTypeId = config.lastUsedLocalEventTypeId @@ -681,7 +681,7 @@ class EventActivity : SimpleActivity() { Thread { when (it) { DELETE_SELECTED_OCCURRENCE -> dbHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true) - DELETE_FUTURE_OCCURRENCES -> dbHelper.addEventRepeatLimit(mEvent.id!!, mEventOccurrenceTS) + DELETE_FUTURE_OCCURRENCES -> EventsHelper(applicationContext).addEventRepeatLimit(mEvent.id!!, mEventOccurrenceTS) DELETE_ALL_OCCURRENCES -> EventsHelper(applicationContext).deleteEvent(mEvent.id!!, true) } runOnUiThread { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/ManageEventTypesAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/ManageEventTypesAdapter.kt index 409a2960d..5e15f82aa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/ManageEventTypesAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/ManageEventTypesAdapter.kt @@ -7,7 +7,7 @@ import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.activities.SimpleActivity import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.dbHelper -import com.simplemobiletools.calendar.pro.helpers.DBHelper +import com.simplemobiletools.calendar.pro.helpers.REGULAR_EVENT_TYPE_ID import com.simplemobiletools.calendar.pro.interfaces.DeleteEventTypesListener import com.simplemobiletools.calendar.pro.models.EventType import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter @@ -100,7 +100,7 @@ class ManageEventTypesAdapter(activity: SimpleActivity, val eventTypes: ArrayLis for (key in selectedKeys) { val type = getItemWithKey(key) ?: continue - if (type.id == DBHelper.REGULAR_EVENT_TYPE_ID) { + if (type.id == REGULAR_EVENT_TYPE_ID) { activity.toast(R.string.cannot_delete_default_type) eventTypesToDelete.remove(type) toggleItemSelection(false, getItemKeyPosition(type.id!!.toInt())) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt index 204f9a91c..35cd91fdc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt @@ -7,7 +7,7 @@ import androidx.room.RoomDatabase import androidx.sqlite.db.SupportSQLiteDatabase import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.extensions.config -import com.simplemobiletools.calendar.pro.helpers.DBHelper +import com.simplemobiletools.calendar.pro.helpers.REGULAR_EVENT_TYPE_ID import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionExceptionsDao import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionsDao import com.simplemobiletools.calendar.pro.interfaces.EventTypesDao @@ -58,9 +58,9 @@ abstract class EventsDatabase : RoomDatabase() { private fun insertRegularEventType(context: Context) { Executors.newSingleThreadScheduledExecutor().execute { val regularEvent = context.resources.getString(R.string.regular_event) - val eventType = EventType(DBHelper.REGULAR_EVENT_TYPE_ID, regularEvent, context.config.primaryColor) + val eventType = EventType(REGULAR_EVENT_TYPE_ID, regularEvent, context.config.primaryColor) db!!.EventTypesDao().insertOrUpdate(eventType) - context.config.addDisplayEventType(DBHelper.REGULAR_EVENT_TYPE_ID.toString()) + context.config.addDisplayEventType(REGULAR_EVENT_TYPE_ID.toString()) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt index 2a362eb36..056b01765 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt @@ -6,24 +6,24 @@ import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.activities.SimpleActivity import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.eventTypesDB -import com.simplemobiletools.calendar.pro.helpers.DBHelper import com.simplemobiletools.calendar.pro.helpers.EventsHelper import com.simplemobiletools.calendar.pro.helpers.IcsImporter import com.simplemobiletools.calendar.pro.helpers.IcsImporter.ImportResult.* +import com.simplemobiletools.calendar.pro.helpers.REGULAR_EVENT_TYPE_ID import com.simplemobiletools.commons.extensions.setFillWithStroke import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.toast import kotlinx.android.synthetic.main.dialog_import_events.view.* class ImportEventsDialog(val activity: SimpleActivity, val path: String, val callback: (refreshView: Boolean) -> Unit) { - var currEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID + var currEventTypeId = REGULAR_EVENT_TYPE_ID var currEventTypeCalDAVCalendarId = 0 val config = activity.config init { Thread { if (activity.eventTypesDB.getEventTypeWithId(config.lastUsedLocalEventTypeId) == null) { - config.lastUsedLocalEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID + config.lastUsedLocalEventTypeId = REGULAR_EVENT_TYPE_ID } activity.runOnUiThread { initDialog() @@ -39,7 +39,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal currEventTypeCalDAVCalendarId = config.lastUsedCaldavCalendarId lastUsedCalDAVCalendar.id!! } else { - DBHelper.REGULAR_EVENT_TYPE_ID + REGULAR_EVENT_TYPE_ID } } else { config.lastUsedLocalEventTypeId diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index ae5342595..0840f0c4f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -453,7 +453,7 @@ fun Context.handleEventDeleting(eventIds: List, timestamps: List, act } DELETE_FUTURE_OCCURRENCES -> { eventIds.forEachIndexed { index, value -> - dbHelper.addEventRepeatLimit(value, timestamps[index]) + EventsHelper(this).addEventRepeatLimit(value, timestamps[index]) } } DELETE_ALL_OCCURRENCES -> { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Config.kt index 8f6affa45..99ba11062 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Config.kt @@ -96,7 +96,7 @@ class Config(context: Context) : BaseConfig(context) { set(calendarId) = prefs.edit().putInt(LAST_USED_CALDAV_CALENDAR, calendarId).apply() var lastUsedLocalEventTypeId: Long - get() = prefs.getLong(LAST_USED_LOCAL_EVENT_TYPE_ID, DBHelper.REGULAR_EVENT_TYPE_ID) + get() = prefs.getLong(LAST_USED_LOCAL_EVENT_TYPE_ID, REGULAR_EVENT_TYPE_ID) set(lastUsedLocalEventTypeId) = prefs.edit().putLong(LAST_USED_LOCAL_EVENT_TYPE_ID, lastUsedLocalEventTypeId).apply() var reminderAudioStream: Int diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt index c2b6ef8c6..d786aa778 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt @@ -15,6 +15,7 @@ const val NEW_EVENT_SET_HOUR_DURATION = "new_event_set_hour_duration" const val WEEK_START_DATE_TIME = "week_start_date_time" const val CALDAV = "Caldav" const val VIEW_TO_OPEN = "view_to_open" +const val REGULAR_EVENT_TYPE_ID = 1L const val MONTHLY_VIEW = 1 const val YEARLY_VIEW = 2 diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt index 358b2f48e..a19246357 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt @@ -39,7 +39,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont companion object { private const val DB_VERSION = 1 const val DB_NAME = "events_old.db" - const val REGULAR_EVENT_TYPE_ID = 1L var dbInstance: DBHelper? = null fun newInstance(context: Context): DBHelper { @@ -101,18 +100,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } } - fun addEventRepeatLimit(eventId: Long, limitTS: Int) { - val time = Formatter.getDateTimeFromTS(limitTS) - context.eventRepetitionsDB.updateEventRepetitionLimit(limitTS - time.hourOfDay, eventId) - - if (context.config.caldavSync) { - val event = context.eventsDB.getEventWithId(eventId) - if (event?.getCalDAVCalendarId() != 0) { - CalDAVHandler(context).updateCalDAVEvent(event!!) - } - } - } - fun deleteEventsWithType(eventTypeId: Long) { val selection = "$MAIN_TABLE_NAME.$COL_EVENT_TYPE = ?" val selectionArgs = arrayOf(eventTypeId.toString()) @@ -122,15 +109,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont EventsHelper(context).deleteEvents(eventIDs, true) } - fun resetEventsWithType(eventTypeId: Long) { - val values = ContentValues() - values.put(COL_EVENT_TYPE, REGULAR_EVENT_TYPE_ID) - - val selection = "$COL_EVENT_TYPE = ?" - val selectionArgs = arrayOf(eventTypeId.toString()) - mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) - } - fun updateEventImportIdAndSource(eventId: Long, importId: String, source: String) { val values = ContentValues() values.put(COL_IMPORT_ID, importId) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index 222307a0b..fd0e7db43 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -44,7 +44,7 @@ class EventsHelper(val context: Context) { fun getEventTypeWithCalDAVCalendarId(calendarId: Int) = context.eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId) fun deleteEventTypes(eventTypes: ArrayList, deleteEvents: Boolean) { - val typesToDelete = eventTypes.asSequence().filter { it.caldavCalendarId == 0 && it.id != DBHelper.REGULAR_EVENT_TYPE_ID }.toMutableList() + val typesToDelete = eventTypes.asSequence().filter { it.caldavCalendarId == 0 && it.id != REGULAR_EVENT_TYPE_ID }.toMutableList() val deleteIds = typesToDelete.map { it.id }.toMutableList() val deletedSet = deleteIds.map { it.toString() }.toHashSet() context.config.removeDisplayEventTypes(deletedSet) @@ -57,7 +57,7 @@ class EventsHelper(val context: Context) { if (deleteEvents) { context.dbHelper.deleteEventsWithType(eventTypeId!!) } else { - context.dbHelper.resetEventsWithType(eventTypeId!!) + context.eventsDB.resetEventsWithType(eventTypeId!!) } } @@ -167,4 +167,16 @@ class EventsHelper(val context: Context) { deleteEvents(childIds, deleteFromCalDAV) } } + + fun addEventRepeatLimit(eventId: Long, limitTS: Int) { + val time = Formatter.getDateTimeFromTS(limitTS) + context.eventRepetitionsDB.updateEventRepetitionLimit(limitTS - time.hourOfDay, eventId) + + if (context.config.caldavSync) { + val event = context.eventsDB.getEventWithId(eventId) + if (event?.getCalDAVCalendarId() != 0) { + CalDAVHandler(context).updateCalDAVEvent(event!!) + } + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt index 43916feac..59fcca212 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt @@ -29,7 +29,7 @@ class IcsImporter(val activity: SimpleActivity) { private var curRepeatInterval = 0 private var curRepeatLimit = 0 private var curRepeatRule = 0 - private var curEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID + private var curEventTypeId = REGULAR_EVENT_TYPE_ID private var curLastModified = 0L private var curCategoryColor = -2 private var isNotificationDescription = false @@ -255,7 +255,7 @@ class IcsImporter(val activity: SimpleActivity) { curRepeatInterval = 0 curRepeatLimit = 0 curRepeatRule = 0 - curEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID + curEventTypeId = REGULAR_EVENT_TYPE_ID curLastModified = 0L curCategoryColor = -2 isNotificationDescription = false diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt index 8509fe934..261fb8446 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt @@ -4,6 +4,7 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import com.simplemobiletools.calendar.pro.helpers.REGULAR_EVENT_TYPE_ID import com.simplemobiletools.calendar.pro.helpers.SOURCE_CONTACT_ANNIVERSARY import com.simplemobiletools.calendar.pro.helpers.SOURCE_CONTACT_BIRTHDAY import com.simplemobiletools.calendar.pro.models.Event @@ -43,6 +44,9 @@ interface EventsDao { @Query("SELECT id FROM events WHERE source = :source AND import_id != \"\"") fun getCalDAVCalendarEvents(source: String): List + @Query("UPDATE events SET event_type = $REGULAR_EVENT_TYPE_ID WHERE event_type = :eventTypeId") + fun resetEventsWithType(eventTypeId: Long) + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertOrUpdate(event: Event): Long diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt index fa21e9185..8f9a8689e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt @@ -28,7 +28,7 @@ data class Event( @ColumnInfo(name = "repeat_limit") var repeatLimit: Int = 0, @ColumnInfo(name = "import_id") var importId: String = "", @ColumnInfo(name = "flags") var flags: Int = 0, - @ColumnInfo(name = "event_type") var eventType: Long = DBHelper.REGULAR_EVENT_TYPE_ID, + @ColumnInfo(name = "event_type") var eventType: Long = REGULAR_EVENT_TYPE_ID, @ColumnInfo(name = "parent_id") var parentId: Long = 0, @ColumnInfo(name = "last_updated") var lastUpdated: Long = 0L, @ColumnInfo(name = "source") var source: String = SOURCE_SIMPLE_CALENDAR)