diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-05-06 16:55:29 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-05-06 17:02:28 +0200 |
commit | 94ecb13d2fd9f36a10b0209b2ddf161b4cc0a07f (patch) | |
tree | ddf5437289149c6151a572dca889d8e4ebc9f61a /ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt | |
parent | ui: remove hack for broken kotlin compiler (diff) | |
download | wireguard-android-94ecb13d2fd9f36a10b0209b2ddf161b4cc0a07f.tar.xz wireguard-android-94ecb13d2fd9f36a10b0209b2ddf161b4cc0a07f.zip |
ui: update datastore and rework api
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt b/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt index 96ca2b1c..dc9375bc 100644 --- a/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt +++ b/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt @@ -5,12 +5,15 @@ package com.wireguard.android.preference -import androidx.datastore.DataStore -import androidx.datastore.preferences.Preferences -import androidx.datastore.preferences.edit -import androidx.datastore.preferences.preferencesKey -import androidx.datastore.preferences.preferencesSetKey -import androidx.datastore.preferences.remove +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.booleanPreferencesKey +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.floatPreferencesKey +import androidx.datastore.preferences.core.intPreferencesKey +import androidx.datastore.preferences.core.longPreferencesKey +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.core.stringSetPreferencesKey import androidx.preference.PreferenceDataStore import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.first @@ -21,7 +24,7 @@ import kotlinx.coroutines.runBlocking class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, private val dataStore: DataStore<Preferences>) : PreferenceDataStore() { override fun putString(key: String?, value: String?) { if (key == null) return - val pk = preferencesKey<String>(key) + val pk = stringPreferencesKey(key) coroutineScope.launch { dataStore.edit { if (value == null) it.remove(pk) @@ -32,7 +35,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun putStringSet(key: String?, values: Set<String?>?) { if (key == null) return - val pk = preferencesSetKey<String>(key) + val pk = stringSetPreferencesKey(key) val filteredValues = values?.filterNotNull()?.toSet() coroutineScope.launch { dataStore.edit { @@ -44,7 +47,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun putInt(key: String?, value: Int) { if (key == null) return - val pk = preferencesKey<Int>(key) + val pk = intPreferencesKey(key) coroutineScope.launch { dataStore.edit { it[pk] = value @@ -54,7 +57,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun putLong(key: String?, value: Long) { if (key == null) return - val pk = preferencesKey<Long>(key) + val pk = longPreferencesKey(key) coroutineScope.launch { dataStore.edit { it[pk] = value @@ -64,7 +67,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun putFloat(key: String?, value: Float) { if (key == null) return - val pk = preferencesKey<Float>(key) + val pk = floatPreferencesKey(key) coroutineScope.launch { dataStore.edit { it[pk] = value @@ -74,7 +77,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun putBoolean(key: String?, value: Boolean) { if (key == null) return - val pk = preferencesKey<Boolean>(key) + val pk = booleanPreferencesKey(key) coroutineScope.launch { dataStore.edit { it[pk] = value @@ -84,7 +87,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun getString(key: String?, defValue: String?): String? { if (key == null) return defValue - val pk = preferencesKey<String>(key) + val pk = stringPreferencesKey(key) return runBlocking { dataStore.data.map { it[pk] ?: defValue }.first() } @@ -92,7 +95,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun getStringSet(key: String?, defValues: Set<String?>?): Set<String?>? { if (key == null) return defValues - val pk = preferencesSetKey<String>(key) + val pk = stringSetPreferencesKey(key) return runBlocking { dataStore.data.map { it[pk] ?: defValues }.first() } @@ -100,7 +103,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun getInt(key: String?, defValue: Int): Int { if (key == null) return defValue - val pk = preferencesKey<Int>(key) + val pk = intPreferencesKey(key) return runBlocking { dataStore.data.map { it[pk] ?: defValue }.first() } @@ -108,7 +111,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun getLong(key: String?, defValue: Long): Long { if (key == null) return defValue - val pk = preferencesKey<Long>(key) + val pk = longPreferencesKey(key) return runBlocking { dataStore.data.map { it[pk] ?: defValue }.first() } @@ -116,7 +119,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun getFloat(key: String?, defValue: Float): Float { if (key == null) return defValue - val pk = preferencesKey<Float>(key) + val pk = floatPreferencesKey(key) return runBlocking { dataStore.data.map { it[pk] ?: defValue }.first() } @@ -124,7 +127,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, override fun getBoolean(key: String?, defValue: Boolean): Boolean { if (key == null) return defValue - val pk = preferencesKey<Boolean>(key) + val pk = booleanPreferencesKey(key) return runBlocking { dataStore.data.map { it[pk] ?: defValue }.first() } |