From 5cef06637fbe5ea089767f8653c6d935cf73fb13 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 16 Aug 2017 23:59:06 +0200 Subject: [PATCH] implement deleting/unsyncing caldav events locally --- .../calendar/activities/EventActivity.kt | 2 +- .../calendar/activities/SettingsActivity.kt | 16 +++++++++---- .../calendar/dialogs/SelectCalendarsDialog.kt | 2 +- .../calendar/fragments/DayFragment.kt | 2 +- .../calendar/fragments/EventListFragment.kt | 2 +- .../calendar/helpers/CalDAVEventsHandler.kt | 6 +++++ .../calendar/helpers/Config.kt | 2 ++ .../calendar/helpers/DBHelper.kt | 23 +++++++++++++------ 8 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt index 0617727e6..6ec0f8b14 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt @@ -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) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt index 50f8d0ea3..9ded300ba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt @@ -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 - 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 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() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectCalendarsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectCalendarsDialog.kt index 607afac4b..8cb023f97 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectCalendarsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectCalendarsDialog.kt @@ -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 + val ids = activity.config.getCalendarIdsAsList() val calendars = CalDAVEventsHandler(activity).getCalDAVCalendars() val sorted = calendars.sortedWith(compareBy({ it.accountName }, { it.displayName })) sorted.forEach { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt index e28dd8868..02005fa1f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -150,7 +150,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListen override fun deleteItems(ids: ArrayList) { 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, timestamps: ArrayList) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt index 001ac7ac6..23dfa1e0a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt @@ -111,7 +111,7 @@ class EventListFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEvents override fun deleteItems(ids: ArrayList) { 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, timestamps: ArrayList) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt index 916c9878e..4873ed10c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt @@ -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()) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt index 1dbfac987..4c1fce4c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt @@ -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 + fun addDisplayEventType(type: String) { addDisplayEventTypes(HashSet(Arrays.asList(type))) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index 2848bcdfb..9caf8d130 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -331,7 +331,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return null } - fun deleteEvents(ids: Array) { + fun deleteEvents(ids: Array, 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() @@ -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 { + 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) {