delete redundant local google sync events when appropriate

This commit is contained in:
tibbi 2017-07-23 22:12:18 +02:00
parent c9ec5c9606
commit 55c6f7267c
3 changed files with 19 additions and 8 deletions

View File

@ -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 {

View File

@ -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<String> {
val remoteImportIds = ArrayList<String>()
var updateEvent = false
var eventId = 0
val importIDs = activity.dbHelper.getImportIds()
val token = object : TypeToken<List<GoogleEvent>>() {}.type
val googleEvents = Gson().fromJson<ArrayList<GoogleEvent>>(json, token) ?: ArrayList<GoogleEvent>(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<Event>, remoteGoogleEvents: ArrayList<String>) {
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 {

View File

@ -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<String>()
@ -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<Event> {
@ -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)