Persist "Only favorites" toggle (#813)

Drop didChange subjects from filters model, observe published fields
directly.
This commit is contained in:
Davide 2024-11-05 11:28:27 +01:00 committed by GitHub
parent 1cb46e066c
commit 833d717f06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 16 deletions

View File

@ -24,6 +24,7 @@
// //
import Combine import Combine
import CommonLibrary
import Foundation import Foundation
import PassepartoutKit import PassepartoutKit
@ -33,6 +34,8 @@ extension VPNFiltersView {
final class Model: ObservableObject { final class Model: ObservableObject {
typealias CodeWithDescription = (code: String, description: String) typealias CodeWithDescription = (code: String, description: String)
private let defaults: UserDefaults
private var options: VPNFilterOptions private var options: VPNFilterOptions
@Published @Published
@ -45,22 +48,21 @@ extension VPNFiltersView {
private(set) var presets: [AnyVPNPreset] private(set) var presets: [AnyVPNPreset]
@Published @Published
var filters = VPNFilters() var filters: VPNFilters
@Published @Published
var onlyShowsFavorites = false var onlyShowsFavorites: Bool
let filtersDidChange = PassthroughSubject<VPNFilters, Never>()
let onlyShowsFavoritesDidChange = PassthroughSubject<Bool, Never>()
private var subscriptions: Set<AnyCancellable> private var subscriptions: Set<AnyCancellable>
init() { init(defaults: UserDefaults = .standard) {
self.defaults = defaults
options = VPNFilterOptions() options = VPNFilterOptions()
categories = [] categories = []
countries = [] countries = []
presets = [] presets = []
filters = VPNFilters()
onlyShowsFavorites = false
subscriptions = [] subscriptions = []
observeObjects() observeObjects()
@ -129,22 +131,31 @@ private extension VPNFiltersView.Model {
private extension VPNFiltersView.Model { private extension VPNFiltersView.Model {
func observeObjects() { func observeObjects() {
$filters $onlyShowsFavorites
.dropFirst()
.sink { [weak self] in .sink { [weak self] in
self?.filtersDidChange.send($0) self?.defaults.onlyShowsFavorites = $0
} }
.store(in: &subscriptions) .store(in: &subscriptions)
$onlyShowsFavorites // send initial value
.sink { [weak self] in onlyShowsFavorites = defaults.onlyShowsFavorites
self?.onlyShowsFavoritesDidChange.send($0)
}
.store(in: &subscriptions)
} }
} }
// MARK: - // MARK: -
private extension UserDefaults {
var onlyShowsFavorites: Bool {
get {
bool(forKey: AppPreference.onlyShowsFavorites.key)
}
set {
set(newValue, forKey: AppPreference.onlyShowsFavorites.key)
}
}
}
private extension String { private extension String {
var asCountryCodeWithDescription: VPNFiltersView.Model.CodeWithDescription { var asCountryCodeWithDescription: VPNFiltersView.Model.CodeWithDescription {
(self, localizedAsRegionCode ?? self) (self, localizedAsRegionCode ?? self)

View File

@ -166,12 +166,12 @@ extension VPNProviderServerView {
errorHandler.handle(error, title: Strings.Global.servers) errorHandler.handle(error, title: Strings.Global.servers)
} }
} }
.onReceive(filtersViewModel.filtersDidChange) { newValue in .onReceive(filtersViewModel.$filters) { newValue in
Task { Task {
await reloadServers(filters: newValue) await reloadServers(filters: newValue)
} }
} }
.onReceive(filtersViewModel.onlyShowsFavoritesDidChange) { newValue in .onReceive(filtersViewModel.$onlyShowsFavorites) { newValue in
onlyShowsFavorites = newValue onlyShowsFavorites = newValue
} }
.onDisappear { .onDisappear {

View File

@ -32,6 +32,8 @@ public enum AppPreference: String {
case logsPrivateData case logsPrivateData
case onlyShowsFavorites
case profilesLayout case profilesLayout
case providerFavoriteServers case providerFavoriteServers