Adding the ability to add URIs as a comma delimited list in block auto fill (#718)

This commit is contained in:
Joshua Queen 2024-01-22 21:15:08 -05:00 committed by Álison Fernandes
parent 427299eddf
commit 6a49a37ef0
2 changed files with 42 additions and 7 deletions

View file

@ -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)
uriList.forEach { newUri ->
val uriIndex = currentUris.indexOf(newUri)
if (uriIndex != -1) {
currentUris[uriIndex] = action.newUri
currentUris[uriIndex] = newUri
} else {
currentUris.add(action.newUri)
currentUris.add(newUri)
}
}
settingsRepository.blockedAutofillUris = currentUris

View file

@ -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()