Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize redraws in provider servers views #760

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"kind" : "remoteSourceControl",
"location" : "[email protected]:passepartoutvpn/passepartoutkit-source",
"state" : {
"revision" : "3934b7a4e64624d499f0d52d9053560554bd4be8"
"revision" : "79ff98a69c87cc90ef213e00ab02c9d90d63baaf"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Passepartout/Library/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let package = Package(
],
dependencies: [
// .package(url: "[email protected]:passepartoutvpn/passepartoutkit-source", from: "0.9.0"),
.package(url: "[email protected]:passepartoutvpn/passepartoutkit-source", revision: "3934b7a4e64624d499f0d52d9053560554bd4be8"),
.package(url: "[email protected]:passepartoutvpn/passepartoutkit-source", revision: "79ff98a69c87cc90ef213e00ab02c9d90d63baaf"),
// .package(path: "../../../passepartoutkit-source"),
.package(url: "[email protected]:passepartoutvpn/passepartoutkit-source-openvpn-openssl", from: "0.9.1"),
// .package(url: "[email protected]:passepartoutvpn/passepartoutkit-source-openvpn-openssl", revision: "031863a1cd683962a7dfe68e20b91fa820a1ecce"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// ProviderFavoritesManager.swift
// Passepartout
//
// Created by Davide De Rosa on 10/26/24.
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//

import CommonLibrary
import Foundation

@MainActor
final class ProviderFavoritesManager: ObservableObject {
private let defaults: UserDefaults

private var allFavorites: ProviderFavoriteServers

var moduleId: UUID {
didSet {
guard let rawValue = defaults.string(forKey: AppPreference.moduleFavoriteServers.key) else {
allFavorites = ProviderFavoriteServers()
return
}
allFavorites = ProviderFavoriteServers(rawValue: rawValue) ?? ProviderFavoriteServers()
}
}

var serverIds: Set<String> {
get {
allFavorites.servers(forModuleWithID: moduleId)
}
set {
objectWillChange.send()
allFavorites.setServers(newValue, forModuleWithID: moduleId)
}
}

init(defaults: UserDefaults = .standard) {
self.defaults = defaults
allFavorites = ProviderFavoriteServers()
moduleId = UUID()
}

func save() {
defaults.set(allFavorites.rawValue, forKey: AppPreference.moduleFavoriteServers.key)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// VPNFiltersView+Model.swift
// Passepartout
//
// Created by Davide De Rosa on 10/26/24.
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//

import Combine
import Foundation
import PassepartoutKit

extension VPNFiltersView {

@MainActor
final class Model: ObservableObject {
private(set) var categories: [String]

private(set) var countries: [(code: String, description: String)]

private(set) var presets: [AnyVPNPreset]

@Published
var filters = VPNFilters()

@Published
var onlyShowsFavorites = false

let filtersDidChange = PassthroughSubject<VPNFilters, Never>()

let onlyShowsFavoritesDidChange = PassthroughSubject<Bool, Never>()

init(
categories: [String] = [],
countries: [(code: String, description: String)] = [],
presets: [AnyVPNPreset] = []
) {
self.categories = categories
self.countries = countries
self.presets = presets
}

func load<C>(
with vpnManager: VPNProviderManager<C>,
initialFilters: VPNFilters?
) {
categories = vpnManager
.allCategoryNames
.sorted()

countries = vpnManager
.allCountryCodes
.map {
(code: $0, description: $0.localizedAsRegionCode ?? $0)
}
.sorted {
$0.description < $1.description
}

presets = vpnManager
.allPresets
.values
.sorted {
$0.description < $1.description
}

if let initialFilters {
filters = initialFilters
}

objectWillChange.send()
}
}
}
151 changes: 32 additions & 119 deletions Passepartout/Library/Sources/AppUI/Views/Provider/VPNFiltersView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,178 +24,91 @@
//

import AppLibrary
import CommonLibrary
import Combine
import PassepartoutKit
import SwiftUI

struct VPNFiltersView<Configuration>: View where Configuration: ProviderConfigurationIdentifiable & Decodable {
struct VPNFiltersView: View {

@ObservedObject
var manager: VPNProviderManager<Configuration>

@Binding
var filters: VPNFilters

@Binding
var onlyShowsFavorites: Bool

let favorites: Set<String>
var model: Model

var body: some View {
debugChanges()
return Subview(
filters: $filters,
onlyShowsFavorites: $onlyShowsFavorites,
categories: categories,
countries: countries,
presets: presets,
favorites: favorites
)
.disabled(manager.isFiltering)
.onChange(of: filters) { filters in
Task {
await manager.applyFilters(filters)
}
}
.onChange(of: favorites) {
if onlyShowsFavorites {
filters.serverIds = $0
Task {
await manager.applyFilters(filters)
}
}
}
.onChange(of: onlyShowsFavorites) {
filters.serverIds = $0 ? favorites : nil
Task {
await manager.applyFilters(filters)
}
}
}
}

private extension VPNFiltersView {
var categories: [String] {
manager
.allCategoryNames
.sorted()
}

var countries: [(code: String, description: String)] {
manager
.allCountryCodes
.map {
(code: $0, description: $0.localizedAsRegionCode ?? $0)
}
.sorted {
$0.description < $1.description
}
}

var presets: [VPNPreset<Configuration>] {
manager
.presets
.sorted {
$0.description < $1.description
}
}
}

// MARK: -

private extension VPNFiltersView {
struct Subview: View {

@Binding
var filters: VPNFilters

@Binding
var onlyShowsFavorites: Bool

let categories: [String]

let countries: [(code: String, description: String)]

let presets: [VPNPreset<Configuration>]

let favorites: Set<String>

var body: some View {
debugChanges()
return Form {
Section {
categoryPicker
countryPicker
presetPicker
favoritesToggle
return Form {
Section {
categoryPicker
countryPicker
presetPicker
favoritesToggle
#if os(iOS)
clearFiltersButton
.frame(maxWidth: .infinity, alignment: .center)
clearFiltersButton
.frame(maxWidth: .infinity, alignment: .center)
#else
HStack {
Spacer()
clearFiltersButton
}
#endif
HStack {
Spacer()
clearFiltersButton
}
#endif
}
}
.onChange(of: model.filters) {
model.filtersDidChange.send($0)
}
.onChange(of: model.onlyShowsFavorites) {
model.onlyShowsFavoritesDidChange.send($0)
}
}
}

private extension VPNFiltersView.Subview {
private extension VPNFiltersView {
var categoryPicker: some View {
Picker(Strings.Global.category, selection: $filters.categoryName) {
Picker(Strings.Global.category, selection: $model.filters.categoryName) {
Text(Strings.Global.any)
.tag(nil as String?)
ForEach(categories, id: \.self) {
ForEach(model.categories, id: \.self) {
Text($0.capitalized)
.tag($0 as String?)
}
}
}

var countryPicker: some View {
Picker(Strings.Global.country, selection: $filters.countryCode) {
Picker(Strings.Global.country, selection: $model.filters.countryCode) {
Text(Strings.Global.any)
.tag(nil as String?)
ForEach(countries, id: \.code) {
ForEach(model.countries, id: \.code) {
Text($0.description)
.tag($0.code as String?)
}
}
}

var presetPicker: some View {
Picker(Strings.Providers.Vpn.preset, selection: $filters.presetId) {
Picker(Strings.Providers.Vpn.preset, selection: $model.filters.presetId) {
Text(Strings.Global.any)
.tag(nil as String?)
ForEach(presets, id: \.presetId) {
ForEach(model.presets, id: \.presetId) {
Text($0.description)
.tag($0.presetId as String?)
}
}
}

var favoritesToggle: some View {
Toggle(Strings.Providers.onlyFavorites, isOn: $onlyShowsFavorites)
Toggle(Strings.Providers.onlyFavorites, isOn: $model.onlyShowsFavorites)
}

var clearFiltersButton: some View {
Button(Strings.Providers.clearFilters, role: .destructive) {
onlyShowsFavorites = false
filters = VPNFilters()
model.filters = VPNFilters()
model.onlyShowsFavorites = false
}
}
}

#Preview {
NavigationStack {
VPNFiltersView<OpenVPN.Configuration>(
manager: VPNProviderManager(),
filters: .constant(VPNFilters()),
onlyShowsFavorites: .constant(false),
favorites: []
)
VPNFiltersView(model: .init())
}
}
Loading