mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-17 12:20:51 +01:00
handle queued Delete operation
This commit is contained in:
parent
386c48bc56
commit
9758919b80
@ -26,13 +26,24 @@ class FetchGoogleEventsTask(val context: Context, val googleSyncListener: Google
|
||||
private var service = context.getGoogleSyncService()
|
||||
|
||||
override fun doInBackground(vararg params: Void): String {
|
||||
if (!context.isGoogleSyncActive())
|
||||
if (!context.isGoogleSyncActive() || !context.isOnline())
|
||||
return ""
|
||||
|
||||
// always handle queued operations before fetching new data
|
||||
val queuedOperations = context.googleSyncQueue.getOperations()
|
||||
queuedOperations.forEach {
|
||||
when (it.operation) {
|
||||
OPERATION_INSERT -> {
|
||||
|
||||
}
|
||||
OPERATION_UPDATE -> {
|
||||
|
||||
}
|
||||
OPERATION_DELETE -> {
|
||||
context.deleteFromGoogleSync(it.importId)
|
||||
context.googleSyncQueue.clearOperationsOf(it.eventId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -259,6 +259,13 @@ fun Context.scheduleGoogleSync(activate: Boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.deleteFromGoogleSync(importId: String) {
|
||||
try {
|
||||
getGoogleSyncService().events().delete(PRIMARY, importId).execute()
|
||||
} catch (ignored: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.getNewEventTimestampFromCode(dayCode: String) = Formatter.getLocalDateTimeFromCode(dayCode).withTime(13, 0, 0, 0).seconds()
|
||||
|
||||
fun Context.getCurrentOffset() = SimpleDateFormat("Z", Locale.getDefault()).format(Date())
|
||||
|
@ -349,12 +349,9 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
events.forEach {
|
||||
Thread({
|
||||
if (context.isOnline()) {
|
||||
try {
|
||||
context.getGoogleSyncService().events().delete(PRIMARY, it.importId).execute()
|
||||
} catch (ignored: Exception) {
|
||||
}
|
||||
context.deleteFromGoogleSync(it.importId)
|
||||
} else {
|
||||
context.googleSyncQueue.addOperation(it.id, OPERATION_DELETE)
|
||||
context.googleSyncQueue.addOperation(it.id, OPERATION_DELETE, it.importId)
|
||||
}
|
||||
}).start()
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ class GoogleSyncHandler {
|
||||
createRemoteGoogleEvent(activity, event)
|
||||
}).start()
|
||||
} else {
|
||||
activity.googleSyncQueue.addOperation(event.id, OPERATION_INSERT)
|
||||
activity.googleSyncQueue.addOperation(event.id, OPERATION_INSERT, event.importId)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -94,7 +94,7 @@ class GoogleSyncHandler {
|
||||
}
|
||||
}).start()
|
||||
} else {
|
||||
activity.googleSyncQueue.addOperation(event.id, OPERATION_UPDATE)
|
||||
activity.googleSyncQueue.addOperation(event.id, OPERATION_UPDATE, event.importId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import com.simplemobiletools.calendar.models.GoogleOperation
|
||||
import com.simplemobiletools.commons.extensions.getIntValue
|
||||
import com.simplemobiletools.commons.extensions.getStringValue
|
||||
|
||||
// database for storing operations performed on google events locally, while the user was offline
|
||||
class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
|
||||
@ -14,6 +15,7 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe
|
||||
private val COL_ID = "id"
|
||||
private val COL_EVENT_ID = "event_id"
|
||||
private val COL_OPERATION = "operation"
|
||||
private val COL_IMPORT_ID = "import_id"
|
||||
|
||||
private val mDb: SQLiteDatabase = writableDatabase
|
||||
|
||||
@ -31,14 +33,15 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe
|
||||
}
|
||||
|
||||
override fun onCreate(db: SQLiteDatabase) {
|
||||
db.execSQL("CREATE TABLE $OPERATIONS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_EVENT_ID INTEGER, $COL_OPERATION INTEGER)")
|
||||
db.execSQL("CREATE TABLE $OPERATIONS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_EVENT_ID INTEGER, $COL_OPERATION INTEGER, " +
|
||||
"$COL_IMPORT_ID TEXT)")
|
||||
}
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||
|
||||
}
|
||||
|
||||
fun addOperation(eventId: Int, operation: Int) {
|
||||
fun addOperation(eventId: Int, operation: Int, importId: String) {
|
||||
val hadInsertOperation = getOperationOf(eventId)?.operation?.equals(OPERATION_INSERT) == true
|
||||
if (operation == OPERATION_DELETE) {
|
||||
clearOperationsOf(eventId)
|
||||
@ -56,18 +59,21 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe
|
||||
val contentValues = ContentValues().apply {
|
||||
put(COL_EVENT_ID, eventId)
|
||||
put(COL_OPERATION, operation)
|
||||
put(COL_IMPORT_ID, importId)
|
||||
}
|
||||
mDb.insert(OPERATIONS_TABLE_NAME, null, contentValues)
|
||||
}
|
||||
|
||||
fun getOperationOf(eventId: Int): GoogleOperation? {
|
||||
val selection = "$COL_EVENT_ID = $eventId"
|
||||
val projection = arrayOf(COL_OPERATION)
|
||||
val projection = arrayOf(COL_OPERATION, COL_IMPORT_ID)
|
||||
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))
|
||||
val operation = cursor.getIntValue(COL_OPERATION)
|
||||
val importId = cursor.getStringValue(COL_IMPORT_ID)
|
||||
return GoogleOperation(eventId, operation, importId)
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
@ -77,7 +83,7 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe
|
||||
|
||||
fun getOperations(): ArrayList<GoogleOperation> {
|
||||
val operations = ArrayList<GoogleOperation>()
|
||||
val projection = arrayOf(COL_EVENT_ID, COL_OPERATION)
|
||||
val projection = arrayOf(COL_EVENT_ID, COL_OPERATION, COL_IMPORT_ID)
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = mDb.query(OPERATIONS_TABLE_NAME, projection, null, null, null, null, null)
|
||||
@ -85,7 +91,8 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe
|
||||
do {
|
||||
val eventId = cursor.getIntValue(COL_EVENT_ID)
|
||||
val operation = cursor.getIntValue(COL_OPERATION)
|
||||
operations.add(GoogleOperation(eventId, operation))
|
||||
val importId = cursor.getStringValue(COL_IMPORT_ID)
|
||||
operations.add(GoogleOperation(eventId, operation, importId))
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
} finally {
|
||||
|
@ -1,3 +1,3 @@
|
||||
package com.simplemobiletools.calendar.models
|
||||
|
||||
data class GoogleOperation(val eventId: Int, val operation: Int)
|
||||
data class GoogleOperation(val eventId: Int, val operation: Int, val importId: String)
|
||||
|
Loading…
x
Reference in New Issue
Block a user