1
0
Fork 0
mirror of https://github.com/bitwarden/android.git synced 2025-03-30 01:29:19 +03:00

Lazily determine autofill setting ()

This commit is contained in:
Lucas Kivi 2024-01-24 14:03:26 -06:00 committed by Álison Fernandes
parent 0edec8419e
commit ee8b9563a3
3 changed files with 36 additions and 11 deletions
app/src
main/java/com/x8bit/bitwarden/data/platform/repository
test/java/com/x8bit/bitwarden/data/platform/repository

View file

@ -87,6 +87,9 @@ interface SettingsRepository {
/**
* Emits updates whenever there is a change in the app's status for supporting autofill.
*
* Note that the correct value is only populated upon subscription so calling [StateFlow.value]
* may result in an out-of-date value.
*/
val isAutofillEnabledStateFlow: StateFlow<Boolean>

View file

@ -12,10 +12,13 @@ import com.x8bit.bitwarden.data.vault.datasource.sdk.VaultSdkSource
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppLanguage
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
@ -326,9 +329,15 @@ class SettingsRepositoryImpl(
}
}
@OptIn(ExperimentalCoroutinesApi::class)
private fun observeAutofillEnabledChanges() {
appForegroundManager
.appForegroundStateFlow
mutableIsAutofillEnabledStateFlow
// Only observe when subscribed to.
.subscriptionCount.map { it > 0 }
.filter { hasSubscribers -> hasSubscribers }
.flatMapLatest {
appForegroundManager.appForegroundStateFlow
}
.onEach {
mutableIsAutofillEnabledStateFlow.value = isAutofillEnabledAndSupported
}

View file

@ -480,6 +480,15 @@ class SettingsRepositoryTest {
@Test
fun `isAutofillEnabledStateFlow should emit updates if necessary when the app foreground state changes and disableAutofill is called`() =
runTest {
// Updates are not received without an isAutofillEnabledStateFlow subscriber.
isAutofillEnabledAndSupported = true
mutableAppForegroundStateFlow.value = AppForegroundState.FOREGROUNDED
assertFalse(settingsRepository.isAutofillEnabledStateFlow.value)
// Revert back to initial state.
isAutofillEnabledAndSupported = false
mutableAppForegroundStateFlow.value = AppForegroundState.BACKGROUNDED
settingsRepository.isAutofillEnabledStateFlow.test {
assertFalse(awaitItem())
@ -511,17 +520,21 @@ class SettingsRepositoryTest {
@Suppress("MaxLineLength")
@Test
fun `disableAutofill should trigger an emission of false from isAutofillEnabledStateFlow and disable autofill with the OS`() {
// Start in a state where autofill is enabled
isAutofillEnabledAndSupported = true
mutableAppForegroundStateFlow.value = AppForegroundState.FOREGROUNDED
assertTrue(settingsRepository.isAutofillEnabledStateFlow.value)
fun `disableAutofill should trigger an emission of false from isAutofillEnabledStateFlow and disable autofill with the OS`() =
runTest {
// Start in a state where autofill is enabled
isAutofillEnabledAndSupported = true
mutableAppForegroundStateFlow.value = AppForegroundState.FOREGROUNDED
settingsRepository.isAutofillEnabledStateFlow.test {
assertTrue(awaitItem())
expectNoEvents()
}
settingsRepository.disableAutofill()
settingsRepository.disableAutofill()
assertFalse(settingsRepository.isAutofillEnabledStateFlow.value)
verify { autofillManager.disableAutofillServices() }
}
assertFalse(settingsRepository.isAutofillEnabledStateFlow.value)
verify { autofillManager.disableAutofillServices() }
}
@Test
fun `getPullToRefreshEnabledFlow should react to changes in SettingsDiskSource`() = runTest {