Skip to content

Commit

Permalink
PM-14009 Refactor storing first time values to the first time action …
Browse files Browse the repository at this point in the history
…manager (#4161)
  • Loading branch information
dseverns-livefront authored Oct 25, 2024
1 parent 53d4c4c commit eb4ffeb
Show file tree
Hide file tree
Showing 21 changed files with 196 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,4 @@ interface AuthRepository : AuthenticatorProvider, AuthRequestManager {
* Update the value of the onboarding status for the user.
*/
fun setOnboardingStatus(userId: String, status: OnboardingStatus?)

/**
* Update the value of the showImportLogins status for the user.
*/
fun setShowImportLogins(showImportLogins: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1327,11 +1327,6 @@ class AuthRepositoryImpl(
authDiskSource.storeOnboardingStatus(userId = userId, onboardingStatus = status)
}

override fun setShowImportLogins(showImportLogins: Boolean) {
val userId: String = activeUserId ?: return
authDiskSource.storeShowImportLogins(userId = userId, showImportLogins = showImportLogins)
}

@Suppress("CyclomaticComplexMethod")
private suspend fun validatePasswordAgainstPolicy(
password: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,21 @@ interface FirstTimeActionManager {
* a default configuration.
*/
val currentOrDefaultUserFirstTimeState: FirstTimeState

/**
* Stores the given value for whether or not the active user has signalled they want to
* set up unlock options later, during onboarding.
*/
fun storeShowUnlockSettingBadge(showBadge: Boolean)

/**
* Stores the given value for whether or not the active user has signalled they want to
* enable autofill later, during onboarding.
*/
fun storeShowAutoFillSettingBadge(showBadge: Boolean)

/**
* Update the value of the showImportLogins status for the active user.
*/
fun storeShowImportLogins(showImportLogins: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,28 @@ class FirstTimeActionManagerImpl @Inject constructor(
showSetupUnlockCard = null,
showSetupAutofillCard = null,
)

override fun storeShowUnlockSettingBadge(showBadge: Boolean) {
val activeUserId = authDiskSource.userState?.activeUserId ?: return
settingsDiskSource.storeShowUnlockSettingBadge(
userId = activeUserId,
showBadge = showBadge,
)
}

override fun storeShowAutoFillSettingBadge(showBadge: Boolean) {
val activeUserId = authDiskSource.userState?.activeUserId ?: return
settingsDiskSource.storeShowAutoFillSettingBadge(
userId = activeUserId,
showBadge = showBadge,
)
}

override fun storeShowImportLogins(showImportLogins: Boolean) {
val activeUserId = authDiskSource.userState?.activeUserId ?: return
authDiskSource.storeShowImportLogins(
userId = activeUserId,
showImportLogins = showImportLogins,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,38 +254,4 @@ interface SettingsRepository {
* Record that a user has logged in on this device.
*/
fun storeUserHasLoggedInValue(userId: String)

/**
* Gets whether or not the given [userId] has signalled they want to enable autofill
* later, during onboarding.
*/
fun getShowAutoFillSettingBadge(userId: String): Boolean

/**
* Stores the given value for whether or not the given [userId] has signalled they want to
* enable autofill later, during onboarding.
*/
fun storeShowAutoFillSettingBadge(userId: String, showBadge: Boolean)

/**
* Gets whether or not the given [userId] has signalled they want to enable unlock options
* later, during onboarding.
*/
fun getShowUnlockSettingBadge(userId: String): Boolean

/**
* Stores the given value for whether or not the given [userId] has signalled they want to
* set up unlock options later, during onboarding.
*/
fun storeShowUnlockSettingBadge(userId: String, showBadge: Boolean)

/**
* Gets whether or not the given [userId] has signalled they want to enable autofill
*/
fun getShowAutofillBadgeFlow(userId: String): Flow<Boolean>

/**
* Gets whether or not the given [userId] has signalled they want to enable unlock options
*/
fun getShowUnlockBadgeFlow(userId: String): Flow<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -538,28 +538,6 @@ class SettingsRepositoryImpl(
settingsDiskSource.storeUseHasLoggedInPreviously(userId)
}

override fun getShowAutoFillSettingBadge(userId: String): Boolean =
settingsDiskSource.getShowAutoFillSettingBadge(userId) ?: false

override fun storeShowAutoFillSettingBadge(userId: String, showBadge: Boolean) {
settingsDiskSource.storeShowAutoFillSettingBadge(userId, showBadge)
}

override fun getShowUnlockSettingBadge(userId: String): Boolean =
settingsDiskSource.getShowUnlockSettingBadge(userId) ?: false

override fun storeShowUnlockSettingBadge(userId: String, showBadge: Boolean) {
settingsDiskSource.storeShowUnlockSettingBadge(userId, showBadge)
}

override fun getShowAutofillBadgeFlow(userId: String): Flow<Boolean> =
settingsDiskSource.getShowAutoFillSettingBadgeFlow(userId)
.map { it ?: false }

override fun getShowUnlockBadgeFlow(userId: String): Flow<Boolean> =
settingsDiskSource.getShowUnlockSettingBadgeFlow(userId)
.map { it ?: false }

/**
* If there isn't already one generated, generate a symmetric sync key that would be used
* for communicating via IPC.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.x8bit.bitwarden.data.auth.datasource.disk.model.OnboardingStatus
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.platform.manager.FirstTimeActionManager
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -25,6 +26,7 @@ class SetupAutoFillViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val settingsRepository: SettingsRepository,
private val authRepository: AuthRepository,
private val firstTimeActionManager: FirstTimeActionManager,
) :
BaseViewModel<SetupAutoFillState, SetupAutoFillEvent, SetupAutoFillAction>(
// We load the state from the savedStateHandle for testing purposes.
Expand Down Expand Up @@ -97,7 +99,7 @@ class SetupAutoFillViewModel @Inject constructor(
}

private fun handleTurnOnLaterConfirmClick() {
settingsRepository.storeShowAutoFillSettingBadge(state.userId, true)
firstTimeActionManager.storeShowAutoFillSettingBadge(showBadge = true)
updateOnboardingStatusToNextStep()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.datasource.disk.model.OnboardingStatus
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.platform.manager.BiometricsEncryptionManager
import com.x8bit.bitwarden.data.platform.manager.FirstTimeActionManager
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.BiometricsKeyResult
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
Expand All @@ -32,6 +33,7 @@ class SetupUnlockViewModel @Inject constructor(
private val authRepository: AuthRepository,
private val settingsRepository: SettingsRepository,
private val biometricsEncryptionManager: BiometricsEncryptionManager,
private val firstTimeActionManager: FirstTimeActionManager,
) : BaseViewModel<SetupUnlockState, SetupUnlockEvent, SetupUnlockAction>(
// We load the state from the savedStateHandle for testing purposes.
initialState = savedStateHandle[KEY_STATE] ?: run {
Expand Down Expand Up @@ -109,7 +111,7 @@ class SetupUnlockViewModel @Inject constructor(
}

private fun handleSetUpLaterClick() {
settingsRepository.storeShowUnlockSettingBadge(state.userId, true)
firstTimeActionManager.storeShowUnlockSettingBadge(showBadge = true)
updateOnboardingStatusToNextStep()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.x8bit.bitwarden.data.auth.repository.model.UserFingerprintResult
import com.x8bit.bitwarden.data.auth.repository.util.policyInformation
import com.x8bit.bitwarden.data.platform.manager.BiometricsEncryptionManager
import com.x8bit.bitwarden.data.platform.manager.FeatureFlagManager
import com.x8bit.bitwarden.data.platform.manager.FirstTimeActionManager
import com.x8bit.bitwarden.data.platform.manager.PolicyManager
import com.x8bit.bitwarden.data.platform.manager.model.FlagKey
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
Expand Down Expand Up @@ -48,6 +49,7 @@ class AccountSecurityViewModel @Inject constructor(
private val environmentRepository: EnvironmentRepository,
private val biometricsEncryptionManager: BiometricsEncryptionManager,
private val featureFlagManager: FeatureFlagManager,
private val firstTimeActionManager: FirstTimeActionManager,
policyManager: PolicyManager,
savedStateHandle: SavedStateHandle,
) : BaseViewModel<AccountSecurityState, AccountSecurityEvent, AccountSecurityAction>(
Expand Down Expand Up @@ -114,10 +116,10 @@ class AccountSecurityViewModel @Inject constructor(
}
.launchIn(viewModelScope)

settingsRepository
.getShowUnlockBadgeFlow(state.userId)
firstTimeActionManager
.firstTimeStateFlow
.map {
AccountSecurityAction.Internal.ShowUnlockBadgeUpdated(it)
AccountSecurityAction.Internal.ShowUnlockBadgeUpdated(it.showSetupUnlockCard)
}
.onEach(::sendAction)
.launchIn(viewModelScope)
Expand Down Expand Up @@ -164,7 +166,6 @@ class AccountSecurityViewModel @Inject constructor(
}

private fun handleUnlockCardCtaClick() {
dismissUnlockNotificationBadge()
sendEvent(AccountSecurityEvent.NavigateToSetupUnlockScreen)
}

Expand Down Expand Up @@ -443,8 +444,7 @@ class AccountSecurityViewModel @Inject constructor(

private fun dismissUnlockNotificationBadge() {
if (!state.shouldShowUnlockActionCard) return
settingsRepository.storeShowUnlockSettingBadge(
userId = state.userId,
firstTimeActionManager.storeShowUnlockSettingBadge(
showBadge = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Parcelable
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.platform.manager.FirstTimeActionManager
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.data.platform.util.isBuildVersionBelow
Expand All @@ -29,6 +30,7 @@ class AutoFillViewModel @Inject constructor(
authRepository: AuthRepository,
private val savedStateHandle: SavedStateHandle,
private val settingsRepository: SettingsRepository,
private val firstTimeActionManager: FirstTimeActionManager,
) : BaseViewModel<AutoFillState, AutoFillEvent, AutoFillAction>(
initialState = savedStateHandle[KEY_STATE]
?: run {
Expand Down Expand Up @@ -74,9 +76,9 @@ class AutoFillViewModel @Inject constructor(
.onEach(::sendAction)
.launchIn(viewModelScope)

settingsRepository
.getShowAutofillBadgeFlow(userId = state.activeUserId)
.map { AutoFillAction.Internal.UpdateShowAutofillActionCard(it) }
firstTimeActionManager
.firstTimeStateFlow
.map { AutoFillAction.Internal.UpdateShowAutofillActionCard(it.showSetupAutofillCard) }
.onEach(::sendAction)
.launchIn(viewModelScope)
}
Expand Down Expand Up @@ -117,7 +119,6 @@ class AutoFillViewModel @Inject constructor(
}

private fun handleAutoFillActionCardCtClick() {
dismissShowAutofillActionCard()
sendEvent(AutoFillEvent.NavigateToSetupAutofill)
}

Expand Down Expand Up @@ -194,8 +195,7 @@ class AutoFillViewModel @Inject constructor(

private fun dismissShowAutofillActionCard() {
if (!state.showAutofillActionCard) return
settingsRepository.storeShowAutoFillSettingBadge(
userId = state.activeUserId,
firstTimeActionManager.storeShowAutoFillSettingBadge(
showBadge = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.x8bit.bitwarden.ui.platform.feature.settings.vault

import androidx.lifecycle.viewModelScope
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.platform.manager.FeatureFlagManager
import com.x8bit.bitwarden.data.platform.manager.FirstTimeActionManager
import com.x8bit.bitwarden.data.platform.manager.model.FlagKey
Expand All @@ -23,7 +22,6 @@ import javax.inject.Inject
class VaultSettingsViewModel @Inject constructor(
environmentRepository: EnvironmentRepository,
featureFlagManager: FeatureFlagManager,
private val authRepository: AuthRepository,
private val firstTimeActionManager: FirstTimeActionManager,
) : BaseViewModel<VaultSettingsState, VaultSettingsEvent, VaultSettingsAction>(
initialState = run {
Expand Down Expand Up @@ -80,11 +78,11 @@ class VaultSettingsViewModel @Inject constructor(
}

private fun handleImportLoginsCardDismissClicked() {
dismissImportLoginsCard()
if (!state.shouldShowImportCard) return
firstTimeActionManager.storeShowImportLogins(showImportLogins = false)
}

private fun handleImportLoginsCardClicked() {
dismissImportLoginsCard()
sendEvent(VaultSettingsEvent.NavigateToImportVault(state.importUrl))
}

Expand Down Expand Up @@ -121,11 +119,6 @@ class VaultSettingsViewModel @Inject constructor(
VaultSettingsEvent.NavigateToImportVault(state.importUrl),
)
}

private fun dismissImportLoginsCard() {
if (!state.shouldShowImportCard) return
authRepository.setShowImportLogins(showImportLogins = false)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.x8bit.bitwarden.data.auth.repository.model.SwitchAccountResult
import com.x8bit.bitwarden.data.auth.repository.model.UserState
import com.x8bit.bitwarden.data.auth.repository.model.ValidatePasswordResult
import com.x8bit.bitwarden.data.platform.manager.FeatureFlagManager
import com.x8bit.bitwarden.data.platform.manager.FirstTimeActionManager
import com.x8bit.bitwarden.data.platform.manager.PolicyManager
import com.x8bit.bitwarden.data.platform.manager.clipboard.BitwardenClipboardManager
import com.x8bit.bitwarden.data.platform.manager.event.OrganizationEventManager
Expand Down Expand Up @@ -71,6 +72,7 @@ class VaultViewModel @Inject constructor(
private val settingsRepository: SettingsRepository,
private val vaultRepository: VaultRepository,
private val featureFlagManager: FeatureFlagManager,
private val firstTimeActionManager: FirstTimeActionManager,
) : BaseViewModel<VaultState, VaultEvent, VaultAction>(
initialState = run {
val userState = requireNotNull(authRepository.userStateFlow.value)
Expand Down Expand Up @@ -180,12 +182,12 @@ class VaultViewModel @Inject constructor(
}

private fun handleImportActionCardClick() {
dismissImportLoginCard()
sendEvent(VaultEvent.NavigateToImportLogins)
}

private fun handleDismissImportActionCard() {
dismissImportLoginCard()
if (!state.showImportActionCard) return
firstTimeActionManager.storeShowImportLogins(false)
}

private fun handleIconLoadingSettingReceive(
Expand Down Expand Up @@ -631,11 +633,6 @@ class VaultViewModel @Inject constructor(
}

//endregion VaultAction Handlers

private fun dismissImportLoginCard() {
if (!state.showImportActionCard) return
authRepository.setShowImportLogins(false)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6361,13 +6361,6 @@ class AuthRepositoryTest {
assertNull(fakeAuthDiskSource.getOnboardingStatus(USER_ID_1))
}

@Test
fun `setShowImportLogins should save the showImportLogins to disk`() {
fakeAuthDiskSource.userState = MULTI_USER_STATE
repository.setShowImportLogins(showImportLogins = true)
assertEquals(true, fakeAuthDiskSource.getShowImportLogins(USER_ID_1))
}

companion object {
private const val UNIQUE_APP_ID = "testUniqueAppId"
private const val NAME = "Example Name"
Expand Down
Loading

0 comments on commit eb4ffeb

Please sign in to comment.