Filter countries by selected category (#767)

Countries are filtered through the latest servers list. If a country is
chosen, the countries picker only lists the currently selected country,
because there are no servers from other countries.

Update the library to prefetch the available countries per category.
This commit is contained in:
Davide 2024-10-28 20:53:35 +01:00 committed by GitHub
parent 7ede841346
commit f8773cacf1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 11 deletions

View File

@ -41,7 +41,7 @@
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
"location" : "git@github.com:passepartoutvpn/passepartoutkit-source", "location" : "git@github.com:passepartoutvpn/passepartoutkit-source",
"state" : { "state" : {
"revision" : "db0f5258fdbd5192dd2b87dedc46494494bc7ffb" "revision" : "31aff403169c7cebe91a07fb8d225ab844a9a9ff"
} }
}, },
{ {

View File

@ -28,7 +28,7 @@ let package = Package(
], ],
dependencies: [ dependencies: [
// .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", from: "0.9.0"), // .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", from: "0.9.0"),
.package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", revision: "db0f5258fdbd5192dd2b87dedc46494494bc7ffb"), .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", revision: "31aff403169c7cebe91a07fb8d225ab844a9a9ff"),
// .package(path: "../../../passepartoutkit-source"), // .package(path: "../../../passepartoutkit-source"),
.package(url: "git@github.com:passepartoutvpn/passepartoutkit-source-openvpn-openssl", from: "0.9.1"), .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source-openvpn-openssl", from: "0.9.1"),
// .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source-openvpn-openssl", revision: "031863a1cd683962a7dfe68e20b91fa820a1ecce"), // .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source-openvpn-openssl", revision: "031863a1cd683962a7dfe68e20b91fa820a1ecce"),

View File

@ -52,11 +52,18 @@ final class CDVPNProviderServerRepositoryV3: VPNProviderServerRepository {
"countryCode" "countryCode"
] ]
let serversResults = try serversRequest.execute() let serversResults = try serversRequest.execute()
let categoryNames = serversResults.compactMap {
$0.object(forKey: "categoryName") as? String var countriesByCategoryName: [String: Set<String>] = [:]
} var countryCodes: Set<String> = []
let countryCodes = serversResults.compactMap { serversResults.forEach {
$0.object(forKey: "countryCode") as? String guard let categoryName = $0.object(forKey: "categoryName") as? String,
let countryCode = $0.object(forKey: "countryCode") as? String else {
return
}
var codes: Set<String> = countriesByCategoryName[categoryName] ?? []
codes.insert(countryCode)
countriesByCategoryName[categoryName] = codes
countryCodes.insert(countryCode)
} }
let presetsRequest = CDVPNPresetV3.fetchRequest() let presetsRequest = CDVPNPresetV3.fetchRequest()
@ -67,7 +74,7 @@ final class CDVPNProviderServerRepositoryV3: VPNProviderServerRepository {
let presetsResults = try presetsRequest.execute() let presetsResults = try presetsRequest.execute()
return VPNFilterOptions( return VPNFilterOptions(
categoryNames: Set(categoryNames), countriesByCategoryName: countriesByCategoryName,
countryCodes: Set(countryCodes), countryCodes: Set(countryCodes),
presets: Set(try presetsResults.compactMap { presets: Set(try presetsResults.compactMap {
try mapper.preset(from: $0) try mapper.preset(from: $0)

View File

@ -63,7 +63,7 @@ extension VPNFiltersView {
func load(options: VPNFilterOptions, initialFilters: VPNFilters?) { func load(options: VPNFilterOptions, initialFilters: VPNFilters?) {
self.options = options self.options = options
setCategories(withNames: options.categoryNames) setCategories(withNames: Set(options.countriesByCategoryName.keys))
setCountries(withCodes: options.countryCodes) setCountries(withCodes: options.countryCodes)
setPresets(with: options.presets) setPresets(with: options.presets)
@ -74,8 +74,13 @@ extension VPNFiltersView {
func update(with servers: [VPNServer]) { func update(with servers: [VPNServer]) {
// only non-empty countries // only countries that have servers in this category
let knownCountryCodes = Set(servers.map(\.provider.countryCode)) let knownCountryCodes: Set<String>
if let categoryName = filters.categoryName {
knownCountryCodes = options.countriesByCategoryName[categoryName] ?? []
} else {
knownCountryCodes = options.countryCodes
}
// only presets known in filtered servers // only presets known in filtered servers
var knownPresets = options.presets var knownPresets = options.presets