From 6a49a37ef0a01513dba47694d9392d7250c8f009 Mon Sep 17 00:00:00 2001 From: Joshua Queen <139182194+joshua-livefront@users.noreply.github.com> Date: Mon, 22 Jan 2024 21:15:08 -0500 Subject: [PATCH] Adding the ability to add URIs as a comma delimited list in block auto fill (#718) --- .../blockautofill/BlockAutoFillViewModel.kt | 25 +++++++++++++------ .../BlockAutoFillViewModelTest.kt | 24 ++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/blockautofill/BlockAutoFillViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/blockautofill/BlockAutoFillViewModel.kt index 52a385cc2..8af17cd8e 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/blockautofill/BlockAutoFillViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/blockautofill/BlockAutoFillViewModel.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.SavedStateHandle import com.x8bit.bitwarden.data.platform.repository.SettingsRepository import com.x8bit.bitwarden.ui.platform.base.BaseViewModel import com.x8bit.bitwarden.ui.platform.base.util.Text +import com.x8bit.bitwarden.ui.platform.feature.settings.autofill.blockautofill.util.isValidPattern import com.x8bit.bitwarden.ui.platform.feature.settings.autofill.blockautofill.util.validateUri import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.update @@ -98,7 +99,16 @@ class BlockAutoFillViewModel @Inject constructor( } private fun handleSaveUri(action: BlockAutoFillAction.SaveUri) { - val errorText = action.newUri.validateUri(settingsRepository.blockedAutofillUris) + val uriList = action.newUri.split(",").map { it.trim() } + + val errorText = uriList + .filter { uri -> + uri in settingsRepository.blockedAutofillUris || !uri.isValidPattern() + } + .mapNotNull { uri -> + uri.validateUri(settingsRepository.blockedAutofillUris) + } + .firstOrNull() if (errorText != null) { mutableStateFlow.update { currentState -> @@ -113,12 +123,13 @@ class BlockAutoFillViewModel @Inject constructor( } val currentUris = settingsRepository.blockedAutofillUris.toMutableList() - - val uriIndex = currentUris.indexOf(action.newUri) - if (uriIndex != -1) { - currentUris[uriIndex] = action.newUri - } else { - currentUris.add(action.newUri) + uriList.forEach { newUri -> + val uriIndex = currentUris.indexOf(newUri) + if (uriIndex != -1) { + currentUris[uriIndex] = newUri + } else { + currentUris.add(newUri) + } } settingsRepository.blockedAutofillUris = currentUris diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/blockautofill/BlockAutoFillViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/blockautofill/BlockAutoFillViewModelTest.kt index ee3f81060..1540fd993 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/blockautofill/BlockAutoFillViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/blockautofill/BlockAutoFillViewModelTest.kt @@ -135,6 +135,30 @@ class BlockAutoFillViewModelTest : BaseViewModelTest() { assertEquals(expectedState, viewModel.stateFlow.value) } + @Test + fun `on SaveUri action with valid URIs in a list should add URIs to list`() = runTest { + val initialUris = mutableListOf("http://existing.com") + val newUris = "http://new.com, http://another.com" + + every { settingsRepository.blockedAutofillUris } answers { initialUris.toList() } + every { settingsRepository.blockedAutofillUris = any() } answers { + initialUris.clear() + initialUris.addAll(firstArg()) + } + + val viewModel = createViewModel() + viewModel.trySendAction(BlockAutoFillAction.SaveUri(newUri = newUris)) + + val expectedState = BlockAutoFillState( + dialog = null, + viewState = BlockAutoFillState.ViewState.Content( + blockedUris = listOf("http://existing.com", "http://new.com", "http://another.com"), + ), + ) + + assertEquals(expectedState, viewModel.stateFlow.value) + } + @Test fun `on BackClick should emit NavigateBack`() = runTest { val viewModel = createViewModel()