mirror of
https://github.com/bitwarden/android.git
synced 2026-06-07 23:58:03 -05:00
Add disk storage for Vault Timeout (#518)
This commit is contained in:
committed by
Álison Fernandes
parent
f54af724b1
commit
54c288cb25
@@ -10,6 +10,37 @@ import androidx.core.content.edit
|
||||
abstract class BaseDiskSource(
|
||||
private val sharedPreferences: SharedPreferences,
|
||||
) {
|
||||
/**
|
||||
* Gets the [Int] for the given [key] from [SharedPreferences], or return the [default] value
|
||||
* if that key is not present.
|
||||
*/
|
||||
protected fun getInt(
|
||||
key: String,
|
||||
default: Int? = null,
|
||||
): Int? =
|
||||
if (sharedPreferences.contains(key)) {
|
||||
sharedPreferences.getInt(key, 0)
|
||||
} else {
|
||||
// Make sure we can return a null value as a default if necessary
|
||||
default
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the [value] in [SharedPreferences] for the given [key] (or removes the key when the
|
||||
* value is `null`).
|
||||
*/
|
||||
protected fun putInt(
|
||||
key: String,
|
||||
value: Int?,
|
||||
): Unit =
|
||||
sharedPreferences.edit {
|
||||
if (value != null) {
|
||||
putInt(key, value)
|
||||
} else {
|
||||
remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
protected fun getString(
|
||||
key: String,
|
||||
default: String? = null,
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.disk
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* Primary access point for general settings-related disk information.
|
||||
*/
|
||||
interface SettingsDiskSource {
|
||||
/**
|
||||
* Gets the current vault timeout (in minutes) for the given [userId] (or `null` if the vault
|
||||
* should never time out).
|
||||
*/
|
||||
fun getVaultTimeoutInMinutes(userId: String): Int?
|
||||
|
||||
/**
|
||||
* Emits updates that track [getVaultTimeoutInMinutes] for the given [userId]. This will replay
|
||||
* the last known value, if any.
|
||||
*/
|
||||
fun getVaultTimeoutInMinutesFlow(userId: String): Flow<Int?>
|
||||
|
||||
/**
|
||||
* Stores the given [vaultTimeoutInMinutes] for the given [userId].
|
||||
*/
|
||||
fun storeVaultTimeoutInMinutes(userId: String, vaultTimeoutInMinutes: Int?)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.disk
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import com.x8bit.bitwarden.data.platform.datasource.disk.BaseDiskSource.Companion.BASE_KEY
|
||||
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.onSubscription
|
||||
|
||||
private const val VAULT_TIME_IN_MINUTES_KEY = "$BASE_KEY:vaultTimeout"
|
||||
|
||||
/**
|
||||
* Primary implementation of [SettingsDiskSource].
|
||||
*/
|
||||
class SettingsDiskSourceImpl(
|
||||
val sharedPreferences: SharedPreferences,
|
||||
) : BaseDiskSource(sharedPreferences = sharedPreferences),
|
||||
SettingsDiskSource {
|
||||
private val mutableVaultTimeoutInMinutesFlowMap =
|
||||
mutableMapOf<String, MutableSharedFlow<Int?>>()
|
||||
|
||||
override fun getVaultTimeoutInMinutes(userId: String): Int? =
|
||||
getInt(key = "${VAULT_TIME_IN_MINUTES_KEY}_$userId")
|
||||
|
||||
override fun getVaultTimeoutInMinutesFlow(userId: String): Flow<Int?> =
|
||||
getMutableVaultTimeoutInMinutesFlow(userId = userId)
|
||||
.onSubscription { emit(getVaultTimeoutInMinutes(userId = userId)) }
|
||||
|
||||
override fun storeVaultTimeoutInMinutes(
|
||||
userId: String,
|
||||
vaultTimeoutInMinutes: Int?,
|
||||
) {
|
||||
putInt(
|
||||
key = "${VAULT_TIME_IN_MINUTES_KEY}_$userId",
|
||||
value = vaultTimeoutInMinutes,
|
||||
)
|
||||
getMutableVaultTimeoutInMinutesFlow(userId = userId).tryEmit(vaultTimeoutInMinutes)
|
||||
}
|
||||
|
||||
private fun getMutableVaultTimeoutInMinutesFlow(
|
||||
userId: String,
|
||||
): MutableSharedFlow<Int?> =
|
||||
mutableVaultTimeoutInMinutesFlowMap.getOrPut(userId) {
|
||||
bufferedMutableSharedFlow(replay = 1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user