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() { private fun deleteEvent() {
DeleteEventDialog(this, arrayListOf(mEvent.id)) { DeleteEventDialog(this, arrayListOf(mEvent.id)) {
if (it) { if (it) {
dbHelper.deleteEvents(arrayOf(mEvent.id.toString())) dbHelper.deleteEvents(arrayOf(mEvent.id.toString()), true)
} else { } else {
dbHelper.addEventRepeatException(mEvent.id, mEventOccurrenceTS) dbHelper.addEventRepeatException(mEvent.id, mEventOccurrenceTS)
} }

View File

@ -129,14 +129,16 @@ class SettingsActivity : SimpleActivity() {
} }
private fun showCalendarPicker() { private fun showCalendarPicker() {
val oldCalendarIds = config.getCalendarIdsAsList()
SelectCalendarsDialog(this) { SelectCalendarsDialog(this) {
val ids = config.caldavSyncedCalendarIDs.split(",").filter { it.trim().isNotEmpty() } as ArrayList<String> val newCalendarIds = config.getCalendarIdsAsList()
settings_manage_synced_calendars_holder.beVisibleIf(ids.isNotEmpty()) settings_manage_synced_calendars_holder.beVisibleIf(newCalendarIds.isNotEmpty())
settings_caldav_sync.isChecked = ids.isNotEmpty() settings_caldav_sync.isChecked = newCalendarIds.isNotEmpty()
config.caldavSync = ids.isNotEmpty() config.caldavSync = newCalendarIds.isNotEmpty()
Thread({ Thread({
if (ids.isNotEmpty()) { if (newCalendarIds.isNotEmpty()) {
val eventTypeNames = dbHelper.fetchEventTypes().map { it.title.toLowerCase() } as ArrayList<String> val eventTypeNames = dbHelper.fetchEventTypes().map { it.title.toLowerCase() } as ArrayList<String>
val calendars = CalDAVEventsHandler(applicationContext).getCalDAVCalendars(config.caldavSyncedCalendarIDs) val calendars = CalDAVEventsHandler(applicationContext).getCalDAVCalendars(config.caldavSyncedCalendarIDs)
calendars.forEach { calendars.forEach {
@ -152,6 +154,10 @@ class SettingsActivity : SimpleActivity() {
CalDAVEventsHandler(applicationContext).fetchCalDAVCalendarEvents(it.id, eventTypeId) CalDAVEventsHandler(applicationContext).fetchCalDAVCalendarEvents(it.id, eventTypeId)
} }
} }
oldCalendarIds.filter { !newCalendarIds.contains(it) }.forEach {
CalDAVEventsHandler(applicationContext).deleteCalDAVCalendarEvents(it.toLong())
}
}).start() }).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) var view = (activity.layoutInflater.inflate(R.layout.dialog_select_calendars, null) as ViewGroup)
init { 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 calendars = CalDAVEventsHandler(activity).getCalDAVCalendars()
val sorted = calendars.sortedWith(compareBy({ it.accountName }, { it.displayName })) val sorted = calendars.sortedWith(compareBy({ it.accountName }, { it.displayName }))
sorted.forEach { sorted.forEach {

View File

@ -150,7 +150,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListen
override fun deleteItems(ids: ArrayList<Int>) { override fun deleteItems(ids: ArrayList<Int>) {
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) }) 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>) { 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>) { override fun deleteItems(ids: ArrayList<Int>) {
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) }) 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>) { 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) { fun deleteCalDAVEvent(event: Event) {
val uri = CalendarContract.Events.CONTENT_URI val uri = CalendarContract.Events.CONTENT_URI
val contentUri = ContentUris.withAppendedId(uri, event.getCalDAVId()) 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, "") get() = prefs.getString(CALDAV_SYNCED_CALENDAR_IDS, "")
set(calendarIDs) = prefs.edit().putString(CALDAV_SYNCED_CALENDAR_IDS, calendarIDs).apply() 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) { fun addDisplayEventType(type: String) {
addDisplayEventTypes(HashSet<String>(Arrays.asList(type))) addDisplayEventTypes(HashSet<String>(Arrays.asList(type)))
} }

View File

@ -331,7 +331,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return null return null
} }
fun deleteEvents(ids: Array<String>) { fun deleteEvents(ids: Array<String>, deleteFromCalDAV: Boolean) {
val args = TextUtils.join(", ", ids) val args = TextUtils.join(", ", ids)
val selection = "$MAIN_TABLE_NAME.$COL_ID IN ($args)" val selection = "$MAIN_TABLE_NAME.$COL_ID IN ($args)"
val cursor = getEventsCursor(selection) val cursor = getEventsCursor(selection)
@ -352,14 +352,16 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
context.cancelNotification(it.toInt()) context.cancelNotification(it.toInt())
} }
if (deleteFromCalDAV) {
events.forEach { events.forEach {
CalDAVEventsHandler(context).deleteCalDAVEvent(it) CalDAVEventsHandler(context).deleteCalDAVEvent(it)
} }
deleteChildEvents(args)
} }
private fun deleteChildEvents(ids: String) { deleteChildEvents(args, deleteFromCalDAV)
}
private fun deleteChildEvents(ids: String, deleteFromCalDAV: Boolean) {
val projection = arrayOf(COL_ID) val projection = arrayOf(COL_ID)
val selection = "$COL_PARENT_EVENT_ID IN ($ids)" val selection = "$COL_PARENT_EVENT_ID IN ($ids)"
val childIds = ArrayList<String>() val childIds = ArrayList<String>()
@ -377,7 +379,14 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} }
if (childIds.isNotEmpty()) 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) { 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 cursor = getEventsCursor(selection, selectionArgs)
val events = fillEvents(cursor) val events = fillEvents(cursor)
val eventIDs = Array(events.size, { i -> (events[i].id.toString()) }) val eventIDs = Array(events.size, { i -> (events[i].id.toString()) })
deleteEvents(eventIDs) deleteEvents(eventIDs, true)
} }
private fun resetEventsWithType(eventTypeId: Int) { private fun resetEventsWithType(eventTypeId: Int) {