1
1
package net.mullvad.mullvadvpn.repository
2
2
3
3
import kotlinx.coroutines.flow.first
4
+ import kotlinx.coroutines.flow.firstOrNull
4
5
import net.mullvad.mullvadvpn.lib.ipc.Event
5
6
import net.mullvad.mullvadvpn.lib.ipc.MessageHandler
6
7
import net.mullvad.mullvadvpn.lib.ipc.Request
@@ -9,8 +10,13 @@ import net.mullvad.mullvadvpn.model.CreateCustomListResult
9
10
import net.mullvad.mullvadvpn.model.CustomList
10
11
import net.mullvad.mullvadvpn.model.CustomListsError
11
12
import net.mullvad.mullvadvpn.model.UpdateCustomListResult
13
+ import net.mullvad.mullvadvpn.relaylist.RelayItem
14
+ import net.mullvad.mullvadvpn.relaylist.toGeographicLocationConstraints
12
15
13
- class CustomListsRepository (private val messageHandler : MessageHandler ) {
16
+ class CustomListsRepository (
17
+ private val messageHandler : MessageHandler ,
18
+ private val settingsRepository : SettingsRepository
19
+ ) {
14
20
suspend fun createCustomList (name : String ): CreateCustomListResult {
15
21
val result = messageHandler.trySendRequest(Request .CreateCustomList (name))
16
22
@@ -34,4 +40,23 @@ class CustomListsRepository(private val messageHandler: MessageHandler) {
34
40
UpdateCustomListResult .Error (CustomListsError .OtherError )
35
41
}
36
42
}
43
+
44
+ suspend fun updateCustomListLocations (
45
+ id : String ,
46
+ locations : List <RelayItem >
47
+ ): UpdateCustomListResult {
48
+ return getCustomListById(id)?.let {
49
+ updateCustomList(it.copy(locations = locations.toGeographicLocationConstraints()))
50
+ } ? : UpdateCustomListResult .Error (CustomListsError .OtherError )
51
+ }
52
+
53
+ suspend fun updateCustomListName (id : String , name : String ): UpdateCustomListResult {
54
+ return getCustomListById(id)?.let { updateCustomList(it.copy(name = name)) }
55
+ ? : UpdateCustomListResult .Error (CustomListsError .OtherError )
56
+ }
57
+
58
+ private suspend fun getCustomListById (id : String ): CustomList ? =
59
+ settingsRepository.settingsUpdates.firstOrNull()?.customLists?.customLists?.find {
60
+ it.id == id
61
+ }
37
62
}
0 commit comments