From 98b2df6ec7264636da5415526b6126424faab2bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Fri, 19 Apr 2024 10:37:13 +0200 Subject: [PATCH] Fix duplicate entries when adding through bottom sheet --- .../mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt | 4 +++- .../mullvadvpn/viewmodel/SelectLocationViewModelTest.kt | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt index 1759055bda9e..9772fb636286 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt @@ -19,6 +19,7 @@ import net.mullvad.mullvadvpn.model.Constraint import net.mullvad.mullvadvpn.model.Ownership import net.mullvad.mullvadvpn.relaylist.Provider import net.mullvad.mullvadvpn.relaylist.RelayItem +import net.mullvad.mullvadvpn.relaylist.descendants import net.mullvad.mullvadvpn.relaylist.filterOnOwnershipAndProvider import net.mullvad.mullvadvpn.relaylist.filterOnSearchTerm import net.mullvad.mullvadvpn.relaylist.toLocationConstraint @@ -137,7 +138,8 @@ class SelectLocationViewModel( fun addLocationToList(item: RelayItem, customList: RelayItem.CustomList) { viewModelScope.launch { - val newLocations = (customList.locations + item).map { it.code } + val newLocations = + (customList.locations + item).filter { it !in item.descendants() }.map { it.code } val result = customListActionUseCase.performAction( CustomListAction.UpdateLocations(customList.id, newLocations) diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt index 6522892ec2f6..5d0ab5f604e5 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt @@ -29,6 +29,7 @@ import net.mullvad.mullvadvpn.model.Providers import net.mullvad.mullvadvpn.relaylist.Provider import net.mullvad.mullvadvpn.relaylist.RelayItem import net.mullvad.mullvadvpn.relaylist.RelayList +import net.mullvad.mullvadvpn.relaylist.descendants import net.mullvad.mullvadvpn.relaylist.filterOnSearchTerm import net.mullvad.mullvadvpn.relaylist.toLocationConstraint import net.mullvad.mullvadvpn.ui.serviceconnection.ConnectionProxy @@ -264,7 +265,10 @@ class SelectLocationViewModelTest { fun `after adding a location to a list should emit location added side effect`() = runTest { // Arrange val expectedResult: CustomListResult.LocationsChanged = mockk() - val location: RelayItem = mockk { every { code } returns "code" } + val location: RelayItem = mockk { + every { code } returns "code" + every { descendants() } returns emptyList() + } val customList: RelayItem.CustomList = mockk { every { id } returns "1" every { locations } returns emptyList()