2024-12-06 10:24:51 +00:00
|
|
|
//
|
|
|
|
// PreferencesManager.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 12/4/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 CommonUtils
|
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
2024-12-08 15:05:23 +00:00
|
|
|
public final class PreferencesManager: ObservableObject, Sendable {
|
2024-12-09 01:00:55 +00:00
|
|
|
private let modulesFactory: @Sendable (UUID) throws -> ModulePreferencesRepository
|
2024-12-06 10:24:51 +00:00
|
|
|
|
2024-12-08 15:05:23 +00:00
|
|
|
private let providersFactory: @Sendable (ProviderID) throws -> ProviderPreferencesRepository
|
2024-12-06 10:24:51 +00:00
|
|
|
|
|
|
|
public init(
|
2024-12-09 01:00:55 +00:00
|
|
|
modulesFactory: (@Sendable (UUID) throws -> ModulePreferencesRepository)? = nil,
|
2024-12-08 15:05:23 +00:00
|
|
|
providersFactory: (@Sendable (ProviderID) throws -> ProviderPreferencesRepository)? = nil
|
2024-12-06 10:24:51 +00:00
|
|
|
) {
|
2024-12-09 01:00:55 +00:00
|
|
|
self.modulesFactory = modulesFactory ?? { _ in
|
|
|
|
DummyModulePreferencesRepository()
|
|
|
|
}
|
2024-12-08 15:05:23 +00:00
|
|
|
self.providersFactory = providersFactory ?? { _ in
|
|
|
|
DummyProviderPreferencesRepository()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PreferencesManager {
|
2024-12-09 01:00:55 +00:00
|
|
|
public func preferencesRepository(forModuleWithId moduleId: UUID) throws -> ModulePreferencesRepository {
|
|
|
|
try modulesFactory(moduleId)
|
2024-12-08 15:05:23 +00:00
|
|
|
}
|
|
|
|
|
2024-12-09 01:00:55 +00:00
|
|
|
public func preferencesRepository(forProviderWithId providerId: ProviderID) throws -> ProviderPreferencesRepository {
|
|
|
|
try providersFactory(providerId)
|
2024-12-06 10:24:51 +00:00
|
|
|
}
|
2024-12-08 15:05:23 +00:00
|
|
|
}
|
2024-12-06 10:24:51 +00:00
|
|
|
|
2024-12-09 01:00:55 +00:00
|
|
|
@MainActor
|
|
|
|
extension PreferencesManager {
|
|
|
|
public func preferences(forModuleWithId moduleId: UUID) throws -> ModulePreferences {
|
|
|
|
let object = ModulePreferences()
|
|
|
|
object.repository = try modulesFactory(moduleId)
|
|
|
|
return object
|
2024-12-06 10:24:51 +00:00
|
|
|
}
|
|
|
|
|
2024-12-09 01:00:55 +00:00
|
|
|
public func preferences(forProviderWithId providerId: ProviderID) throws -> ProviderPreferences {
|
|
|
|
let object = ProviderPreferences()
|
|
|
|
object.repository = try providersFactory(providerId)
|
|
|
|
return object
|
2024-12-06 10:24:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Dummy
|
|
|
|
|
|
|
|
private final class DummyModulePreferencesRepository: ModulePreferencesRepository {
|
2024-12-09 01:00:55 +00:00
|
|
|
func isExcludedEndpoint(_ endpoint: ExtendedEndpoint) -> Bool {
|
|
|
|
false
|
2024-12-06 10:24:51 +00:00
|
|
|
}
|
|
|
|
|
2024-12-09 01:00:55 +00:00
|
|
|
func addExcludedEndpoint(_ endpoint: ExtendedEndpoint) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeExcludedEndpoint(_ endpoint: ExtendedEndpoint) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func save() throws {
|
2024-12-06 10:24:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final class DummyProviderPreferencesRepository: ProviderPreferencesRepository {
|
2024-12-08 15:05:23 +00:00
|
|
|
var favoriteServers: Set<String> = []
|
2024-12-06 10:24:51 +00:00
|
|
|
|
2024-12-09 01:00:55 +00:00
|
|
|
func isExcludedEndpoint(_ endpoint: ExtendedEndpoint) -> Bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
func addExcludedEndpoint(_ endpoint: ExtendedEndpoint) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeExcludedEndpoint(_ endpoint: ExtendedEndpoint) {
|
|
|
|
}
|
|
|
|
|
2024-12-08 15:05:23 +00:00
|
|
|
func save() throws {
|
2024-12-06 10:24:51 +00:00
|
|
|
}
|
|
|
|
}
|