diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/FirstTimeActionManagerImpl.kt b/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/FirstTimeActionManagerImpl.kt index af3c04864fd..318eb70f19d 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/FirstTimeActionManagerImpl.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/FirstTimeActionManagerImpl.kt @@ -39,80 +39,76 @@ class FirstTimeActionManagerImpl @Inject constructor( private val unconfinedScope = CoroutineScope(dispatcherManager.unconfined) - override val allSettingsBadgeCountFlow: StateFlow - get() = combine( - listOf( - allSecuritySettingsBadgeCountFlow, - allAutofillSettingsBadgeCountFlow, - allVaultSettingsBadgeCountFlow, - ), - ) { - it.sum() + @OptIn(ExperimentalCoroutinesApi::class) + override val allSecuritySettingsBadgeCountFlow: StateFlow = authDiskSource + .activeUserIdChangesFlow + .filterNotNull() + .flatMapLatest { + // can be expanded to support multiple security settings + settingsDiskSource.getShowUnlockSettingBadgeFlow(userId = it) + .map { showUnlockBadge -> + listOfNotNull(showUnlockBadge) + } + .map { list -> + list.count { badgeOnValue -> badgeOnValue } + } } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Lazily, - initialValue = 0, - ) + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = 0, + ) @OptIn(ExperimentalCoroutinesApi::class) - override val allSecuritySettingsBadgeCountFlow: StateFlow - get() = authDiskSource - .activeUserIdChangesFlow - .filterNotNull() - .flatMapLatest { - // can be expanded to support multiple security settings - settingsDiskSource.getShowUnlockSettingBadgeFlow(userId = it) - .map { showUnlockBadge -> - listOfNotNull(showUnlockBadge) - } - .map { list -> - list.count { badgeOnValue -> badgeOnValue } - } + override val allAutofillSettingsBadgeCountFlow: StateFlow = authDiskSource + .activeUserIdChangesFlow + .filterNotNull() + .flatMapLatest { + combine( + getShowAutofillSettingBadgeFlowInternal(userId = it), + getShowBrowserAutofillSettingBadgeFlowInternal(userId = it), + ) { showAutofillBadge, showBrowserAutofillBadge -> + listOf(showAutofillBadge, showBrowserAutofillBadge) } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Lazily, - initialValue = 0, - ) - - @OptIn(ExperimentalCoroutinesApi::class) - override val allAutofillSettingsBadgeCountFlow: StateFlow - get() = authDiskSource - .activeUserIdChangesFlow - .filterNotNull() - .flatMapLatest { - combine( - getShowAutofillSettingBadgeFlowInternal(userId = it), - getShowBrowserAutofillSettingBadgeFlowInternal(userId = it), - ) { showAutofillBadge, showBrowserAutofillBadge -> - listOf(showAutofillBadge, showBrowserAutofillBadge) + .map { list -> + list.count { showBadge -> showBadge } } - .map { list -> - list.count { showBadge -> showBadge } - } - } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Lazily, - initialValue = 0, - ) + } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = 0, + ) @OptIn(ExperimentalCoroutinesApi::class) - override val allVaultSettingsBadgeCountFlow: StateFlow - get() = authDiskSource - .activeUserIdChangesFlow - .filterNotNull() - .flatMapLatest { - getShowImportLoginsSettingBadgeFlowInternal(userId = it) - .map { showImportLogins -> listOf(showImportLogins) } - .map { list -> list.count { showImportLogins -> showImportLogins } } - } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Lazily, - initialValue = 0, - ) + override val allVaultSettingsBadgeCountFlow: StateFlow = authDiskSource + .activeUserIdChangesFlow + .filterNotNull() + .flatMapLatest { + getShowImportLoginsSettingBadgeFlowInternal(userId = it) + .map { showImportLogins -> listOf(showImportLogins) } + .map { list -> list.count { showImportLogins -> showImportLogins } } + } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = 0, + ) + + override val allSettingsBadgeCountFlow: StateFlow = combine( + listOf( + allSecuritySettingsBadgeCountFlow, + allAutofillSettingsBadgeCountFlow, + allVaultSettingsBadgeCountFlow, + ), + ) { + it.sum() + } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = 0, + ) /** * Returns a [Flow] that emits every time the active user's first time state is changed. diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/policy/UserNotificationPolicyManagerImpl.kt b/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/policy/UserNotificationPolicyManagerImpl.kt index 0f03257f301..383c8d07f23 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/policy/UserNotificationPolicyManagerImpl.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/policy/UserNotificationPolicyManagerImpl.kt @@ -48,28 +48,27 @@ internal class UserNotificationPolicyManagerImpl( } @OptIn(ExperimentalCoroutinesApi::class) - override val displayDataFlow: StateFlow - get() = authDiskSource - .activeUserIdChangesFlow - .flatMapLatest { userId -> - userId ?: return@flatMapLatest flowOf(null) - combine( - settingsDiskSource.getVaultPolicyBannerDismissedDateFlow(userId = userId), - policyManager - .getActivePoliciesFlow(type = PolicyType.ORGANIZATION_USER_NOTIFICATION) - .map { it.firstOrNull() }, - ) { revisionDate, policy -> - (policy?.userNotificationPolicyData)?.takeIf { - policy.revisionDate?.toEpochMilli() != revisionDate?.toEpochMilli() || - revisionDate == null - } + override val displayDataFlow: StateFlow = authDiskSource + .activeUserIdChangesFlow + .flatMapLatest { userId -> + userId ?: return@flatMapLatest flowOf(null) + combine( + settingsDiskSource.getVaultPolicyBannerDismissedDateFlow(userId = userId), + policyManager + .getActivePoliciesFlow(type = PolicyType.ORGANIZATION_USER_NOTIFICATION) + .map { it.firstOrNull() }, + ) { revisionDate, policy -> + (policy?.userNotificationPolicyData)?.takeIf { + policy.revisionDate?.toEpochMilli() != revisionDate?.toEpochMilli() || + revisionDate == null } } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Lazily, - initialValue = displayData, - ) + } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = displayData, + ) override fun dismissBanner() { policyManager diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/repository/SettingsRepositoryImpl.kt b/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/repository/SettingsRepositoryImpl.kt index 9653b44205a..424700ca88d 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/repository/SettingsRepositoryImpl.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/repository/SettingsRepositoryImpl.kt @@ -13,6 +13,7 @@ import com.x8bit.bitwarden.BuildConfig import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource import com.x8bit.bitwarden.data.auth.repository.model.PolicyInformation import com.x8bit.bitwarden.data.auth.repository.model.UserFingerprintResult +import com.x8bit.bitwarden.data.auth.repository.util.activeUserIdChangesFlow import com.x8bit.bitwarden.data.auth.repository.util.policyInformation import com.x8bit.bitwarden.data.autofill.accessibility.manager.AccessibilityEnabledManager import com.x8bit.bitwarden.data.autofill.manager.AutofillEnabledManager @@ -27,11 +28,13 @@ import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeout import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeoutAction import com.x8bit.bitwarden.data.vault.datasource.sdk.VaultSdkSource 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 import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map @@ -72,26 +75,24 @@ class SettingsRepositoryImpl( settingsDiskSource.appLanguage = value } - override val appLanguageStateFlow: StateFlow - get() = settingsDiskSource - .appLanguageFlow - .map { it ?: AppLanguage.DEFAULT } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = appLanguage, - ) + override val appLanguageStateFlow: StateFlow = settingsDiskSource + .appLanguageFlow + .map { it ?: AppLanguage.DEFAULT } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = appLanguage, + ) override var appTheme: AppTheme by settingsDiskSource::appTheme - override val appThemeStateFlow: StateFlow - get() = settingsDiskSource - .appThemeFlow - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = settingsDiskSource.appTheme, - ) + override val appThemeStateFlow: StateFlow = settingsDiskSource + .appThemeFlow + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = settingsDiskSource.appTheme, + ) override var isDynamicColorsEnabled: Boolean get() = settingsDiskSource.isDynamicColorsEnabled ?: false @@ -99,15 +100,14 @@ class SettingsRepositoryImpl( settingsDiskSource.isDynamicColorsEnabled = value } - override val isDynamicColorsEnabledFlow: StateFlow - get() = settingsDiskSource - .isDynamicColorsEnabledFlow - .map { it ?: false } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = isDynamicColorsEnabled, - ) + override val isDynamicColorsEnabledFlow: StateFlow = settingsDiskSource + .isDynamicColorsEnabledFlow + .map { it ?: false } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = isDynamicColorsEnabled, + ) override var initialAutofillDialogShown: Boolean get() = settingsDiskSource.initialAutofillDialogShown ?: false @@ -122,18 +122,20 @@ class SettingsRepositoryImpl( settingsDiskSource.storeLastSyncTime(userId = userId, lastSyncTime = value) } - override val vaultLastSyncStateFlow: StateFlow - get() = activeUserId - ?.let { - settingsDiskSource - .getLastSyncTimeFlow(userId = it) - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = settingsDiskSource.getLastSyncTime(userId = it), - ) - } - ?: MutableStateFlow(value = null) + @OptIn(ExperimentalCoroutinesApi::class) + override val vaultLastSyncStateFlow: StateFlow = authDiskSource + .activeUserIdChangesFlow + .flatMapLatest { userId -> + userId + ?.let { settingsDiskSource.getLastSyncTimeFlow(userId = it) } + ?: flowOf(value = null) + } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = activeUserId + ?.let { settingsDiskSource.getLastSyncTime(userId = it) }, + ) override var isAuthenticatorSyncEnabled: Boolean // Authenticator sync is enabled if there is an authenticator sync unlock key for @@ -175,17 +177,16 @@ class SettingsRepositoryImpl( settingsDiskSource.isIconLoadingDisabled = value } - override val isIconLoadingDisabledFlow: StateFlow - get() = settingsDiskSource - .isIconLoadingDisabledFlow - .map { it ?: false } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = settingsDiskSource - .isIconLoadingDisabled - ?: false, - ) + override val isIconLoadingDisabledFlow: StateFlow = settingsDiskSource + .isIconLoadingDisabledFlow + .map { it ?: false } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = settingsDiskSource + .isIconLoadingDisabled + ?: false, + ) override var clearClipboardFrequency: ClearClipboardFrequency get() = activeUserId ?.let { userId -> @@ -205,15 +206,14 @@ class SettingsRepositoryImpl( settingsDiskSource.isCrashLoggingEnabled = value } - override val isCrashLoggingEnabledFlow: Flow - get() = settingsDiskSource - .isCrashLoggingEnabledFlow - .map { it ?: isCrashLoggingEnabled } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = isCrashLoggingEnabled, - ) + override val isCrashLoggingEnabledFlow: Flow = settingsDiskSource + .isCrashLoggingEnabledFlow + .map { it ?: isCrashLoggingEnabled } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = isCrashLoggingEnabled, + ) override var hasUserLoggedInOrCreatedAccount: Boolean get() = settingsDiskSource.hasUserLoggedInOrCreatedAccount ?: false @@ -221,15 +221,14 @@ class SettingsRepositoryImpl( settingsDiskSource.hasUserLoggedInOrCreatedAccount = value } - override val hasUserLoggedInOrCreatedAccountFlow: Flow - get() = settingsDiskSource - .hasUserLoggedInOrCreatedAccountFlow - .map { it ?: hasUserLoggedInOrCreatedAccount } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = hasUserLoggedInOrCreatedAccount, - ) + override val hasUserLoggedInOrCreatedAccountFlow: Flow = settingsDiskSource + .hasUserLoggedInOrCreatedAccountFlow + .map { it ?: hasUserLoggedInOrCreatedAccount } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = hasUserLoggedInOrCreatedAccount, + ) override var vaultTimeout: VaultTimeout get() = activeUserId @@ -393,26 +392,24 @@ class SettingsRepositoryImpl( settingsDiskSource.screenCaptureAllowed = value } - override val isScreenCaptureAllowedStateFlow: StateFlow - get() = settingsDiskSource - .screenCaptureAllowedFlow - .map { isAllowed -> isAllowed ?: DEFAULT_IS_SCREEN_CAPTURE_ALLOWED } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Lazily, - initialValue = isScreenCaptureAllowed, - ) + override val isScreenCaptureAllowedStateFlow: StateFlow = settingsDiskSource + .screenCaptureAllowedFlow + .map { isAllowed -> isAllowed ?: DEFAULT_IS_SCREEN_CAPTURE_ALLOWED } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = isScreenCaptureAllowed, + ) - override val hasShownAccessibilityDisclaimerFlow: StateFlow - get() = settingsDiskSource - .hasShownAccessibilityDisclaimerFlow - .map { buildInfoManager.isFdroid || it ?: false } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Lazily, - initialValue = buildInfoManager.isFdroid || - settingsDiskSource.hasShownAccessibilityDisclaimer ?: false, - ) + override val hasShownAccessibilityDisclaimerFlow: StateFlow = settingsDiskSource + .hasShownAccessibilityDisclaimerFlow + .map { buildInfoManager.isFdroid || it ?: false } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = buildInfoManager.isFdroid || + settingsDiskSource.hasShownAccessibilityDisclaimer ?: false, + ) init { policyManager diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/data/vault/manager/VaultLockManagerImpl.kt b/app/src/main/kotlin/com/x8bit/bitwarden/data/vault/manager/VaultLockManagerImpl.kt index c022a2579f7..01533a26ef8 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/data/vault/manager/VaultLockManagerImpl.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/data/vault/manager/VaultLockManagerImpl.kt @@ -127,24 +127,25 @@ internal class VaultLockManagerImpl( get() = mutableVaultUnlockDataStateFlow.asStateFlow() @OptIn(ExperimentalCoroutinesApi::class) - override val isActiveUserUnlockingFlow: StateFlow - get() = authDiskSource - .activeUserIdChangesFlow - .flatMapLatest { activeUserId -> - vaultUnlockDataStateFlow.map { vaultUnlockData -> - vaultUnlockData.any { - it.userId == activeUserId && it.status == VaultUnlockData.Status.UNLOCKING - } + override val isActiveUserUnlockingFlow: StateFlow = authDiskSource + .activeUserIdChangesFlow + .flatMapLatest { activeUserId -> + vaultUnlockDataStateFlow.map { vaultUnlockData -> + vaultUnlockData.any { + it.userId == activeUserId && it.status == VaultUnlockData.Status.UNLOCKING } } - .distinctUntilChanged() - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Lazily, - initialValue = mutableVaultUnlockDataStateFlow.value.any { - it.userId == activeUserId && it.status == VaultUnlockData.Status.UNLOCKING - }, - ) + } + .distinctUntilChanged() + .stateIn( + scope = unconfinedScope, + // Eagerly, because callers read `.value` without collecting (e.g. UserStateManagerImpl) + // and Lazily would leave that value stale until the first collector subscribes. + started = SharingStarted.Eagerly, + initialValue = mutableVaultUnlockDataStateFlow.value.any { + it.userId == activeUserId && it.status == VaultUnlockData.Status.UNLOCKING + }, + ) override val vaultStateEventFlow: Flow get() = mutableVaultStateEventSharedFlow.asSharedFlow() diff --git a/app/src/test/kotlin/com/x8bit/bitwarden/data/platform/manager/policy/UserNotificationPolicyManagerTest.kt b/app/src/test/kotlin/com/x8bit/bitwarden/data/platform/manager/policy/UserNotificationPolicyManagerTest.kt index b54cfccb053..fdb05bdc464 100644 --- a/app/src/test/kotlin/com/x8bit/bitwarden/data/platform/manager/policy/UserNotificationPolicyManagerTest.kt +++ b/app/src/test/kotlin/com/x8bit/bitwarden/data/platform/manager/policy/UserNotificationPolicyManagerTest.kt @@ -201,11 +201,13 @@ class UserNotificationPolicyManagerTest { } @Test - fun `displayDataFlow should have an initial value matching displayData`() { + fun `displayDataFlow should emit a value matching displayData when collected`() = runTest { fakeAuthDiskSource.userState = SINGLE_USER_STATE setPolicies(createPolicy()) - assertEquals(EXPECTED_DISPLAY_DATA, userNotificationPolicyManager.displayDataFlow.value) + userNotificationPolicyManager.displayDataFlow.test { + assertEquals(EXPECTED_DISPLAY_DATA, expectMostRecentItem()) + } } @Test diff --git a/app/src/test/kotlin/com/x8bit/bitwarden/data/platform/repository/SettingsRepositoryTest.kt b/app/src/test/kotlin/com/x8bit/bitwarden/data/platform/repository/SettingsRepositoryTest.kt index 1e910914fbf..83d8d2750d4 100644 --- a/app/src/test/kotlin/com/x8bit/bitwarden/data/platform/repository/SettingsRepositoryTest.kt +++ b/app/src/test/kotlin/com/x8bit/bitwarden/data/platform/repository/SettingsRepositoryTest.kt @@ -54,6 +54,7 @@ import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Assertions.assertSame import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -332,6 +333,79 @@ class SettingsRepositoryTest { } } + @Test + fun `vaultLastSyncStateFlow should follow the active user when it changes`() = runTest { + val secondUserId = "secondUserId" + val firstInstant = Instant.ofEpochMilli(1_698_408_000_000L) + val secondInstant = Instant.ofEpochMilli(1_698_494_400_000L) + fakeSettingsDiskSource.storeLastSyncTime(userId = USER_ID, lastSyncTime = firstInstant) + fakeSettingsDiskSource.storeLastSyncTime( + userId = secondUserId, + lastSyncTime = secondInstant, + ) + fakeAuthDiskSource.userState = MOCK_USER_STATE + + settingsRepository + .vaultLastSyncStateFlow + .test { + assertEquals(firstInstant, expectMostRecentItem()) + + // Switch the active user without re-reading the property. + fakeAuthDiskSource.userState = MOCK_USER_STATE.copy( + activeUserId = secondUserId, + accounts = MOCK_USER_STATE.accounts + (secondUserId to MOCK_ACCOUNT), + ) + assertEquals(secondInstant, awaitItem()) + + // Updates for the previous user no longer affect the flow. + fakeSettingsDiskSource.storeLastSyncTime( + userId = USER_ID, + lastSyncTime = firstInstant.plusSeconds(60), + ) + expectNoEvents() + } + } + + @Test + fun `shared flow properties should return the same instance on every access`() { + assertSame( + settingsRepository.appLanguageStateFlow, + settingsRepository.appLanguageStateFlow, + ) + assertSame( + settingsRepository.appThemeStateFlow, + settingsRepository.appThemeStateFlow, + ) + assertSame( + settingsRepository.isDynamicColorsEnabledFlow, + settingsRepository.isDynamicColorsEnabledFlow, + ) + assertSame( + settingsRepository.vaultLastSyncStateFlow, + settingsRepository.vaultLastSyncStateFlow, + ) + assertSame( + settingsRepository.isIconLoadingDisabledFlow, + settingsRepository.isIconLoadingDisabledFlow, + ) + assertSame( + settingsRepository.isCrashLoggingEnabledFlow, + settingsRepository.isCrashLoggingEnabledFlow, + ) + assertSame( + settingsRepository.hasUserLoggedInOrCreatedAccountFlow, + settingsRepository.hasUserLoggedInOrCreatedAccountFlow, + ) + assertSame( + settingsRepository.isScreenCaptureAllowedStateFlow, + settingsRepository.isScreenCaptureAllowedStateFlow, + ) + assertSame( + settingsRepository.hasShownAccessibilityDisclaimerFlow, + settingsRepository.hasShownAccessibilityDisclaimerFlow, + ) + } + @Test fun `isIconLoadingDisabled should pull from and update SettingsDiskSource`() { assertFalse(settingsRepository.isIconLoadingDisabled) diff --git a/authenticator/src/main/kotlin/com/bitwarden/authenticator/data/auth/repository/AuthRepositoryImpl.kt b/authenticator/src/main/kotlin/com/bitwarden/authenticator/data/auth/repository/AuthRepositoryImpl.kt index 40001e6f7ad..a54b5dad5ee 100644 --- a/authenticator/src/main/kotlin/com/bitwarden/authenticator/data/auth/repository/AuthRepositoryImpl.kt +++ b/authenticator/src/main/kotlin/com/bitwarden/authenticator/data/auth/repository/AuthRepositoryImpl.kt @@ -41,15 +41,14 @@ class AuthRepositoryImpl @Inject constructor( override val isUnlockWithBiometricsEnabled: Boolean get() = authDiskSource.getUserBiometricUnlockKey() != null - override val isUnlockWithBiometricsEnabledFlow: StateFlow - get() = authDiskSource - .userBiometricUnlockKeyFlow - .map { it != null } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = isUnlockWithBiometricsEnabled, - ) + override val isUnlockWithBiometricsEnabledFlow: StateFlow = authDiskSource + .userBiometricUnlockKeyFlow + .map { it != null } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = isUnlockWithBiometricsEnabled, + ) override val appLockStateFlow: StateFlow = appLockManager.appLockStateFlow diff --git a/authenticator/src/main/kotlin/com/bitwarden/authenticator/data/platform/repository/SettingsRepositoryImpl.kt b/authenticator/src/main/kotlin/com/bitwarden/authenticator/data/platform/repository/SettingsRepositoryImpl.kt index 0d596ea290e..34f5779a820 100644 --- a/authenticator/src/main/kotlin/com/bitwarden/authenticator/data/platform/repository/SettingsRepositoryImpl.kt +++ b/authenticator/src/main/kotlin/com/bitwarden/authenticator/data/platform/repository/SettingsRepositoryImpl.kt @@ -50,34 +50,31 @@ class SettingsRepositoryImpl( settingsDiskSource.isDynamicColorsEnabled = value } - override val isDynamicColorsEnabledFlow: StateFlow - get() = settingsDiskSource - .isDynamicColorsEnabledFlow - .map { it ?: false } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = isDynamicColorsEnabled, - ) + override val isDynamicColorsEnabledFlow: StateFlow = settingsDiskSource + .isDynamicColorsEnabledFlow + .map { it ?: false } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = isDynamicColorsEnabled, + ) - override val appThemeStateFlow: StateFlow - get() = settingsDiskSource - .appThemeFlow - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = settingsDiskSource.appTheme, - ) + override val appThemeStateFlow: StateFlow = settingsDiskSource + .appThemeFlow + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = settingsDiskSource.appTheme, + ) - override val authenticatorAlertThresholdSecondsFlow: StateFlow - get() = settingsDiskSource - .getAlertThresholdSecondsFlow() - .map { it } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = settingsDiskSource.getAlertThresholdSeconds(), - ) + override val authenticatorAlertThresholdSecondsFlow: StateFlow = settingsDiskSource + .getAlertThresholdSecondsFlow() + .map { it } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = settingsDiskSource.getAlertThresholdSeconds(), + ) override var hasSeenWelcomeTutorial: Boolean get() = settingsDiskSource.hasSeenWelcomeTutorial @@ -85,14 +82,13 @@ class SettingsRepositoryImpl( settingsDiskSource.hasSeenWelcomeTutorial = value } - override val hasSeenWelcomeTutorialFlow: StateFlow - get() = settingsDiskSource - .hasSeenWelcomeTutorialFlow - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = hasSeenWelcomeTutorial, - ) + override val hasSeenWelcomeTutorialFlow: StateFlow = settingsDiskSource + .hasSeenWelcomeTutorialFlow + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = hasSeenWelcomeTutorial, + ) override var isScreenCaptureAllowed: Boolean get() = settingsDiskSource.getScreenCaptureAllowed() @@ -106,15 +102,15 @@ class SettingsRepositoryImpl( override var previouslySyncedBitwardenAccountIds: Set by settingsDiskSource::previouslySyncedBitwardenAccountIds - override val isScreenCaptureAllowedStateFlow: StateFlow - get() = settingsDiskSource.getScreenCaptureAllowedFlow() - .map { isAllowed -> isAllowed ?: DEFAULT_IS_SCREEN_CAPTURE_ALLOWED } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Lazily, - initialValue = settingsDiskSource.getScreenCaptureAllowed() - ?: DEFAULT_IS_SCREEN_CAPTURE_ALLOWED, - ) + override val isScreenCaptureAllowedStateFlow: StateFlow = settingsDiskSource + .getScreenCaptureAllowedFlow() + .map { isAllowed -> isAllowed ?: DEFAULT_IS_SCREEN_CAPTURE_ALLOWED } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = settingsDiskSource.getScreenCaptureAllowed() + ?: DEFAULT_IS_SCREEN_CAPTURE_ALLOWED, + ) override var isCrashLoggingEnabled: Boolean get() = settingsDiskSource.isCrashLoggingEnabled ?: true @@ -122,15 +118,14 @@ class SettingsRepositoryImpl( settingsDiskSource.isCrashLoggingEnabled = value } - override val isCrashLoggingEnabledFlow: Flow - get() = settingsDiskSource - .isCrashLoggingEnabledFlow - .map { it ?: isCrashLoggingEnabled } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = isCrashLoggingEnabled, - ) + override val isCrashLoggingEnabledFlow: Flow = settingsDiskSource + .isCrashLoggingEnabledFlow + .map { it ?: isCrashLoggingEnabled } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = isCrashLoggingEnabled, + ) override var isShowNextCodeEnabled: Boolean get() = settingsDiskSource.isShowNextCodeEnabled ?: false @@ -138,15 +133,14 @@ class SettingsRepositoryImpl( settingsDiskSource.isShowNextCodeEnabled = value } - override val isShowNextCodeEnabledFlow: Flow - get() = settingsDiskSource - .isShowNextCodeEnabledFlow - .map { it ?: isShowNextCodeEnabled } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = isShowNextCodeEnabled, - ) + override val isShowNextCodeEnabledFlow: Flow = settingsDiskSource + .isShowNextCodeEnabledFlow + .map { it ?: isShowNextCodeEnabled } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = isShowNextCodeEnabled, + ) override var hasUserDismissedDownloadBitwardenCard: Boolean get() = settingsDiskSource.hasUserDismissedDownloadBitwardenCard ?: false diff --git a/authenticator/src/test/kotlin/com/bitwarden/authenticator/data/platform/repository/SettingsRepositoryTest.kt b/authenticator/src/test/kotlin/com/bitwarden/authenticator/data/platform/repository/SettingsRepositoryTest.kt index 8f444e38d89..2245b6f0c42 100644 --- a/authenticator/src/test/kotlin/com/bitwarden/authenticator/data/platform/repository/SettingsRepositoryTest.kt +++ b/authenticator/src/test/kotlin/com/bitwarden/authenticator/data/platform/repository/SettingsRepositoryTest.kt @@ -6,12 +6,14 @@ import com.bitwarden.authenticator.data.platform.manager.lock.model.AppTimeout import com.bitwarden.authenticator.ui.platform.feature.settings.data.model.DefaultSaveOption import com.bitwarden.core.data.manager.dispatcher.FakeDispatcherManager import com.bitwarden.core.data.repository.util.bufferedMutableSharedFlow +import com.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme import io.mockk.every import io.mockk.just import io.mockk.mockk import io.mockk.runs import io.mockk.verify import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.Assertions.assertEquals @@ -22,8 +24,24 @@ import org.junit.jupiter.api.Test class SettingsRepositoryTest { private val mutableAppTimeoutInMinutesFlow = MutableStateFlow(null) + private val mutableDynamicColorsFlow = bufferedMutableSharedFlow() + private val mutableShowNextCodeFlow = bufferedMutableSharedFlow() private val settingsDiskSource: SettingsDiskSource = mockk { + // The repository eagerly shares these flows when it is constructed. every { getAlertThresholdSeconds() } returns 7 + every { getAlertThresholdSecondsFlow() } returns emptyFlow() + every { appTheme } returns AppTheme.DEFAULT + every { appThemeFlow } returns emptyFlow() + every { isDynamicColorsEnabled } returns null + every { isDynamicColorsEnabledFlow } returns mutableDynamicColorsFlow + every { hasSeenWelcomeTutorial } returns false + every { hasSeenWelcomeTutorialFlow } returns emptyFlow() + every { getScreenCaptureAllowed() } returns null + every { getScreenCaptureAllowedFlow() } returns emptyFlow() + every { isCrashLoggingEnabled } returns null + every { isCrashLoggingEnabledFlow } returns emptyFlow() + every { isShowNextCodeEnabled } returns null + every { isShowNextCodeEnabledFlow } returns mutableShowNextCodeFlow every { appTimeoutInMinutesFlow } returns mutableAppTimeoutInMinutesFlow every { appTimeoutInMinutes } answers { mutableAppTimeoutInMinutesFlow.value } } @@ -130,8 +148,6 @@ class SettingsRepositoryTest { @Test fun `isDynamicColorsEnabledFlow should match SettingsDiskSource`() = runTest { // Reading from repository should read from disk source: - val mutableDynamicColorsFlow = bufferedMutableSharedFlow() - every { settingsDiskSource.isDynamicColorsEnabledFlow } returns mutableDynamicColorsFlow every { settingsDiskSource.isDynamicColorsEnabled } returns null settingsRepository.isDynamicColorsEnabledFlow.test { @@ -189,8 +205,6 @@ class SettingsRepositoryTest { @Test fun `isShowNextCodeEnabledFlow should map null to false and emit updates`() = runTest { - val mutableShowNextCodeFlow = bufferedMutableSharedFlow() - every { settingsDiskSource.isShowNextCodeEnabledFlow } returns mutableShowNextCodeFlow every { settingsDiskSource.isShowNextCodeEnabled } returns null settingsRepository.isShowNextCodeEnabledFlow.test { diff --git a/data/src/main/kotlin/com/bitwarden/data/manager/flightrecorder/FlightRecorderManagerImpl.kt b/data/src/main/kotlin/com/bitwarden/data/manager/flightrecorder/FlightRecorderManagerImpl.kt index cc991c9ec1b..2415134a231 100644 --- a/data/src/main/kotlin/com/bitwarden/data/manager/flightrecorder/FlightRecorderManagerImpl.kt +++ b/data/src/main/kotlin/com/bitwarden/data/manager/flightrecorder/FlightRecorderManagerImpl.kt @@ -52,15 +52,14 @@ internal class FlightRecorderManagerImpl( .flightRecorderData ?: FlightRecorderDataSet(data = emptySet()) - override val flightRecorderDataFlow: StateFlow - get() = flightRecorderDiskSource - .flightRecorderDataFlow - .map { it ?: FlightRecorderDataSet(data = emptySet()) } - .stateIn( - scope = unconfinedScope, - started = SharingStarted.Eagerly, - initialValue = flightRecorderData, - ) + override val flightRecorderDataFlow: StateFlow = flightRecorderDiskSource + .flightRecorderDataFlow + .map { it ?: FlightRecorderDataSet(data = emptySet()) } + .stateIn( + scope = unconfinedScope, + started = SharingStarted.Eagerly, + initialValue = flightRecorderData, + ) init { // Always plant the tree, it will not log anything if the flight recorder is off. diff --git a/data/src/test/kotlin/com/bitwarden/data/manager/flightrecorder/FlightRecorderManagerTest.kt b/data/src/test/kotlin/com/bitwarden/data/manager/flightrecorder/FlightRecorderManagerTest.kt index 5f04a732388..1eb62eb5712 100644 --- a/data/src/test/kotlin/com/bitwarden/data/manager/flightrecorder/FlightRecorderManagerTest.kt +++ b/data/src/test/kotlin/com/bitwarden/data/manager/flightrecorder/FlightRecorderManagerTest.kt @@ -139,6 +139,8 @@ class FlightRecorderManagerTest { ), ) fakeFlightRecorderDiskSource.flightRecorderData = expired + assertEquals(expired, awaitItem()) + // The manager prunes the expired data, which is then emitted as empty. assertEquals(FlightRecorderDataSet(data = emptySet()), awaitItem()) fakeFlightRecorderDiskSource.flightRecorderData = null