Add crash logging boolean to settings repository and settings disk source (#851)

This commit is contained in:
Joshua Queen 2024-01-29 15:44:15 -05:00 committed by Álison Fernandes
parent 1ffd233e7e
commit 0e5e6b4444
7 changed files with 108 additions and 0 deletions

View file

@ -43,6 +43,16 @@ interface SettingsDiskSource {
*/
val isIconLoadingDisabledFlow: Flow<Boolean?>
/**
* The current setting for if crash logging is enabled.
*/
var isCrashLoggingEnabled: Boolean?
/**
* The current setting for if crash logging is enabled.
*/
val isCrashLoggingEnabledFlow: Flow<Boolean?>
/**
* Clears all the settings data for the given user.
*/

View file

@ -29,6 +29,7 @@ private const val APPROVE_PASSWORDLESS_LOGINS_KEY = "$BASE_KEY:approvePasswordle
private const val SCREEN_CAPTURE_ALLOW_KEY = "$BASE_KEY:screenCaptureAllowed"
private const val SYSTEM_BIOMETRIC_INTEGRITY_SOURCE_KEY = "$BASE_KEY:biometricIntegritySource"
private const val ACCOUNT_BIOMETRIC_INTEGRITY_VALID_KEY = "$BASE_KEY:accountBiometricIntegrityValid"
private const val CRASH_LOGGING_ENABLED_KEY = "$BASE_KEY:crashLoggingEnabled"
/**
* Primary implementation of [SettingsDiskSource].
@ -56,6 +57,9 @@ class SettingsDiskSourceImpl(
private val mutableIsIconLoadingDisabledFlow =
bufferedMutableSharedFlow<Boolean?>()
private val mutableIsCrashLoggingEnabledFlow =
bufferedMutableSharedFlow<Boolean?>()
private val mutableScreenCaptureAllowedFlowMap =
mutableMapOf<String, MutableSharedFlow<Boolean?>>()
@ -106,6 +110,17 @@ class SettingsDiskSourceImpl(
get() = mutableIsIconLoadingDisabledFlow
.onSubscription { emit(getBoolean(DISABLE_ICON_LOADING_KEY)) }
override var isCrashLoggingEnabled: Boolean?
get() = getBoolean(key = CRASH_LOGGING_ENABLED_KEY)
set(value) {
putBoolean(key = CRASH_LOGGING_ENABLED_KEY, value = value)
mutableIsCrashLoggingEnabledFlow.tryEmit(value)
}
override val isCrashLoggingEnabledFlow: Flow<Boolean?>
get() = mutableIsCrashLoggingEnabledFlow
.onSubscription { emit(getBoolean(CRASH_LOGGING_ENABLED_KEY)) }
override fun clearData(userId: String) {
storeVaultTimeoutInMinutes(userId = userId, vaultTimeoutInMinutes = null)
storeVaultTimeoutAction(userId = userId, vaultTimeoutAction = null)

View file

@ -51,6 +51,16 @@ interface SettingsRepository {
*/
val isIconLoadingDisabledFlow: Flow<Boolean>
/**
* The current setting for crash logging.
*/
var isCrashLoggingEnabled: Boolean
/**
* Emits updates that track the [isCrashLoggingEnabled] value.
*/
val isCrashLoggingEnabledFlow: Flow<Boolean>
/**
* The [VaultTimeout] for the current user.
*/

View file

@ -17,6 +17,7 @@ import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppLang
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
@ -112,6 +113,22 @@ class SettingsRepositoryImpl(
?: false,
)
override var isCrashLoggingEnabled: Boolean
get() = settingsDiskSource.isCrashLoggingEnabled ?: true
set(value) {
settingsDiskSource.isCrashLoggingEnabled = value
}
override val isCrashLoggingEnabledFlow: Flow<Boolean>
get() = settingsDiskSource
.isCrashLoggingEnabledFlow
.map { it ?: isCrashLoggingEnabled }
.stateIn(
scope = unconfinedScope,
started = SharingStarted.Eagerly,
initialValue = isCrashLoggingEnabled,
)
override var vaultTimeout: VaultTimeout
get() = activeUserId
?.let {

View file

@ -252,6 +252,33 @@ class SettingsDiskSourceTest {
)
}
@Test
fun `isCrashLoggingEnabled should pull from and update SharedPreferences`() {
val isCrashLoggingEnabled = "bwPreferencesStorage:crashLoggingEnabled"
val expected = false
assertNull(settingsDiskSource.isCrashLoggingEnabled)
fakeSharedPreferences
.edit {
putBoolean(
isCrashLoggingEnabled,
expected,
)
}
assertEquals(
expected,
settingsDiskSource.isCrashLoggingEnabled,
)
settingsDiskSource.isCrashLoggingEnabled = true
assertTrue(
fakeSharedPreferences.getBoolean(
isCrashLoggingEnabled, false,
),
)
}
@Test
fun `appTheme when values are present should pull from SharedPreferences`() {
val appThemeBaseKey = "bwPreferencesStorage:appTheme"

View file

@ -33,6 +33,9 @@ class FakeSettingsDiskSource : SettingsDiskSource {
private val mutableIsIconLoadingDisabled =
bufferedMutableSharedFlow<Boolean?>()
private val mutableIsCrashLoggingEnabled =
bufferedMutableSharedFlow<Boolean?>()
private val mutableScreenCaptureAllowedFlowMap =
mutableMapOf<String, MutableSharedFlow<Boolean?>>()
@ -46,6 +49,7 @@ class FakeSettingsDiskSource : SettingsDiskSource {
private val storedInlineAutofillEnabled = mutableMapOf<String, Boolean?>()
private val storedBlockedAutofillUris = mutableMapOf<String, List<String>?>()
private var storedIsIconLoadingDisabled: Boolean? = null
private var storedIsCrashLoggingEnabled: Boolean? = null
private val storedApprovePasswordLoginsEnabled = mutableMapOf<String, Boolean?>()
private val storedScreenCaptureAllowed = mutableMapOf<String, Boolean?>()
private var storedSystemBiometricIntegritySource: String? = null
@ -83,6 +87,18 @@ class FakeSettingsDiskSource : SettingsDiskSource {
emit(isIconLoadingDisabled)
}
override var isCrashLoggingEnabled: Boolean?
get() = storedIsCrashLoggingEnabled
set(value) {
storedIsCrashLoggingEnabled = value
mutableIsCrashLoggingEnabled.tryEmit(value)
}
override val isCrashLoggingEnabledFlow: Flow<Boolean?>
get() = mutableIsCrashLoggingEnabled.onSubscription {
emit(isCrashLoggingEnabled)
}
override fun getAccountBiometricIntegrityValidity(
userId: String,
systemBioIntegrityState: String,

View file

@ -171,6 +171,19 @@ class SettingsRepositoryTest {
assertFalse(fakeSettingsDiskSource.isIconLoadingDisabled!!)
}
@Test
fun `isCrashLoggingEnabled should pull from and update SettingsDiskSource`() {
assertTrue(settingsRepository.isCrashLoggingEnabled)
// Updates to the disk source change the repository value.
fakeSettingsDiskSource.isCrashLoggingEnabled = false
assertFalse(settingsRepository.isCrashLoggingEnabled)
// Updates to the repository change the disk source value
settingsRepository.isCrashLoggingEnabled = true
assertTrue(fakeSettingsDiskSource.isCrashLoggingEnabled!!)
}
@Test
fun `appTheme should pull from and update SettingsDiskSource`() {
fakeAuthDiskSource.userState = null