2024-10-26 18:28:02 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
|
|
|
|
2024-11-26 20:21:49 +00:00
|
|
|
import CommonLibrary
|
2024-10-26 18:28:02 +00:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@MainActor
|
2024-11-01 22:32:35 +00:00
|
|
|
public final class ProviderFavoritesManager: ObservableObject {
|
2024-10-26 18:28:02 +00:00
|
|
|
private let defaults: UserDefaults
|
|
|
|
|
|
|
|
private var allFavorites: ProviderFavoriteServers
|
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
public var moduleId: UUID {
|
2024-10-26 18:28:02 +00:00
|
|
|
didSet {
|
2024-11-26 20:21:49 +00:00
|
|
|
guard let rawValue = defaults.string(forKey: UIPreference.providerFavoriteServers.key) else {
|
2024-10-26 18:28:02 +00:00
|
|
|
allFavorites = ProviderFavoriteServers()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
allFavorites = ProviderFavoriteServers(rawValue: rawValue) ?? ProviderFavoriteServers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
public var serverIds: Set<String> {
|
2024-10-26 18:28:02 +00:00
|
|
|
get {
|
2024-11-03 22:35:45 +00:00
|
|
|
allFavorites.servers(forModuleWithId: moduleId)
|
2024-10-26 18:28:02 +00:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
objectWillChange.send()
|
2024-11-03 22:35:45 +00:00
|
|
|
allFavorites.setServers(newValue, forModuleWithId: moduleId)
|
2024-10-26 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
public init(defaults: UserDefaults = .standard) {
|
2024-10-26 18:28:02 +00:00
|
|
|
self.defaults = defaults
|
|
|
|
allFavorites = ProviderFavoriteServers()
|
|
|
|
moduleId = UUID()
|
|
|
|
}
|
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
public func save() {
|
2024-11-26 20:21:49 +00:00
|
|
|
defaults.set(allFavorites.rawValue, forKey: UIPreference.providerFavoriteServers.key)
|
2024-10-26 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|