diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-18 14:03:48 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-18 20:29:23 +0200 |
commit | d200437813ae09769dc90820ded3911a324601ca (patch) | |
tree | a0092ae87fb90afa70c67c4ed3715c5f1c2e1da6 /ui/src/main/java/com/wireguard/android/util/UserKnobs.kt | |
parent | 3ffe7a5e68dcd6e1f3e589d904b723aa4eb24a38 (diff) |
ui: move to Jetpack DataStore instead of SharedPrefs
Hopefully PreferencesPreferenceDataStore gets to go away sometime down
the line.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/util/UserKnobs.kt')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/util/UserKnobs.kt | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt b/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt new file mode 100644 index 00000000..b0107ebf --- /dev/null +++ b/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt @@ -0,0 +1,85 @@ +/* + * Copyright © 2020 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.wireguard.android.util + +import androidx.datastore.preferences.edit +import androidx.datastore.preferences.preferencesKey +import androidx.datastore.preferences.preferencesSetKey +import androidx.datastore.preferences.remove +import com.wireguard.android.Application +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map + +object UserKnobs { + private val DISABLE_KERNEL_MODULE = preferencesKey<Boolean>("disable_kernel_module") + val disableKernelModule: Flow<Boolean> + get() = Application.getPreferencesDataStore().data.map { + it[DISABLE_KERNEL_MODULE] ?: false + } + + suspend fun setDisableKernelModule(disable: Boolean?) { + Application.getPreferencesDataStore().edit { + if (disable == null) + it.remove(DISABLE_KERNEL_MODULE) + else + it[DISABLE_KERNEL_MODULE] = disable + } + } + + private val MULTIPLE_TUNNELS = preferencesKey<Boolean>("multiple_tunnels") + val multipleTunnels: Flow<Boolean> + get() = Application.getPreferencesDataStore().data.map { + it[MULTIPLE_TUNNELS] ?: false + } + + private val DARK_THEME = preferencesKey<Boolean>("dark_theme") + val darkTheme: Flow<Boolean> + get() = Application.getPreferencesDataStore().data.map { + it[DARK_THEME] ?: false + } + + private val ALLOW_REMOTE_CONTROL_INTENTS = preferencesKey<Boolean>("allow_remote_control_intents") + val allowRemoteControlIntents: Flow<Boolean> + get() = Application.getPreferencesDataStore().data.map { + it[ALLOW_REMOTE_CONTROL_INTENTS] ?: false + } + + private val RESTORE_ON_BOOT = preferencesKey<Boolean>("restore_on_boot") + val restoreOnBoot: Flow<Boolean> + get() = Application.getPreferencesDataStore().data.map { + it[RESTORE_ON_BOOT] ?: false + } + + private val LAST_USED_TUNNEL = preferencesKey<String>("last_used_tunnel") + val lastUsedTunnel: Flow<String?> + get() = Application.getPreferencesDataStore().data.map { + it[LAST_USED_TUNNEL] + } + + suspend fun setLastUsedTunnel(lastUsedTunnel: String?) { + Application.getPreferencesDataStore().edit { + if (lastUsedTunnel == null) + it.remove(LAST_USED_TUNNEL) + else + it[LAST_USED_TUNNEL] = lastUsedTunnel + } + } + + private val RUNNING_TUNNELS = preferencesSetKey<String>("enabled_configs") + val runningTunnels: Flow<Set<String>> + get() = Application.getPreferencesDataStore().data.map { + it[RUNNING_TUNNELS] ?: emptySet() + } + + suspend fun setRunningTunnels(runningTunnels: Set<String>) { + Application.getPreferencesDataStore().edit { + if (runningTunnels.isEmpty()) + it.remove(RUNNING_TUNNELS) + else + it[RUNNING_TUNNELS] = runningTunnels + } + } +} |