Update BackendStorage to allow getting/setting extra values

This commit is contained in:
cketti 2018-07-15 15:48:39 +02:00
parent e56b03d7bc
commit 5104780468
2 changed files with 68 additions and 1 deletions

View file

@ -1,5 +1,8 @@
package com.fsck.k9.mailstore
import android.content.ContentValues
import android.database.Cursor
import androidx.core.database.getStringOrNull
import com.fsck.k9.Account
import com.fsck.k9.Preferences
import com.fsck.k9.backend.api.BackendFolder
@ -8,9 +11,68 @@ import com.fsck.k9.backend.api.BackendStorage
class K9BackendStorage(
private val preferences: Preferences,
private val account: Account,
private val localStore: LocalStore) : BackendStorage {
private val localStore: LocalStore
) : BackendStorage {
private val database = localStore.database
override fun getFolder(folderServerId: String): BackendFolder {
return K9BackendFolder(preferences, account, localStore, folderServerId)
}
override fun getExtraString(name: String): String? {
return database.execute(false) { db ->
val cursor = db.query(
"account_extra_values",
arrayOf("value_string"),
"name = ?",
arrayOf(name),
null, null, null)
cursor.use {
if (it.moveToFirst()) {
it.getStringOrNull(0)
} else {
null
}
}
}
}
override fun setExtraString(name: String, value: String) {
database.execute(false) { db ->
val contentValues = ContentValues().apply {
put("value_string", value)
}
db.update("account_extra_values", contentValues, "name = ?", arrayOf(name))
}
}
override fun getExtraNumber(name: String): Long? {
return database.execute(false) { db ->
val cursor = db.query(
"account_extra_values",
arrayOf("value_integer"),
"name = ?",
arrayOf(name),
null, null, null)
cursor.use {
if (it.moveToFirst()) {
it.getLongOrNull(0)
} else {
null
}
}
}
}
override fun setExtraNumber(name: String, value: Long) {
database.execute(false) { db ->
val contentValues = ContentValues().apply {
put("value_integer", value)
}
db.update("account_extra_values", contentValues, "name = ?", arrayOf(name))
}
}
private fun Cursor.getLongOrNull(columnIndex: Int): Long? = if (isNull(columnIndex)) null else getLong(columnIndex)
}

View file

@ -2,4 +2,9 @@ package com.fsck.k9.backend.api
interface BackendStorage {
fun getFolder(folderServerId: String): BackendFolder
fun getExtraString(name: String): String?
fun setExtraString(name: String, value: String)
fun getExtraNumber(name: String): Long?
fun setExtraNumber(name: String, value: Long)
}