Skip to content

Commit 42016c9

Browse files
committed
Improve the save function
1 parent 9825962 commit 42016c9

File tree

1 file changed

+52
-57
lines changed

1 file changed

+52
-57
lines changed

android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/CustomListLocationsViewModel.kt

+52-57
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package net.mullvad.mullvadvpn.viewmodel
33
import androidx.lifecycle.SavedStateHandle
44
import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
6+
import arrow.core.getOrElse
7+
import arrow.core.raise.either
68
import com.ramcosta.composedestinations.generated.destinations.CustomListLocationsDestination
79
import kotlinx.coroutines.flow.MutableSharedFlow
810
import kotlinx.coroutines.flow.MutableStateFlow
@@ -17,6 +19,7 @@ import kotlinx.coroutines.flow.update
1719
import kotlinx.coroutines.launch
1820
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
1921
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
22+
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
2023
import net.mullvad.mullvadvpn.compose.state.CustomListLocationsUiState
2124
import net.mullvad.mullvadvpn.compose.state.RelayLocationListItem
2225
import net.mullvad.mullvadvpn.lib.model.RelayItem
@@ -87,7 +90,7 @@ class CustomListLocationsViewModel(
8790
viewModelScope.launch { fetchInitialSelectedLocations() }
8891
}
8992

90-
fun searchRelayListLocations() =
93+
private fun searchRelayListLocations() =
9194
combine(
9295
_searchTerm,
9396
relayListRepository.relayList,
@@ -109,63 +112,22 @@ class CustomListLocationsViewModel(
109112
viewModelScope.launch {
110113
_selectedLocations.value?.let { selectedLocations ->
111114
val locationsToSave = selectedLocations.calculateLocationsToSave()
112-
customListActionUseCase(
113-
CustomListAction.UpdateLocations(
114-
navArgs.customListId,
115-
locationsToSave.map { it.id }
116-
)
117-
)
118-
.fold(
119-
{
120-
_uiSideEffect.tryEmit(
121-
CustomListLocationsSideEffect.ReturnWithResultData(
122-
CustomListActionResultData.GenericError
123-
)
124-
)
125-
},
126-
{ result ->
127-
val resultData =
128-
if (navArgs.newList) {
129-
CustomListActionResultData.Success.CreatedWithLocations(
130-
customListName = result.name,
131-
locationNames = locationsToSave.map { it.name },
132-
undo = CustomListAction.Delete(id = result.id)
115+
val result =
116+
either {
117+
val success =
118+
customListActionUseCase(
119+
CustomListAction.UpdateLocations(
120+
navArgs.customListId,
121+
locationsToSave.map { it.id }
122+
)
133123
)
134-
} else {
135-
when {
136-
result.addedLocations.size == 1 &&
137-
result.removedLocations.isEmpty() ->
138-
CustomListActionResultData.Success.LocationAdded(
139-
customListName = result.name,
140-
relayListRepository
141-
.find(result.removedLocations.first())!!
142-
.name,
143-
undo = result.undo
144-
)
145-
result.removedLocations.size == 1 &&
146-
result.addedLocations.isEmpty() ->
147-
CustomListActionResultData.Success.LocationRemoved(
148-
customListName = result.name,
149-
locationName =
150-
relayListRepository
151-
.find(result.removedLocations.first())!!
152-
.name,
153-
undo = result.undo
154-
)
155-
else ->
156-
CustomListActionResultData.Success.LocationChanged(
157-
customListName = result.name,
158-
undo = result.undo
159-
)
160-
}
161-
}
162-
_uiSideEffect.tryEmit(
163-
CustomListLocationsSideEffect.ReturnWithResultData(
164-
result = resultData
165-
)
166-
)
124+
.bind()
125+
calculateResultData(success, locationsToSave)
167126
}
168-
)
127+
.getOrElse { CustomListActionResultData.GenericError }
128+
_uiSideEffect.tryEmit(
129+
CustomListLocationsSideEffect.ReturnWithResultData(result = result)
130+
)
169131
}
170132
}
171133
}
@@ -279,7 +241,7 @@ class CustomListLocationsViewModel(
279241
isExpanded: (RelayItemId) -> Boolean,
280242
depth: Int = 0,
281243
): List<RelayLocationListItem> = flatMap { relayItem ->
282-
buildList<RelayLocationListItem> {
244+
buildList {
283245
val expanded = isExpanded(relayItem.id)
284246
add(
285247
RelayLocationListItem(
@@ -315,6 +277,39 @@ class CustomListLocationsViewModel(
315277
}
316278
}
317279

280+
private fun calculateResultData(
281+
success: LocationsChanged,
282+
locationsToSave: List<RelayItem.Location>
283+
) =
284+
if (navArgs.newList) {
285+
CustomListActionResultData.Success.CreatedWithLocations(
286+
customListName = success.name,
287+
locationNames = locationsToSave.map { it.name },
288+
undo = CustomListAction.Delete(id = success.id)
289+
)
290+
} else {
291+
when {
292+
success.addedLocations.size == 1 && success.removedLocations.isEmpty() ->
293+
CustomListActionResultData.Success.LocationAdded(
294+
customListName = success.name,
295+
relayListRepository.find(success.removedLocations.first())!!.name,
296+
undo = success.undo
297+
)
298+
success.removedLocations.size == 1 && success.addedLocations.isEmpty() ->
299+
CustomListActionResultData.Success.LocationRemoved(
300+
customListName = success.name,
301+
locationName =
302+
relayListRepository.find(success.removedLocations.first())!!.name,
303+
undo = success.undo
304+
)
305+
else ->
306+
CustomListActionResultData.Success.LocationChanged(
307+
customListName = success.name,
308+
undo = success.undo
309+
)
310+
}
311+
}
312+
318313
companion object {
319314
private const val EMPTY_SEARCH_TERM = ""
320315
}

0 commit comments

Comments
 (0)