add some logic to queueing operations

This commit is contained in:
tibbi
2017-07-27 22:14:03 +02:00
parent 18d377401a
commit e3dabf8ce5
3 changed files with 29 additions and 6 deletions

View File

@@ -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()
}

View File

@@ -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)
}
}
}

View File

@@ -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<GoogleOperation> {
val operations = ArrayList<GoogleOperation>()
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)
}