Expand selected server country on iOS (#853)

Add isExpanded binding to DisclosureGroup.
This commit is contained in:
Davide 2024-11-11 20:21:02 +01:00 committed by GitHub
parent 4ac524eb01
commit 83d77fafbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 8 deletions

View File

@ -75,7 +75,7 @@ extension VPNProviderServerView {
apis: apis, apis: apis,
moduleId: moduleId, moduleId: moduleId,
providerId: providerId, providerId: providerId,
selectedServerId: selectedEntity?.server.id, selectedServer: selectedEntity?.server,
initialFilters: { initialFilters: {
guard let selectedEntity, filtersWithSelection else { guard let selectedEntity, filtersWithSelection else {
return nil return nil
@ -117,7 +117,7 @@ extension VPNProviderServerView {
let providerId: ProviderID let providerId: ProviderID
let selectedServerId: String? let selectedServer: VPNServer?
let initialFilters: VPNFilters? let initialFilters: VPNFilters?
@ -144,7 +144,7 @@ extension VPNProviderServerView {
debugChanges() debugChanges()
return ServersSubview( return ServersSubview(
servers: filteredServers, servers: filteredServers,
selectedServerId: selectedServerId, selectedServer: selectedServer,
isFiltering: isFiltering, isFiltering: isFiltering,
filtersViewModel: filtersViewModel, filtersViewModel: filtersViewModel,
favoritesManager: favoritesManager, favoritesManager: favoritesManager,

View File

@ -95,7 +95,7 @@ extension VPNProviderServerView {
struct ServersSubview: View { struct ServersSubview: View {
let servers: [VPNServer] let servers: [VPNServer]
let selectedServerId: String? let selectedServer: VPNServer?
let isFiltering: Bool let isFiltering: Bool
@ -109,6 +109,9 @@ extension VPNProviderServerView {
let onSelect: (VPNServer) -> Void let onSelect: (VPNServer) -> Void
@State
private var expandedCodes: Set<String> = []
var body: some View { var body: some View {
debugChanges() debugChanges()
return listView return listView
@ -138,6 +141,11 @@ private extension VPNProviderServerView.ServersSubview {
.themeSection( .themeSection(
header: filtersViewModel.filters.categoryName ?? Strings.Providers.Vpn.Category.any header: filtersViewModel.filters.categoryName ?? Strings.Providers.Vpn.Category.any
) )
.onLoad {
if let selectedServer {
expandedCodes.insert(selectedServer.provider.countryCode)
}
}
} }
} }
@ -153,6 +161,18 @@ private extension VPNProviderServerView.ServersSubview {
.map(\.code) .map(\.code)
} }
func isExpandedCountry(_ code: String) -> Binding<Bool> {
Binding {
expandedCodes.contains(code)
} set: {
if $0 {
expandedCodes.insert(code)
} else {
expandedCodes.remove(code)
}
}
}
func countryServers(for code: String) -> [VPNServer]? { func countryServers(for code: String) -> [VPNServer]? {
servers servers
.filter { .filter {
@ -164,7 +184,7 @@ private extension VPNProviderServerView.ServersSubview {
func countryView(for code: String) -> some View { func countryView(for code: String) -> some View {
countryServers(for: code) countryServers(for: code)
.map { servers in .map { servers in
DisclosureGroup { DisclosureGroup(isExpanded: isExpandedCountry(code)) {
ForEach(servers, id: \.id, content: serverView) ForEach(servers, id: \.id, content: serverView)
} label: { } label: {
ThemeCountryText(code) ThemeCountryText(code)
@ -178,7 +198,7 @@ private extension VPNProviderServerView.ServersSubview {
} label: { } label: {
HStack { HStack {
ThemeImage(.marked) ThemeImage(.marked)
.opaque(server.id == selectedServerId) .opaque(server.id == selectedServer?.id)
VStack(alignment: .leading) { VStack(alignment: .leading) {
if let area = server.provider.area { if let area = server.provider.area {
Text(area) Text(area)

View File

@ -46,7 +46,7 @@ extension VPNProviderServerView {
struct ServersSubview: View { struct ServersSubview: View {
let servers: [VPNServer] let servers: [VPNServer]
let selectedServerId: String? let selectedServer: VPNServer?
let isFiltering: Bool let isFiltering: Bool
@ -68,7 +68,7 @@ extension VPNProviderServerView {
return Table(servers) { return Table(servers) {
TableColumn("") { server in TableColumn("") { server in
ThemeImage(.marked) ThemeImage(.marked)
.opaque(server.id == selectedServerId) .opaque(server.id == selectedServer?.id)
} }
.width(10.0) .width(10.0)