Improve provider servers responsiveness on iOS (#927)

Pre-compute servers by country code.

Fixes #898
This commit is contained in:
Davide 2024-11-25 10:18:10 +01:00 committed by GitHub
parent 9c93158a3d
commit 7e49493d75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -112,6 +112,9 @@ extension VPNProviderServerView {
let onSelect: (VPNServer) -> Void
@State
private var serversByCountryCode: [String: [VPNServer]] = [:]
@State
private var expandedCodes: Set<String> = []
@ -119,6 +122,7 @@ extension VPNProviderServerView {
debugChanges()
return listView
.themeAnimation(on: isFiltering, category: .providers)
.onChange(of: servers, perform: computeServersByCountry)
}
}
}
@ -176,16 +180,8 @@ private extension VPNProviderServerView.ServersSubview {
}
}
func countryServers(for code: String) -> [VPNServer]? {
servers
.filter {
$0.provider.countryCode == code
}
.nilIfEmpty
}
func countryView(for code: String) -> some View {
countryServers(for: code)
serversByCountryCode[code]
.map { servers in
DisclosureGroup(isExpanded: isExpandedCountry(code)) {
ForEach(servers, id: \.id, content: serverView)
@ -219,6 +215,17 @@ private extension VPNProviderServerView.ServersSubview {
}
}
}
func computeServersByCountry(_ servers: [VPNServer]) {
var map: [String: [VPNServer]] = [:]
servers.forEach {
let code = $0.provider.countryCode
var list = map[code] ?? []
list.append($0)
map[code] = list
}
serversByCountryCode = map
}
}
// MARK: - Preview