@@ -16,7 +16,7 @@ import net.mullvad.mullvadvpn.compose.state.toConstraintProviders
16
16
import net.mullvad.mullvadvpn.compose.state.toOwnershipConstraint
17
17
import net.mullvad.mullvadvpn.compose.state.toSelectedProviders
18
18
import net.mullvad.mullvadvpn.lib.model.Ownership
19
- import net.mullvad.mullvadvpn.lib.model.Provider
19
+ import net.mullvad.mullvadvpn.lib.model.ProviderId
20
20
import net.mullvad.mullvadvpn.repository.RelayListFilterRepository
21
21
import net.mullvad.mullvadvpn.usecase.AvailableProvidersUseCase
22
22
@@ -28,15 +28,17 @@ class FilterViewModel(
28
28
val uiSideEffect = _uiSideEffect .receiveAsFlow()
29
29
30
30
private val selectedOwnership = MutableStateFlow <Ownership ?>(null )
31
- private val selectedProviders = MutableStateFlow <List <Provider >>(emptyList())
31
+ private val selectedProviders = MutableStateFlow <List <ProviderId >>(emptyList())
32
32
33
33
init {
34
34
viewModelScope.launch {
35
35
selectedProviders.value =
36
36
combine(availableProvidersUseCase(), relayListFilterRepository.selectedProviders) {
37
37
allProviders,
38
38
selectedConstraintProviders ->
39
- selectedConstraintProviders.toSelectedProviders(allProviders)
39
+ selectedConstraintProviders.toSelectedProviders(allProviders).map {
40
+ it.providerId
41
+ }
40
42
}
41
43
.first()
42
44
@@ -51,26 +53,36 @@ class FilterViewModel(
51
53
allProviders,
52
54
selectedProviders ->
53
55
RelayFilterUiState (
56
+ filteredOwnershipByProviders =
57
+ if (selectedProviders.isEmpty()) {
58
+ Ownership .entries
59
+ } else {
60
+ Ownership .entries.filter { ownership ->
61
+ selectedProviders.any { providerId ->
62
+ allProviders.any {
63
+ it.providerId == providerId && ownership in it.ownership
64
+ }
65
+ }
66
+ }
67
+ },
54
68
selectedOwnership = selectedOwnership,
55
- allProviders = allProviders,
69
+ filteredProvidersByOwnership =
70
+ if (selectedOwnership != null )
71
+ allProviders
72
+ .filter { provider -> selectedOwnership in provider.ownership }
73
+ .map { it.providerId }
74
+ else allProviders.map { it.providerId },
75
+ allProviders = allProviders.map { it.providerId },
56
76
selectedProviders = selectedProviders,
57
77
)
58
78
}
59
- .stateIn(
60
- viewModelScope,
61
- SharingStarted .WhileSubscribed (),
62
- RelayFilterUiState (
63
- allProviders = emptyList(),
64
- selectedOwnership = null ,
65
- selectedProviders = emptyList(),
66
- ),
67
- )
79
+ .stateIn(viewModelScope, SharingStarted .WhileSubscribed (), RelayFilterUiState ())
68
80
69
81
fun setSelectedOwnership (ownership : Ownership ? ) {
70
82
selectedOwnership.value = ownership
71
83
}
72
84
73
- fun setSelectedProvider (checked : Boolean , provider : Provider ) {
85
+ fun setSelectedProvider (checked : Boolean , provider : ProviderId ) {
74
86
selectedProviders.value =
75
87
if (checked) {
76
88
selectedProviders.value + provider
@@ -83,7 +95,7 @@ class FilterViewModel(
83
95
viewModelScope.launch {
84
96
selectedProviders.value =
85
97
if (isChecked) {
86
- availableProvidersUseCase().first()
98
+ availableProvidersUseCase().first().map { it.providerId }
87
99
} else {
88
100
emptyList()
89
101
}
@@ -92,8 +104,11 @@ class FilterViewModel(
92
104
93
105
fun onApplyButtonClicked () {
94
106
val newSelectedOwnership = selectedOwnership.value.toOwnershipConstraint()
107
+ // TODO should be all providers?!
95
108
val newSelectedProviders =
96
- selectedProviders.value.toConstraintProviders(uiState.value.allProviders)
109
+ selectedProviders.value.toConstraintProviders(
110
+ uiState.value.filteredProvidersByOwnership
111
+ )
97
112
98
113
viewModelScope.launch {
99
114
relayListFilterRepository.updateSelectedOwnershipAndProviderFilter(
0 commit comments