Skip to content

Commit

Permalink
Fix unselectable presets (#1004)
Browse files Browse the repository at this point in the history
Revert #996 and do something midway:

- When set, use preset filter as selection (disambiguate compatible
presets)
- Set initial preset filter with current selected server preset

Server selection still picks a random preset if:

- Preset filter is "Any"
- More than one preset is compatible
  • Loading branch information
keeshux authored Dec 12, 2024
1 parent 4fb457a commit 2075f10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Library/Sources/AppUIMain/Views/VPN/VPNFiltersView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ private extension VPNFiltersView {

var presetPicker: some View {
Picker(Strings.Views.Vpn.preset, selection: $model.filters.presetId) {
Text(Strings.Global.Nouns.any)
.tag(nil as String?)
ForEach(model.presets, id: \.presetId) {
Text($0.description)
.tag($0.presetId as String?)
Expand Down
31 changes: 16 additions & 15 deletions Library/Sources/AppUIMain/Views/VPN/VPNProviderServerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,31 @@ private extension VPNProviderServerView {
return servers
}

var initialFilters: VPNFilters {
var initialFilters: VPNFilters? {
guard let selectedEntity else {
return nil
}
var filters = VPNFilters()

// force initial preset filter
filters.presetId = vpnManager.options.presets.first?.presetId

if let selectedEntity {
filters.presetId = selectedEntity.preset.presetId
if filtersWithSelection {
filters.categoryName = selectedEntity.server.provider.categoryName
filters.presetId = selectedEntity.preset.presetId
if filtersWithSelection {
filters.categoryName = selectedEntity.server.provider.categoryName
#if os(macOS)
filters.countryCode = selectedEntity.server.provider.countryCode
filters.countryCode = selectedEntity.server.provider.countryCode
#endif
}
}
return filters
}

func compatiblePreset(with server: VPNServer) -> VPNPreset<Configuration>? {
func compatiblePresets(with server: VPNServer) -> [VPNPreset<Configuration>] {
vpnManager
.presets
.filter {
$0.presetId == filtersViewModel.filters.presetId
if let selectedId = filtersViewModel.filters.presetId {
return $0.presetId == selectedId
}
return true
}
.first {
.filter {
if let supportedIds = server.provider.supportedPresetIds {
return supportedIds.contains($0.presetId)
}
Expand Down Expand Up @@ -219,7 +219,8 @@ private extension VPNProviderServerView {
}

func onSelectServer(_ server: VPNServer) {
guard let preset = compatiblePreset(with: server) else {
let presets = compatiblePresets(with: server)
guard let preset = presets.first else {
pp_log(.app, .error, "Unable to find a compatible preset. Supported IDs: \(server.provider.supportedPresetIds ?? [])")
assertionFailure("No compatible presets for server \(server.serverId) (provider=\(vpnManager.providerId), configuration=\(Configuration.configurationIdentifier), supported=\(server.provider.supportedPresetIds ?? []))")
return
Expand Down

0 comments on commit 2075f10

Please sign in to comment.