mirror of
https://github.com/bitwarden/android.git
synced 2026-06-07 14:57:41 -05:00
Add VaultTimeoutAction and handle its persistence (#520)
This commit is contained in:
committed by
Álison Fernandes
parent
6acfb10709
commit
e2c35fc373
@@ -1,5 +1,6 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.disk
|
||||
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeoutAction
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
@@ -22,4 +23,23 @@ interface SettingsDiskSource {
|
||||
* Stores the given [vaultTimeoutInMinutes] for the given [userId].
|
||||
*/
|
||||
fun storeVaultTimeoutInMinutes(userId: String, vaultTimeoutInMinutes: Int?)
|
||||
|
||||
/**
|
||||
* Gets the current [VaultTimeoutAction] for the given [userId].
|
||||
*/
|
||||
fun getVaultTimeoutAction(userId: String): VaultTimeoutAction?
|
||||
|
||||
/**
|
||||
* Emits updates that track [getVaultTimeoutAction] for the given [userId]. This will replay
|
||||
* the last known value, if any.
|
||||
*/
|
||||
fun getVaultTimeoutActionFlow(userId: String): Flow<VaultTimeoutAction?>
|
||||
|
||||
/**
|
||||
* Stores the given [vaultTimeoutAction] for the given [userId].
|
||||
*/
|
||||
fun storeVaultTimeoutAction(
|
||||
userId: String,
|
||||
vaultTimeoutAction: VaultTimeoutAction?,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ 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.model.VaultTimeoutAction
|
||||
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_TIMEOUT_ACTION_KEY = "$BASE_KEY:vaultTimeoutAction"
|
||||
private const val VAULT_TIME_IN_MINUTES_KEY = "$BASE_KEY:vaultTimeout"
|
||||
|
||||
/**
|
||||
@@ -16,6 +18,9 @@ class SettingsDiskSourceImpl(
|
||||
val sharedPreferences: SharedPreferences,
|
||||
) : BaseDiskSource(sharedPreferences = sharedPreferences),
|
||||
SettingsDiskSource {
|
||||
private val mutableVaultTimeoutActionFlowMap =
|
||||
mutableMapOf<String, MutableSharedFlow<VaultTimeoutAction?>>()
|
||||
|
||||
private val mutableVaultTimeoutInMinutesFlowMap =
|
||||
mutableMapOf<String, MutableSharedFlow<Int?>>()
|
||||
|
||||
@@ -37,6 +42,33 @@ class SettingsDiskSourceImpl(
|
||||
getMutableVaultTimeoutInMinutesFlow(userId = userId).tryEmit(vaultTimeoutInMinutes)
|
||||
}
|
||||
|
||||
override fun getVaultTimeoutAction(userId: String): VaultTimeoutAction? =
|
||||
getInt(key = "${VAULT_TIMEOUT_ACTION_KEY}_$userId")?.let { storedValue ->
|
||||
VaultTimeoutAction.entries.firstOrNull { storedValue == it.value }
|
||||
}
|
||||
|
||||
override fun getVaultTimeoutActionFlow(userId: String): Flow<VaultTimeoutAction?> =
|
||||
getMutableVaultTimeoutActionFlow(userId = userId)
|
||||
.onSubscription { emit(getVaultTimeoutAction(userId = userId)) }
|
||||
|
||||
override fun storeVaultTimeoutAction(
|
||||
userId: String,
|
||||
vaultTimeoutAction: VaultTimeoutAction?,
|
||||
) {
|
||||
putInt(
|
||||
key = "${VAULT_TIMEOUT_ACTION_KEY}_$userId",
|
||||
value = vaultTimeoutAction?.value,
|
||||
)
|
||||
getMutableVaultTimeoutActionFlow(userId = userId).tryEmit(vaultTimeoutAction)
|
||||
}
|
||||
|
||||
private fun getMutableVaultTimeoutActionFlow(
|
||||
userId: String,
|
||||
): MutableSharedFlow<VaultTimeoutAction?> =
|
||||
mutableVaultTimeoutActionFlowMap.getOrPut(userId) {
|
||||
bufferedMutableSharedFlow(replay = 1)
|
||||
}
|
||||
|
||||
private fun getMutableVaultTimeoutInMinutesFlow(
|
||||
userId: String,
|
||||
): MutableSharedFlow<Int?> =
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.x8bit.bitwarden.data.platform.repository.model
|
||||
|
||||
/**
|
||||
* Represents different type of actions that may be performed when a vault times out.
|
||||
*
|
||||
* The [value] is used for consistent storage purposes.
|
||||
*/
|
||||
enum class VaultTimeoutAction(
|
||||
val value: Int,
|
||||
) {
|
||||
/**
|
||||
* The vault should lock when it times out.
|
||||
*/
|
||||
LOCK(0),
|
||||
|
||||
/**
|
||||
* The user should be logged out when their vault times out.
|
||||
*/
|
||||
LOGOUT(1),
|
||||
}
|
||||
Reference in New Issue
Block a user