From 27cf585f24144124d3bec068868125ebd4533feb Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 29 Nov 2017 17:55:00 +0100 Subject: [PATCH] insert the default shared theme only after the function has been activated --- .../dbhelpers/MyContentProviderDbHelper.kt | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/thankyou/dbhelpers/MyContentProviderDbHelper.kt b/app/src/main/kotlin/com/simplemobiletools/thankyou/dbhelpers/MyContentProviderDbHelper.kt index 190e5b1..bf2c59c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/thankyou/dbhelpers/MyContentProviderDbHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/thankyou/dbhelpers/MyContentProviderDbHelper.kt @@ -15,7 +15,7 @@ import com.simplemobiletools.commons.helpers.MyContentProvider.Companion.fillThe import com.simplemobiletools.commons.models.SharedTheme class MyContentProviderDbHelper private constructor(private val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { - private val mDb: SQLiteDatabase = writableDatabase + private val mDb = writableDatabase companion object { private val DB_NAME = "Commons.db" @@ -29,16 +29,15 @@ class MyContentProviderDbHelper private constructor(private val context: Context override fun onCreate(db: SQLiteDatabase) { db.execSQL("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_TEXT_COLOR INTEGER DEFAULT 0, $COL_BACKGROUND_COLOR INTEGER DEFAULT 0," + " $COL_PRIMARY_COLOR INTEGER DEFAULT 0, $COL_LAST_UPDATED_TS INTEGER DEFAULT 0)") - insertDefaultTheme(db) } override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {} - private fun insertDefaultTheme(db: SQLiteDatabase) { + private fun insertDefaultTheme() { val resources = context.resources val theme = SharedTheme(resources.getColor(R.color.theme_dark_text_color), resources.getColor(R.color.theme_dark_background_color), resources.getColor(R.color.color_primary)) - insertTheme(theme, db) + insertTheme(theme, mDb) } private fun insertTheme(sharedTheme: SharedTheme, db: SQLiteDatabase) { @@ -52,7 +51,24 @@ class MyContentProviderDbHelper private constructor(private val context: Context return mDb.update(TABLE_NAME, values, selection, selectionArgs) } + fun isThemeAvailable(): Boolean { + val cols = arrayOf(COL_ID) + val selection = "$COL_ID = ?" + val selectionArgs = arrayOf(THEME_ID.toString()) + var cursor: Cursor? = null + try { + cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null) + return cursor.moveToFirst() + } finally { + cursor?.close() + } + } + fun getSharedTheme(): Cursor? { + if (!isThemeAvailable()) { + insertDefaultTheme() + } + val cols = arrayOf(COL_TEXT_COLOR, COL_BACKGROUND_COLOR, COL_PRIMARY_COLOR, COL_LAST_UPDATED_TS) val selection = "$COL_ID = ?" val selectionArgs = arrayOf(THEME_ID.toString())