diff --git a/Passepartout/Library/Sources/AppUIMain/Views/Provider/VPNFiltersView+Model.swift b/Passepartout/Library/Sources/AppUIMain/Views/Provider/VPNFiltersView+Model.swift index eba0f924..640c4c38 100644 --- a/Passepartout/Library/Sources/AppUIMain/Views/Provider/VPNFiltersView+Model.swift +++ b/Passepartout/Library/Sources/AppUIMain/Views/Provider/VPNFiltersView+Model.swift @@ -24,6 +24,7 @@ // import Combine +import CommonLibrary import Foundation import PassepartoutKit @@ -33,6 +34,8 @@ extension VPNFiltersView { final class Model: ObservableObject { typealias CodeWithDescription = (code: String, description: String) + private let defaults: UserDefaults + private var options: VPNFilterOptions @Published @@ -45,22 +48,21 @@ extension VPNFiltersView { private(set) var presets: [AnyVPNPreset] @Published - var filters = VPNFilters() + var filters: VPNFilters @Published - var onlyShowsFavorites = false - - let filtersDidChange = PassthroughSubject() - - let onlyShowsFavoritesDidChange = PassthroughSubject() + var onlyShowsFavorites: Bool private var subscriptions: Set - init() { + init(defaults: UserDefaults = .standard) { + self.defaults = defaults options = VPNFilterOptions() categories = [] countries = [] presets = [] + filters = VPNFilters() + onlyShowsFavorites = false subscriptions = [] observeObjects() @@ -129,22 +131,31 @@ private extension VPNFiltersView.Model { private extension VPNFiltersView.Model { func observeObjects() { - $filters + $onlyShowsFavorites + .dropFirst() .sink { [weak self] in - self?.filtersDidChange.send($0) + self?.defaults.onlyShowsFavorites = $0 } .store(in: &subscriptions) - $onlyShowsFavorites - .sink { [weak self] in - self?.onlyShowsFavoritesDidChange.send($0) - } - .store(in: &subscriptions) + // send initial value + onlyShowsFavorites = defaults.onlyShowsFavorites } } // MARK: - +private extension UserDefaults { + var onlyShowsFavorites: Bool { + get { + bool(forKey: AppPreference.onlyShowsFavorites.key) + } + set { + set(newValue, forKey: AppPreference.onlyShowsFavorites.key) + } + } +} + private extension String { var asCountryCodeWithDescription: VPNFiltersView.Model.CodeWithDescription { (self, localizedAsRegionCode ?? self) diff --git a/Passepartout/Library/Sources/AppUIMain/Views/Provider/VPNProviderServerView.swift b/Passepartout/Library/Sources/AppUIMain/Views/Provider/VPNProviderServerView.swift index 0ea30e9c..7bceb419 100644 --- a/Passepartout/Library/Sources/AppUIMain/Views/Provider/VPNProviderServerView.swift +++ b/Passepartout/Library/Sources/AppUIMain/Views/Provider/VPNProviderServerView.swift @@ -166,12 +166,12 @@ extension VPNProviderServerView { errorHandler.handle(error, title: Strings.Global.servers) } } - .onReceive(filtersViewModel.filtersDidChange) { newValue in + .onReceive(filtersViewModel.$filters) { newValue in Task { await reloadServers(filters: newValue) } } - .onReceive(filtersViewModel.onlyShowsFavoritesDidChange) { newValue in + .onReceive(filtersViewModel.$onlyShowsFavorites) { newValue in onlyShowsFavorites = newValue } .onDisappear { diff --git a/Passepartout/Library/Sources/CommonLibrary/Domain/AppPreference.swift b/Passepartout/Library/Sources/CommonLibrary/Domain/AppPreference.swift index 9492074b..6f5e3b3e 100644 --- a/Passepartout/Library/Sources/CommonLibrary/Domain/AppPreference.swift +++ b/Passepartout/Library/Sources/CommonLibrary/Domain/AppPreference.swift @@ -32,6 +32,8 @@ public enum AppPreference: String { case logsPrivateData + case onlyShowsFavorites + case profilesLayout case providerFavoriteServers