@@ -3,6 +3,8 @@ package net.mullvad.mullvadvpn.viewmodel
3
3
import androidx.lifecycle.SavedStateHandle
4
4
import androidx.lifecycle.ViewModel
5
5
import androidx.lifecycle.viewModelScope
6
+ import arrow.core.getOrElse
7
+ import arrow.core.raise.either
6
8
import com.ramcosta.composedestinations.generated.destinations.CustomListLocationsDestination
7
9
import kotlinx.coroutines.flow.MutableSharedFlow
8
10
import kotlinx.coroutines.flow.MutableStateFlow
@@ -17,6 +19,7 @@ import kotlinx.coroutines.flow.update
17
19
import kotlinx.coroutines.launch
18
20
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
19
21
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
22
+ import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
20
23
import net.mullvad.mullvadvpn.compose.state.CustomListLocationsUiState
21
24
import net.mullvad.mullvadvpn.compose.state.RelayLocationListItem
22
25
import net.mullvad.mullvadvpn.lib.model.RelayItem
@@ -87,7 +90,7 @@ class CustomListLocationsViewModel(
87
90
viewModelScope.launch { fetchInitialSelectedLocations() }
88
91
}
89
92
90
- fun searchRelayListLocations () =
93
+ private fun searchRelayListLocations () =
91
94
combine(
92
95
_searchTerm ,
93
96
relayListRepository.relayList,
@@ -109,63 +112,22 @@ class CustomListLocationsViewModel(
109
112
viewModelScope.launch {
110
113
_selectedLocations .value?.let { selectedLocations ->
111
114
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
+ )
133
123
)
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)
167
126
}
168
- )
127
+ .getOrElse { CustomListActionResultData .GenericError }
128
+ _uiSideEffect .tryEmit(
129
+ CustomListLocationsSideEffect .ReturnWithResultData (result = result)
130
+ )
169
131
}
170
132
}
171
133
}
@@ -279,7 +241,7 @@ class CustomListLocationsViewModel(
279
241
isExpanded : (RelayItemId ) -> Boolean ,
280
242
depth : Int = 0,
281
243
): List <RelayLocationListItem > = flatMap { relayItem ->
282
- buildList< RelayLocationListItem > {
244
+ buildList {
283
245
val expanded = isExpanded(relayItem.id)
284
246
add(
285
247
RelayLocationListItem (
@@ -315,6 +277,39 @@ class CustomListLocationsViewModel(
315
277
}
316
278
}
317
279
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
+
318
313
companion object {
319
314
private const val EMPTY_SEARCH_TERM = " "
320
315
}
0 commit comments