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 911cadddf..8d8eadd55 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt @@ -113,7 +113,7 @@ class SettingsActivity : SimpleActivity() { settings_google_sync_holder.setOnClickListener { if (config.googleSync) { ConfirmationDialog(this, getString(R.string.google_sync_disabling), positive = R.string.ok, negative = R.string.cancel) { - dbHelper.deleteGoogleSyncEvents() + dbHelper.deleteAllGoogleSyncEvents() toggleGoogleSync() } } else { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt index f1ecf5ed0..eee19d805 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt @@ -62,7 +62,9 @@ class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: Goog for ((key, value) in events) { if (key == ITEMS) { eventTypes = dbHelper.fetchEventTypes() - parseEvents(value.toString()) + val localGoogleEvents = dbHelper.getGoogleSyncEvents() + val remoteGoogleEvents = parseEvents(value.toString()) + removeRedundantLocalEvents(localGoogleEvents, remoteGoogleEvents) } } @@ -74,18 +76,20 @@ class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: Goog } } - private fun parseEvents(json: String) { + private fun parseEvents(json: String): ArrayList { + val remoteImportIds = ArrayList() var updateEvent = false var eventId = 0 val importIDs = activity.dbHelper.getImportIds() val token = object : TypeToken>() {}.type val googleEvents = Gson().fromJson>(json, token) ?: ArrayList(8) for (googleEvent in googleEvents) { + val importId = googleEvent.id + remoteImportIds.add(importId) if (googleEvent.status != CONFIRMED) continue val lastUpdate = DateTime(googleEvent.updated).millis - val importId = googleEvent.id if (importIDs.contains(importId)) { val oldEvent = dbHelper.getEventWithImportId(importId) if (oldEvent != null) { @@ -132,6 +136,13 @@ class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: Goog dbHelper.insert(event) {} } } + return remoteImportIds + } + + private fun removeRedundantLocalEvents(localGoogleEvents: List, remoteGoogleEvents: ArrayList) { + val filtered = localGoogleEvents.filter { !remoteGoogleEvents.contains(it.importId) } + val filteredIds = Array(filtered.size, { i -> (filtered[i].id.toString()) }) + dbHelper.deleteEvents(filteredIds, false) } private fun getEventTypeId(colorId: Int): Int { 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 e37138362..de583d3a6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -345,7 +345,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont context.cancelNotification(it.toInt()) } - deleteChildEvents(args) + deleteChildEvents(args, deleteFromGoogle) if (deleteFromGoogle) { importIDs.forEach { Thread({ @@ -360,7 +360,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } } - private fun deleteChildEvents(ids: String) { + private fun deleteChildEvents(ids: String, deleteFromGoogle: Boolean) { val projection = arrayOf(COL_ID) val selection = "$COL_PARENT_EVENT_ID IN ($ids)" val childIds = ArrayList() @@ -378,7 +378,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } if (childIds.isNotEmpty()) - deleteEvents(childIds.toTypedArray()) + deleteEvents(childIds.toTypedArray(), deleteFromGoogle) } fun getGoogleSyncEvents(): List { @@ -387,7 +387,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return fillEvents(cursor) } - fun deleteGoogleSyncEvents() { + fun deleteAllGoogleSyncEvents() { val events = getGoogleSyncEvents() val eventIDs = Array(events.size, { i -> (events[i].id.toString()) }) deleteEvents(eventIDs, false)