Skip to content

Commit 7224dfe

Browse files
RawaPururun
authored andcommitted
Add support for more detailed location changed messages
1 parent 2368bb8 commit 7224dfe

File tree

6 files changed

+103
-7
lines changed

6 files changed

+103
-7
lines changed

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/communication/CustomListSuccess.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package net.mullvad.mullvadvpn.compose.communication
22

33
import android.os.Parcelable
4+
import kotlinx.parcelize.IgnoredOnParcel
45
import kotlinx.parcelize.Parcelize
56
import net.mullvad.mullvadvpn.lib.model.CustomListId
67
import net.mullvad.mullvadvpn.lib.model.CustomListName
8+
import net.mullvad.mullvadvpn.lib.model.GeoLocationId
79

810
sealed interface CustomListSuccess : Parcelable {
911
val undo: CustomListAction
@@ -31,6 +33,14 @@ data class Renamed(override val undo: CustomListAction.Rename) : CustomListSucce
3133

3234
@Parcelize
3335
data class LocationsChanged(
36+
val id: CustomListId,
3437
val name: CustomListName,
38+
val locations: List<GeoLocationId>,
39+
val oldLocations: List<GeoLocationId>,
40+
) : CustomListSuccess {
3541
override val undo: CustomListAction.UpdateLocations
36-
) : CustomListSuccess
42+
get() = CustomListAction.UpdateLocations(id, oldLocations)
43+
44+
@IgnoredOnParcel val addedLocations = locations - oldLocations
45+
@IgnoredOnParcel val removedLocations = oldLocations - locations
46+
}

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SelectLocationScreen.kt

+16-1
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,22 @@ private fun CustomListSuccess.message(context: Context): String =
849849
} ?: context.getString(R.string.locations_were_changed_for, name)
850850
is Deleted -> context.getString(R.string.delete_custom_list_message, name)
851851
is Renamed -> context.getString(R.string.name_was_changed_to, name)
852-
is LocationsChanged -> context.getString(R.string.locations_were_changed_for, name)
852+
is LocationsChanged ->
853+
when {
854+
addedLocations.isNotEmpty() && removedLocations.isEmpty() ->
855+
context.getString(
856+
R.string.location_was_added_to_list,
857+
addedLocations.first(),
858+
name
859+
)
860+
removedLocations.isNotEmpty() && addedLocations.isEmpty() ->
861+
context.getString(
862+
R.string.location_was_removed_from_list,
863+
removedLocations.first(),
864+
name
865+
)
866+
else -> context.getString(R.string.locations_were_changed_for, name)
867+
}
853868
}
854869

855870
@Composable

android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/customlists/CustomListActionUseCase.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ class CustomListActionUseCase(
107107
.mapLeft(UpdateLocationsError::UpdateLocations)
108108
.bind()
109109
LocationsChanged(
110+
id = action.id,
110111
name = customList.name,
111-
undo = action.not(locations = customList.locations)
112+
locations = action.locations,
113+
oldLocations = customList.locations,
112114
)
113115
}
114116
}

android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/CustomListActionUseCaseTest.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,13 @@ class CustomListActionUseCaseTest {
175175
val customList = CustomList(id = customListId, name = name, locations = oldLocations)
176176
val action = CustomListAction.UpdateLocations(id = customListId, locations = newLocations)
177177
val expectedResult =
178-
LocationsChanged(name = name, undo = action.not(locations = oldLocations)).right()
178+
LocationsChanged(
179+
id = customListId,
180+
name = name,
181+
locations = newLocations,
182+
oldLocations = oldLocations,
183+
)
184+
.right()
179185
coEvery { mockCustomListsRepository.getCustomListById(customListId) } returns
180186
customList.right()
181187

android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt

+65-3
Original file line numberDiff line numberDiff line change
@@ -252,20 +252,37 @@ class SelectLocationViewModelTest {
252252
@Test
253253
fun `after adding a location to a list should emit location added side effect`() = runTest {
254254
// Arrange
255-
val expectedResult: LocationsChanged = mockk()
255+
val customListId = CustomListId("1")
256+
val addedLocationsId = GeoLocationId.Country("se")
257+
val customListName = CustomListName.fromString("custom")
256258
val location: RelayItem.Location.Country = mockk {
257259
every { id } returns GeoLocationId.Country("se")
260+
every { name } returns "Sweden"
258261
every { descendants() } returns emptyList()
259262
}
260263
val customList =
261264
RelayItem.CustomList(
262265
id = CustomListId("1"),
263-
customListName = CustomListName.fromString("custom"),
266+
customListName = customListName,
264267
locations = emptyList(),
265268
expanded = false
266269
)
270+
val expectedResult =
271+
LocationsChanged(
272+
id = customListId,
273+
name = customListName,
274+
locations = listOf(addedLocationsId),
275+
oldLocations = emptyList(),
276+
)
277+
267278
coEvery { mockCustomListActionUseCase(any<CustomListAction.UpdateLocations>()) } returns
268-
expectedResult.right()
279+
LocationsChanged(
280+
id = customListId,
281+
name = customListName,
282+
locations = listOf(addedLocationsId),
283+
oldLocations = emptyList()
284+
)
285+
.right()
269286

270287
// Act, Assert
271288
viewModel.uiSideEffect.test {
@@ -276,6 +293,51 @@ class SelectLocationViewModelTest {
276293
}
277294
}
278295

296+
@Test
297+
fun `after removing a location from a list should emit location removed side effect`() =
298+
runTest {
299+
// Arrange
300+
val locationName = "Sweden"
301+
val customListId = CustomListId("1")
302+
val removedLocationsId = GeoLocationId.Country("se")
303+
val customListName = CustomListName.fromString("custom")
304+
val location: RelayItem.Location.Country = mockk {
305+
every { id } returns removedLocationsId
306+
every { name } returns locationName
307+
every { descendants() } returns emptyList()
308+
}
309+
val customList =
310+
RelayItem.CustomList(
311+
id = customListId,
312+
customListName = customListName,
313+
locations = emptyList(),
314+
expanded = false
315+
)
316+
val expectedResult =
317+
LocationsChanged(
318+
id = customListId,
319+
name = customListName,
320+
locations = emptyList(),
321+
oldLocations = listOf(removedLocationsId),
322+
)
323+
coEvery { mockCustomListActionUseCase(any<CustomListAction.UpdateLocations>()) } returns
324+
LocationsChanged(
325+
id = customListId,
326+
name = customListName,
327+
locations = emptyList(),
328+
oldLocations = listOf(removedLocationsId),
329+
)
330+
.right()
331+
332+
// Act, Assert
333+
viewModel.uiSideEffect.test {
334+
viewModel.removeLocationFromList(item = location, customList = customList)
335+
val sideEffect = awaitItem()
336+
assertIs<SelectLocationSideEffect.LocationRemovedFromCustomList>(sideEffect)
337+
assertEquals(expectedResult, sideEffect.result)
338+
}
339+
}
340+
279341
companion object {
280342
private const val RELAY_LIST_EXTENSIONS =
281343
"net.mullvad.mullvadvpn.relaylist.RelayListExtensionsKt"

android/lib/resource/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,5 @@
383383
<string name="delete_method_question">Delete method?</string>
384384
<string name="failed_to_set_current_test_error">Failed to set to current - API not reachable</string>
385385
<string name="failed_to_set_current_unknown_error">Failed to set to current - Unknown reason</string>
386+
<string name="location_was_removed_from_list">%s was removed from \"%s\"</string>
386387
</resources>

0 commit comments

Comments
 (0)