From d14a64d7446d85cd7840c8fbfe18a5cacc7d5b3d Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 26 Jul 2017 22:14:28 +0200 Subject: [PATCH] create some helper functions for handling operation items in the database --- .../calendar/helpers/Constants.kt | 3 ++ .../calendar/helpers/GoogleSyncQueueDB.kt | 44 ++++++++++++++++--- .../calendar/models/GoogleOperation.kt | 3 ++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/models/GoogleOperation.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt index 278a397fb..556a9f355 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt @@ -124,3 +124,6 @@ val SOURCE_IMPORTED_ICS = 2 // Google Sync val PRIMARY = "primary" val POPUP = "popup" +val OPERATION_INSERT = 1 +val OPERATION_UPDATE = 2 +val OPERATION_DELETE = 3 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 f6609aa9a..f60d100c3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncQueueDB.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncQueueDB.kt @@ -1,20 +1,20 @@ package com.simplemobiletools.calendar.helpers +import android.content.ContentValues import android.content.Context +import android.database.Cursor import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper +import com.simplemobiletools.calendar.models.GoogleOperation +import com.simplemobiletools.commons.extensions.getIntValue // 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) { - private val MAIN_TABLE_NAME = "operations" + private val OPERATIONS_TABLE_NAME = "operations" private val COL_ID = "id" private val COL_EVENT_ID = "event_id" private val COL_OPERATION = "operation" - private val OPERATION_INSERT = 1 - private val OPERATION_UPDATE = 2 - private val OPERATION_DELETE = 3 - private val mDb: SQLiteDatabase = writableDatabase companion object { @@ -31,10 +31,42 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe } override fun onCreate(db: SQLiteDatabase) { - db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_EVENT_ID INTEGER, $COL_OPERATION INTEGER)") + db.execSQL("CREATE TABLE $OPERATIONS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_EVENT_ID INTEGER, $COL_OPERATION INTEGER)") } override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { } + + fun insert(operation: Int, eventId: Int) { + val contentValues = ContentValues().apply { + put(COL_OPERATION, operation) + put(COL_EVENT_ID, eventId) + } + mDb.insert(OPERATIONS_TABLE_NAME, null, contentValues) + } + + fun getOperations(): ArrayList { + val operations = ArrayList() + val projection = arrayOf(COL_EVENT_ID, COL_OPERATION) + var cursor: Cursor? = null + try { + cursor = mDb.query(OPERATIONS_TABLE_NAME, projection, null, null, null, null, null) + if (cursor?.moveToFirst() == true) { + do { + val eventId = cursor.getIntValue(COL_EVENT_ID) + val operation = cursor.getIntValue(COL_OPERATION) + operations.add(GoogleOperation(eventId, operation)) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + return operations + } + + fun delete(eventId: Int) { + val selection = "$COL_EVENT_ID = $eventId" + mDb.delete(OPERATIONS_TABLE_NAME, selection, null) + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/GoogleOperation.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/GoogleOperation.kt new file mode 100644 index 000000000..d37ce9f4b --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/GoogleOperation.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.calendar.models + +data class GoogleOperation(val eventId: Int, val operation: Int)