From e3dabf8ce584e8c41c3c8559d4253fc808ea133b Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 27 Jul 2017 22:14:03 +0200 Subject: [PATCH] add some logic to queueing operations --- .../calendar/helpers/DBHelper.kt | 2 +- .../calendar/helpers/GoogleSyncHandler.kt | 4 +-- .../calendar/helpers/GoogleSyncQueueDB.kt | 29 +++++++++++++++++-- 3 files changed, 29 insertions(+), 6 deletions(-) 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 41bc7497f..32c8f5799 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -354,7 +354,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } catch (ignored: Exception) { } } else { - context.googleSyncQueue.insert(it.id, OPERATION_DELETE) + context.googleSyncQueue.addOperation(it.id, OPERATION_DELETE) } }).start() } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncHandler.kt index fd3016e78..7bf8510c4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncHandler.kt @@ -19,7 +19,7 @@ class GoogleSyncHandler { createRemoteGoogleEvent(activity, event) }).start() } else { - activity.googleSyncQueue.insert(event.id, OPERATION_INSERT) + activity.googleSyncQueue.addOperation(event.id, OPERATION_INSERT) } } } @@ -94,7 +94,7 @@ class GoogleSyncHandler { } }).start() } else { - activity.googleSyncQueue.insert(event.id, OPERATION_UPDATE) + activity.googleSyncQueue.addOperation(event.id, OPERATION_UPDATE) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncQueueDB.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncQueueDB.kt index 1a9aaa371..59f870f12 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncQueueDB.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncQueueDB.kt @@ -38,8 +38,16 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe } - fun insert(eventId: Int, operation: Int) { - delete(eventId) + fun addOperation(eventId: Int, operation: Int) { + if (operation == OPERATION_DELETE) { + clearOperationsOf(eventId) + } + + if (operation == OPERATION_UPDATE) { + if (getOperationOf(eventId)?.operation?.equals(OPERATION_INSERT) == true) { + return + } + } val contentValues = ContentValues().apply { put(COL_EVENT_ID, eventId) @@ -48,6 +56,21 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe mDb.insert(OPERATIONS_TABLE_NAME, null, contentValues) } + fun getOperationOf(eventId: Int): GoogleOperation? { + val selection = "$COL_EVENT_ID = $eventId" + val projection = arrayOf(COL_OPERATION) + var cursor: Cursor? = null + try { + cursor = mDb.query(OPERATIONS_TABLE_NAME, projection, selection, null, null, null, null) + if (cursor?.moveToFirst() == true) { + return GoogleOperation(eventId, cursor.getIntValue(COL_OPERATION)) + } + } finally { + cursor?.close() + } + return null + } + fun getOperations(): ArrayList { val operations = ArrayList() val projection = arrayOf(COL_EVENT_ID, COL_OPERATION) @@ -67,7 +90,7 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe return operations } - fun delete(eventId: Int) { + fun clearOperationsOf(eventId: Int) { val selection = "$COL_EVENT_ID = $eventId" mDb.delete(OPERATIONS_TABLE_NAME, selection, null) }