implement deleting/unsyncing caldav events locally

This commit is contained in:
tibbi 2017-08-16 23:59:06 +02:00
parent 16538e1bde
commit 5cef06637f
8 changed files with 39 additions and 16 deletions

View file

@ -412,7 +412,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
private fun deleteEvent() {
DeleteEventDialog(this, arrayListOf(mEvent.id)) {
if (it) {
dbHelper.deleteEvents(arrayOf(mEvent.id.toString()))
dbHelper.deleteEvents(arrayOf(mEvent.id.toString()), true)
} else {
dbHelper.addEventRepeatException(mEvent.id, mEventOccurrenceTS)
}

View file

@ -129,14 +129,16 @@ class SettingsActivity : SimpleActivity() {
}
private fun showCalendarPicker() {
val oldCalendarIds = config.getCalendarIdsAsList()
SelectCalendarsDialog(this) {
val ids = config.caldavSyncedCalendarIDs.split(",").filter { it.trim().isNotEmpty() } as ArrayList<String>
settings_manage_synced_calendars_holder.beVisibleIf(ids.isNotEmpty())
settings_caldav_sync.isChecked = ids.isNotEmpty()
config.caldavSync = ids.isNotEmpty()
val newCalendarIds = config.getCalendarIdsAsList()
settings_manage_synced_calendars_holder.beVisibleIf(newCalendarIds.isNotEmpty())
settings_caldav_sync.isChecked = newCalendarIds.isNotEmpty()
config.caldavSync = newCalendarIds.isNotEmpty()
Thread({
if (ids.isNotEmpty()) {
if (newCalendarIds.isNotEmpty()) {
val eventTypeNames = dbHelper.fetchEventTypes().map { it.title.toLowerCase() } as ArrayList<String>
val calendars = CalDAVEventsHandler(applicationContext).getCalDAVCalendars(config.caldavSyncedCalendarIDs)
calendars.forEach {
@ -152,6 +154,10 @@ class SettingsActivity : SimpleActivity() {
CalDAVEventsHandler(applicationContext).fetchCalDAVCalendarEvents(it.id, eventTypeId)
}
}
oldCalendarIds.filter { !newCalendarIds.contains(it) }.forEach {
CalDAVEventsHandler(applicationContext).deleteCalDAVCalendarEvents(it.toLong())
}
}).start()
}
}

View file

@ -20,7 +20,7 @@ class SelectCalendarsDialog(val activity: Activity, val callback: () -> Unit) :
var view = (activity.layoutInflater.inflate(R.layout.dialog_select_calendars, null) as ViewGroup)
init {
val ids = activity.config.caldavSyncedCalendarIDs.split(",").filter { it.trim().isNotEmpty() } as ArrayList<String>
val ids = activity.config.getCalendarIdsAsList()
val calendars = CalDAVEventsHandler(activity).getCalDAVCalendars()
val sorted = calendars.sortedWith(compareBy({ it.accountName }, { it.displayName }))
sorted.forEach {

View file

@ -150,7 +150,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListen
override fun deleteItems(ids: ArrayList<Int>) {
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) })
DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs)
DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs, true)
}
override fun addEventRepeatException(parentIds: ArrayList<Int>, timestamps: ArrayList<Int>) {

View file

@ -111,7 +111,7 @@ class EventListFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEvents
override fun deleteItems(ids: ArrayList<Int>) {
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) })
DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs)
DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs, true)
}
override fun addEventRepeatException(parentIds: ArrayList<Int>, timestamps: ArrayList<Int>) {

View file

@ -157,6 +157,12 @@ class CalDAVEventsHandler(val context: Context) {
}
}
fun deleteCalDAVCalendarEvents(calendarId: Long) {
val events = context.dbHelper.getCalDAVCalendarEvents(calendarId)
val eventIds = events.map { it.id.toString() }.toTypedArray()
context.dbHelper.deleteEvents(eventIds, false)
}
fun deleteCalDAVEvent(event: Event) {
val uri = CalendarContract.Events.CONTENT_URI
val contentUri = ContentUris.withAppendedId(uri, event.getCalDAVId())

View file

@ -78,6 +78,8 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getString(CALDAV_SYNCED_CALENDAR_IDS, "")
set(calendarIDs) = prefs.edit().putString(CALDAV_SYNCED_CALENDAR_IDS, calendarIDs).apply()
fun getCalendarIdsAsList() = caldavSyncedCalendarIDs.split(",").filter { it.trim().isNotEmpty() } as ArrayList<String>
fun addDisplayEventType(type: String) {
addDisplayEventTypes(HashSet<String>(Arrays.asList(type)))
}

View file

@ -331,7 +331,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return null
}
fun deleteEvents(ids: Array<String>) {
fun deleteEvents(ids: Array<String>, deleteFromCalDAV: Boolean) {
val args = TextUtils.join(", ", ids)
val selection = "$MAIN_TABLE_NAME.$COL_ID IN ($args)"
val cursor = getEventsCursor(selection)
@ -352,14 +352,16 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
context.cancelNotification(it.toInt())
}
events.forEach {
CalDAVEventsHandler(context).deleteCalDAVEvent(it)
if (deleteFromCalDAV) {
events.forEach {
CalDAVEventsHandler(context).deleteCalDAVEvent(it)
}
}
deleteChildEvents(args)
deleteChildEvents(args, deleteFromCalDAV)
}
private fun deleteChildEvents(ids: String) {
private fun deleteChildEvents(ids: String, deleteFromCalDAV: Boolean) {
val projection = arrayOf(COL_ID)
val selection = "$COL_PARENT_EVENT_ID IN ($ids)"
val childIds = ArrayList<String>()
@ -377,7 +379,14 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
if (childIds.isNotEmpty())
deleteEvents(childIds.toTypedArray())
deleteEvents(childIds.toTypedArray(), deleteFromCalDAV)
}
fun getCalDAVCalendarEvents(calendarId: Long): List<Event> {
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_SOURCE = (?)"
val selectionArgs = arrayOf("$CALDAV-$calendarId")
val cursor = getEventsCursor(selection, selectionArgs)
return fillEvents(cursor).filter { it.importId.isNotEmpty() }
}
fun addEventRepeatException(parentEventId: Int, occurrenceTS: Int) {
@ -416,7 +425,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val cursor = getEventsCursor(selection, selectionArgs)
val events = fillEvents(cursor)
val eventIDs = Array(events.size, { i -> (events[i].id.toString()) })
deleteEvents(eventIDs)
deleteEvents(eventIDs, true)
}
private fun resetEventsWithType(eventTypeId: Int) {