refact: [DATASTORE] add mapper in case we encrypted rows (#393)

Co-authored-by: Florent Champigny <florent@bere.al>
This commit is contained in:
Florent CHAMPIGNY 2025-11-18 21:10:59 +01:00 committed by GitHub
parent ce4c8f5a0a
commit db56d741a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 2 deletions

View file

@ -14,6 +14,11 @@ import io.github.openflocon.flocon.plugins.sharedprefs.model.FloconPreference
import io.github.openflocon.flocon.plugins.sharedprefs.model.FloconPreferenceValue
import kotlinx.coroutines.flow.first
interface FloconDatastoreMapper {
fun fromDatastore(datastoreValue: String) : String
fun toDatastore(valueForDatastore: String) : String
}
class FloconDatastorePreference(
override val name: String,
val dataStore: DataStore<Preferences>,

View file

@ -14,9 +14,15 @@ import io.github.openflocon.flocon.plugins.sharedprefs.model.FloconPreference
import io.github.openflocon.flocon.plugins.sharedprefs.model.FloconPreferenceValue
import kotlinx.coroutines.flow.first
interface FloconDatastoreMapper {
fun fromDatastore(datastoreValue: String) : String
fun toDatastore(valueForDatastore: String) : String
}
class FloconDatastorePreference(
override val name: String,
private val dataStore: DataStore<Preferences>,
private val mapper: FloconDatastoreMapper? = null // if we encrypted the datastore
) : FloconPreference {
override suspend fun set(
@ -30,7 +36,9 @@ class FloconDatastorePreference(
dataStore.edit {
try {
when (it[key]) {
is String -> it[stringPreferencesKey(columnName)] = value.stringValue!!
is String -> it[stringPreferencesKey(columnName)] = mapper?.let {
it.toDatastore(value.stringValue!!)
} ?: value.stringValue!!
is Int -> it[intPreferencesKey(columnName)] = value.intValue!!
is Boolean -> it[booleanPreferencesKey(columnName)] = value.booleanValue!!
is Float -> it[floatPreferencesKey(columnName)] = value.floatValue!!
@ -63,7 +71,7 @@ class FloconDatastorePreference(
val value = data[key] ?: return null
return when (value) {
is String -> FloconPreferenceValue(stringValue = value)
is String -> FloconPreferenceValue(stringValue = mapper?.fromDatastore(value) ?: value)
is Int -> FloconPreferenceValue(intValue = value)
is Float -> FloconPreferenceValue(floatValue = value)
is Double -> FloconPreferenceValue(floatValue = value.toFloat())