moving a few more functions into Room

This commit is contained in:
tibbi 2018-11-14 22:17:29 +01:00
parent 86a4b3fdb1
commit 771f66ac8e
12 changed files with 36 additions and 41 deletions

View file

@ -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 {

View file

@ -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()))

View file

@ -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())
}
}
}

View file

@ -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

View file

@ -453,7 +453,7 @@ fun Context.handleEventDeleting(eventIds: List<Long>, timestamps: List<Int>, act
}
DELETE_FUTURE_OCCURRENCES -> {
eventIds.forEachIndexed { index, value ->
dbHelper.addEventRepeatLimit(value, timestamps[index])
EventsHelper(this).addEventRepeatLimit(value, timestamps[index])
}
}
DELETE_ALL_OCCURRENCES -> {

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -44,7 +44,7 @@ class EventsHelper(val context: Context) {
fun getEventTypeWithCalDAVCalendarId(calendarId: Int) = context.eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId)
fun deleteEventTypes(eventTypes: ArrayList<EventType>, 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!!)
}
}
}
}

View file

@ -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

View file

@ -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<Long>
@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

View file

@ -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)