mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-26 16:37:46 +01:00
moving event type deleting in Room
This commit is contained in:
parent
4ed323e60e
commit
aa6ae0811a
@ -251,7 +251,7 @@ class EventActivity : SimpleActivity() {
|
||||
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
|
||||
event_title.requestFocus()
|
||||
updateActionBarTitle(getString(R.string.new_event))
|
||||
val isLastCaldavCalendarOK = config.caldavSync && config.getSyncedCalendarIdsAsList().contains(config.lastUsedCaldavCalendarId.toString())
|
||||
val isLastCaldavCalendarOK = config.caldavSync && config.getSyncedCalendarIdsAsList().contains(config.lastUsedCaldavCalendarId)
|
||||
mEventCalendarId = if (isLastCaldavCalendarOK) config.lastUsedCaldavCalendarId else STORED_LOCALLY_ONLY
|
||||
|
||||
if (intent.action == Intent.ACTION_EDIT || intent.action == Intent.ACTION_INSERT) {
|
||||
@ -600,7 +600,7 @@ class EventActivity : SimpleActivity() {
|
||||
event_caldav_calendar_divider.beVisible()
|
||||
|
||||
val calendars = CalDAVHandler(applicationContext).getCalDAVCalendars(this).filter {
|
||||
it.canWrite() && config.getSyncedCalendarIdsAsList().contains(it.id.toString())
|
||||
it.canWrite() && config.getSyncedCalendarIdsAsList().contains(it.id)
|
||||
}
|
||||
updateCurrentCalendarInfo(if (mEventCalendarId == STORED_LOCALLY_ONLY) null else getCalendarWithId(calendars, getCalendarId()))
|
||||
|
||||
|
@ -6,7 +6,6 @@ import android.view.MenuItem
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.adapters.ManageEventTypesAdapter
|
||||
import com.simplemobiletools.calendar.pro.dialogs.EditEventTypeDialog
|
||||
import com.simplemobiletools.calendar.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.pro.helpers.EventTypesHelper
|
||||
import com.simplemobiletools.calendar.pro.interfaces.DeleteEventTypesListener
|
||||
import com.simplemobiletools.calendar.pro.models.EventType
|
||||
@ -61,11 +60,7 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteEventTypesListener {
|
||||
}
|
||||
|
||||
Thread {
|
||||
dbHelper.deleteEventTypes(eventTypes, deleteEvents) {
|
||||
if (it == 0) {
|
||||
toast(R.string.unknown_error_occurred)
|
||||
}
|
||||
}
|
||||
EventTypesHelper().deleteEventTypes(applicationContext, eventTypes, deleteEvents)
|
||||
}.start()
|
||||
return true
|
||||
}
|
||||
|
@ -4,13 +4,9 @@ import android.content.Intent
|
||||
import android.content.res.Resources
|
||||
import android.media.AudioManager
|
||||
import android.os.Bundle
|
||||
import android.text.TextUtils
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.dialogs.SelectCalendarsDialog
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
import com.simplemobiletools.calendar.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.pro.extensions.getSyncedCalDAVCalendars
|
||||
import com.simplemobiletools.calendar.pro.extensions.updateWidgets
|
||||
import com.simplemobiletools.calendar.pro.extensions.*
|
||||
import com.simplemobiletools.calendar.pro.helpers.*
|
||||
import com.simplemobiletools.calendar.pro.models.EventType
|
||||
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
|
||||
@ -188,7 +184,7 @@ class SettingsActivity : SimpleActivity() {
|
||||
config.getSyncedCalendarIdsAsList().forEach {
|
||||
CalDAVHandler(applicationContext).deleteCalDAVCalendarEvents(it.toLong())
|
||||
}
|
||||
dbHelper.deleteEventTypesWithCalendarId(config.caldavSyncedCalendarIDs)
|
||||
eventTypesDB.deleteEventTypesWithCalendarId(config.getSyncedCalendarIdsAsList())
|
||||
}.start()
|
||||
}
|
||||
}
|
||||
@ -227,11 +223,12 @@ class SettingsActivity : SimpleActivity() {
|
||||
val removedCalendarIds = oldCalendarIds.filter { !newCalendarIds.contains(it) }
|
||||
removedCalendarIds.forEach {
|
||||
CalDAVHandler(applicationContext).deleteCalDAVCalendarEvents(it.toLong())
|
||||
EventTypesHelper().getEventTypeWithCalDAVCalendarId(applicationContext, it.toInt())?.apply {
|
||||
dbHelper.deleteEventTypes(arrayListOf(this), true) {}
|
||||
EventTypesHelper().getEventTypeWithCalDAVCalendarId(applicationContext, it)?.apply {
|
||||
EventTypesHelper().deleteEventTypes(applicationContext, arrayListOf(this), true)
|
||||
}
|
||||
}
|
||||
dbHelper.deleteEventTypesWithCalendarId(TextUtils.join(",", removedCalendarIds))
|
||||
|
||||
eventTypesDB.deleteEventTypesWithCalendarId(removedCalendarIds)
|
||||
if (settings_caldav_sync.isChecked) {
|
||||
toast(R.string.synchronization_completed)
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal
|
||||
}
|
||||
|
||||
private fun initDialog() {
|
||||
val isLastCaldavCalendarOK = config.caldavSync && config.getSyncedCalendarIdsAsList().contains(config.lastUsedCaldavCalendarId.toString())
|
||||
val isLastCaldavCalendarOK = config.caldavSync && config.getSyncedCalendarIdsAsList().contains(config.lastUsedCaldavCalendarId)
|
||||
currEventTypeId = if (isLastCaldavCalendarOK) {
|
||||
val lastUsedCalDAVCalendar = EventTypesHelper().getEventTypeWithCalDAVCalendarId(activity, config.lastUsedCaldavCalendarId)
|
||||
if (lastUsedCalDAVCalendar != null) {
|
||||
|
@ -29,7 +29,7 @@ class SelectCalendarsDialog(val activity: SimpleActivity, val callback: () -> Un
|
||||
addCalendarItem(false, it.accountName)
|
||||
}
|
||||
|
||||
addCalendarItem(true, it.displayName, it.id, ids.contains(it.id.toString()))
|
||||
addCalendarItem(true, it.displayName, it.id, ids.contains(it.id))
|
||||
}
|
||||
|
||||
dialog = AlertDialog.Builder(activity)
|
||||
|
@ -119,7 +119,7 @@ class Config(context: Context) : BaseConfig(context) {
|
||||
get() = prefs.getBoolean(DIM_PAST_EVENTS, true)
|
||||
set(dimPastEvents) = prefs.edit().putBoolean(DIM_PAST_EVENTS, dimPastEvents).apply()
|
||||
|
||||
fun getSyncedCalendarIdsAsList() = caldavSyncedCalendarIDs.split(",").filter { it.trim().isNotEmpty() } as ArrayList<String>
|
||||
fun getSyncedCalendarIdsAsList() = caldavSyncedCalendarIDs.split(",").filter { it.trim().isNotEmpty() }.map { Integer.parseInt(it) }.toMutableList() as ArrayList<Int>
|
||||
|
||||
fun addDisplayEventType(type: String) {
|
||||
addDisplayEventTypes(HashSet<String>(Arrays.asList(type)))
|
||||
|
@ -16,8 +16,6 @@ import com.simplemobiletools.commons.extensions.getIntValue
|
||||
import com.simplemobiletools.commons.extensions.getLongValue
|
||||
import com.simplemobiletools.commons.extensions.getStringValue
|
||||
import org.joda.time.DateTime
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
|
||||
private val MAIN_TABLE_NAME = "events"
|
||||
@ -42,13 +40,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
private val COL_REPEAT_RULE = "repeat_rule"
|
||||
private val COL_REPEAT_LIMIT = "repeat_limit"
|
||||
|
||||
private val TYPES_TABLE_NAME = "event_types"
|
||||
private val COL_TYPE_TITLE = "event_type_title"
|
||||
private val COL_TYPE_COLOR = "event_type_color"
|
||||
private val COL_TYPE_CALDAV_CALENDAR_ID = "event_caldav_calendar_id"
|
||||
private val COL_TYPE_CALDAV_DISPLAY_NAME = "event_caldav_display_name"
|
||||
private val COL_TYPE_CALDAV_EMAIL = "event_caldav_email"
|
||||
|
||||
private val REPEAT_EXCEPTIONS_TABLE_NAME = "event_repeat_exceptions"
|
||||
private val COL_OCCURRENCE_DAYCODE = "event_occurrence_daycode"
|
||||
private val COL_PARENT_EVENT_ID = "event_parent_id"
|
||||
@ -76,7 +67,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
"$COL_PARENT_EVENT_ID INTEGER, $COL_LAST_UPDATED INTEGER, $COL_EVENT_SOURCE TEXT, $COL_LOCATION TEXT)")
|
||||
|
||||
createRepetitionsTable(db)
|
||||
createTypesTable(db)
|
||||
createExceptionsTable(db)
|
||||
}
|
||||
|
||||
@ -87,11 +77,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
"$COL_REPEAT_INTERVAL INTEGER, $COL_REPEAT_LIMIT INTEGER, $COL_REPEAT_RULE INTEGER)")
|
||||
}
|
||||
|
||||
private fun createTypesTable(db: SQLiteDatabase) {
|
||||
db.execSQL("CREATE TABLE $TYPES_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_TYPE_TITLE TEXT, $COL_TYPE_COLOR INTEGER, " +
|
||||
"$COL_TYPE_CALDAV_CALENDAR_ID INTEGER, $COL_TYPE_CALDAV_DISPLAY_NAME TEXT, $COL_TYPE_CALDAV_EMAIL TEXT)")
|
||||
}
|
||||
|
||||
private fun createExceptionsTable(db: SQLiteDatabase) {
|
||||
db.execSQL("CREATE TABLE $REPEAT_EXCEPTIONS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_PARENT_EVENT_ID INTEGER, " +
|
||||
"$COL_OCCURRENCE_DAYCODE INTEGER)")
|
||||
@ -345,36 +330,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteEventTypes(eventTypes: ArrayList<EventType>, deleteEvents: Boolean, callback: (deletedCnt: Int) -> Unit) {
|
||||
var deleteIds = eventTypes.asSequence().filter { it.caldavCalendarId == 0 }.map { it.id }.toList()
|
||||
deleteIds = deleteIds.filter { it != REGULAR_EVENT_TYPE_ID } as ArrayList<Long>
|
||||
|
||||
val deletedSet = HashSet<String>()
|
||||
deleteIds.map { deletedSet.add(it.toString()) }
|
||||
context.config.removeDisplayEventTypes(deletedSet)
|
||||
if (deleteIds.isEmpty()) {
|
||||
return
|
||||
}
|
||||
|
||||
for (eventTypeId in deleteIds) {
|
||||
if (deleteEvents) {
|
||||
deleteEventsWithType(eventTypeId)
|
||||
} else {
|
||||
resetEventsWithType(eventTypeId)
|
||||
}
|
||||
}
|
||||
|
||||
val args = TextUtils.join(", ", deleteIds)
|
||||
val selection = "$COL_ID IN ($args)"
|
||||
callback(mDb.delete(TYPES_TABLE_NAME, selection, null))
|
||||
}
|
||||
|
||||
fun deleteEventTypesWithCalendarId(calendarIds: String) {
|
||||
val selection = "$COL_TYPE_CALDAV_CALENDAR_ID IN ($calendarIds)"
|
||||
mDb.delete(TYPES_TABLE_NAME, selection, null)
|
||||
}
|
||||
|
||||
private fun deleteEventsWithType(eventTypeId: Long) {
|
||||
fun deleteEventsWithType(eventTypeId: Long) {
|
||||
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_TYPE = ?"
|
||||
val selectionArgs = arrayOf(eventTypeId.toString())
|
||||
val cursor = getEventsCursor(selection, selectionArgs)
|
||||
@ -383,7 +339,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
deleteEvents(eventIDs, true)
|
||||
}
|
||||
|
||||
private fun resetEventsWithType(eventTypeId: Long) {
|
||||
fun resetEventsWithType(eventTypeId: Long) {
|
||||
val values = ContentValues()
|
||||
values.put(COL_EVENT_TYPE, REGULAR_EVENT_TYPE_ID)
|
||||
|
||||
|
@ -3,8 +3,10 @@ package com.simplemobiletools.calendar.pro.helpers
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
import com.simplemobiletools.calendar.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
|
||||
import com.simplemobiletools.calendar.pro.models.EventType
|
||||
import java.util.*
|
||||
|
||||
class EventTypesHelper {
|
||||
fun getEventTypes(activity: Activity, callback: (notes: ArrayList<EventType>) -> Unit) {
|
||||
@ -40,4 +42,25 @@ class EventTypesHelper {
|
||||
fun getEventTypeIdWithTitle(context: Context, title: String) = context.eventTypesDB.getEventTypeIdWithTitle(title) ?: -1L
|
||||
|
||||
fun getEventTypeWithCalDAVCalendarId(context: Context, calendarId: Int) = context.eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId)
|
||||
|
||||
fun deleteEventTypes(context: Context, eventTypes: ArrayList<EventType>, deleteEvents: Boolean) {
|
||||
val typesToDelete = eventTypes.asSequence().filter { it.caldavCalendarId == 0 && it.id != DBHelper.REGULAR_EVENT_TYPE_ID }.toMutableList()
|
||||
val deleteIds = typesToDelete.map { it.id }.toMutableList()
|
||||
val deletedSet = deleteIds.map { it.toString() }.toHashSet()
|
||||
context.config.removeDisplayEventTypes(deletedSet)
|
||||
|
||||
if (deleteIds.isEmpty()) {
|
||||
return
|
||||
}
|
||||
|
||||
for (eventTypeId in deleteIds) {
|
||||
if (deleteEvents) {
|
||||
context.dbHelper.deleteEventsWithType(eventTypeId!!)
|
||||
} else {
|
||||
context.dbHelper.resetEventsWithType(eventTypeId!!)
|
||||
}
|
||||
}
|
||||
|
||||
context.eventTypesDB.deleteEventTypes(typesToDelete)
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,6 @@
|
||||
package com.simplemobiletools.calendar.pro.interfaces
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.*
|
||||
import com.simplemobiletools.calendar.pro.models.EventType
|
||||
|
||||
@Dao
|
||||
@ -20,6 +17,12 @@ interface EventTypesDao {
|
||||
@Query("SELECT * FROM event_types WHERE caldav_calendar_id = :calendarId")
|
||||
fun getEventTypeWithCalDAVCalendarId(calendarId: Int): EventType?
|
||||
|
||||
@Query("DELETE FROM event_types WHERE caldav_calendar_id IN (:ids)")
|
||||
fun deleteEventTypesWithCalendarId(ids: List<Int>)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertOrUpdate(eventType: EventType): Long
|
||||
|
||||
@Delete
|
||||
fun deleteEventTypes(eventTypes: List<EventType>)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user