2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// ProfileEditor.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/17/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 CommonLibrary
|
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
|
|
|
@MainActor
|
2024-10-29 13:30:41 +00:00
|
|
|
public final class ProfileEditor: ObservableObject {
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
@Published
|
2024-10-06 11:41:02 +00:00
|
|
|
private var editableProfile: EditableProfile
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-03 16:41:27 +00:00
|
|
|
@Published
|
2024-10-29 13:30:41 +00:00
|
|
|
public var isShared: Bool
|
2024-10-03 16:41:27 +00:00
|
|
|
|
2024-12-09 01:00:55 +00:00
|
|
|
private var trackedPreferences: [UUID: ModulePreferences]
|
2024-12-08 15:05:23 +00:00
|
|
|
|
2024-10-06 11:41:02 +00:00
|
|
|
private(set) var removedModules: [UUID: any ModuleBuilder]
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public convenience init() {
|
2024-09-23 13:02:26 +00:00
|
|
|
self.init(modules: [])
|
|
|
|
}
|
|
|
|
|
2024-12-08 15:05:23 +00:00
|
|
|
// for testing/previews
|
2024-10-29 13:30:41 +00:00
|
|
|
public init(profile: Profile) {
|
2024-10-06 11:41:02 +00:00
|
|
|
editableProfile = profile.editable()
|
2024-10-03 16:41:27 +00:00
|
|
|
isShared = false
|
2024-12-09 01:00:55 +00:00
|
|
|
trackedPreferences = [:]
|
2024-10-06 11:41:02 +00:00
|
|
|
removedModules = [:]
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-12-08 15:05:23 +00:00
|
|
|
// for testing/previews
|
|
|
|
public init(modules: [any ModuleBuilder]) {
|
|
|
|
editableProfile = EditableProfile(
|
|
|
|
modules: modules,
|
|
|
|
activeModulesIds: Set(modules.map(\.id))
|
|
|
|
)
|
|
|
|
isShared = false
|
2024-12-09 01:00:55 +00:00
|
|
|
trackedPreferences = [:]
|
2024-10-06 11:41:02 +00:00
|
|
|
removedModules = [:]
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 11:41:02 +00:00
|
|
|
// MARK: - Types
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
extension ProfileEditor {
|
2024-10-29 13:30:41 +00:00
|
|
|
public var moduleTypes: [ModuleType] {
|
2024-10-06 11:41:02 +00:00
|
|
|
editableProfile.modules
|
2024-09-23 13:02:26 +00:00
|
|
|
.map(\.moduleType)
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public var availableModuleTypes: [ModuleType] {
|
2024-09-23 13:02:26 +00:00
|
|
|
ModuleType
|
|
|
|
.allCases
|
|
|
|
.filter {
|
2024-10-01 13:45:25 +00:00
|
|
|
// TODO: #657, hide manual OpenVPN/WireGuard until editable
|
2024-10-23 21:19:53 +00:00
|
|
|
// $0 != .openVPN && $0 != .wireGuard
|
|
|
|
$0 != .wireGuard
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
.filter {
|
|
|
|
!moduleTypes.contains($0)
|
|
|
|
}
|
|
|
|
.sorted {
|
2024-11-09 14:20:59 +00:00
|
|
|
$0.localizedDescription.lowercased() < $1.localizedDescription.lowercased()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-06 11:41:02 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-10 22:24:06 +00:00
|
|
|
// MARK: - Editing
|
2024-10-06 11:41:02 +00:00
|
|
|
|
|
|
|
extension ProfileEditor {
|
2024-10-29 13:30:41 +00:00
|
|
|
public var profile: EditableProfile {
|
2024-10-06 11:41:02 +00:00
|
|
|
get {
|
2024-10-10 22:24:06 +00:00
|
|
|
editableProfile
|
2024-10-06 11:41:02 +00:00
|
|
|
}
|
|
|
|
set {
|
2024-10-10 22:24:06 +00:00
|
|
|
editableProfile = newValue
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-10-06 11:41:02 +00:00
|
|
|
}
|
2024-11-03 22:42:17 +00:00
|
|
|
|
|
|
|
public var isAvailableForTV: Bool {
|
|
|
|
get {
|
|
|
|
editableProfile.attributes.isAvailableForTV == true
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
editableProfile.attributes.isAvailableForTV = newValue
|
|
|
|
}
|
|
|
|
}
|
2024-10-06 11:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileEditor {
|
2024-10-29 13:30:41 +00:00
|
|
|
public var modules: [any ModuleBuilder] {
|
2024-10-06 11:41:02 +00:00
|
|
|
editableProfile.modules
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
public var activeModules: [any ModuleBuilder] {
|
|
|
|
editableProfile.modules.filter {
|
|
|
|
isActiveModule(withId: $0.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public func module(withId moduleId: UUID) -> (any ModuleBuilder)? {
|
2024-10-06 11:41:02 +00:00
|
|
|
editableProfile.modules.first {
|
2024-09-23 13:02:26 +00:00
|
|
|
$0.id == moduleId
|
|
|
|
} ?? removedModules[moduleId]
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public func isActiveModule(withId moduleId: UUID) -> Bool {
|
2024-10-06 11:41:02 +00:00
|
|
|
editableProfile.isActiveModule(withId: moduleId)
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public func toggleModule(withId moduleId: UUID) {
|
2024-10-06 11:41:02 +00:00
|
|
|
guard let existingModule = module(withId: moduleId) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if isActiveModule(withId: moduleId) {
|
|
|
|
editableProfile.activeModulesIds.remove(moduleId)
|
|
|
|
} else {
|
|
|
|
activateModule(existingModule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public func moveModules(from offsets: IndexSet, to newOffset: Int) {
|
2024-10-06 11:41:02 +00:00
|
|
|
editableProfile.modules.move(fromOffsets: offsets, toOffset: newOffset)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public func removeModules(at offsets: IndexSet) {
|
2024-09-23 13:02:26 +00:00
|
|
|
offsets.forEach {
|
2024-10-06 11:41:02 +00:00
|
|
|
let module = editableProfile.modules[$0]
|
2024-09-23 13:02:26 +00:00
|
|
|
removedModules[module.id] = module
|
2024-10-06 11:41:02 +00:00
|
|
|
editableProfile.modules.remove(at: $0)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public func removeModule(withId moduleId: UUID) {
|
2024-10-06 11:41:02 +00:00
|
|
|
guard let index = editableProfile.modules.firstIndex(where: { $0.id == moduleId }) else {
|
2024-09-23 13:02:26 +00:00
|
|
|
return
|
|
|
|
}
|
2024-10-06 11:41:02 +00:00
|
|
|
let module = editableProfile.modules[index]
|
2024-09-23 13:02:26 +00:00
|
|
|
removedModules[module.id] = module
|
2024-10-06 11:41:02 +00:00
|
|
|
editableProfile.modules.remove(at: index)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public func saveModule(_ module: any ModuleBuilder, activating: Bool) {
|
2024-10-06 11:41:02 +00:00
|
|
|
if let index = editableProfile.modules.firstIndex(where: { $0.id == module.id }) {
|
|
|
|
editableProfile.modules[index] = module
|
2024-09-23 13:02:26 +00:00
|
|
|
} else {
|
2024-10-06 11:41:02 +00:00
|
|
|
editableProfile.modules.append(module)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
if activating {
|
|
|
|
activateModule(module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProfileEditor {
|
2024-10-06 11:41:02 +00:00
|
|
|
func activateModule(_ module: any ModuleBuilder) {
|
|
|
|
editableProfile.activeModulesIds.insert(module.id)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Building
|
|
|
|
|
|
|
|
extension ProfileEditor {
|
2024-10-29 13:30:41 +00:00
|
|
|
public func build() throws -> Profile {
|
2024-10-06 11:41:02 +00:00
|
|
|
let builder = try editableProfile.builder()
|
2024-09-23 13:02:26 +00:00
|
|
|
let profile = try builder.tryBuild()
|
|
|
|
|
|
|
|
// update local view
|
2024-11-17 21:30:22 +00:00
|
|
|
editableProfile.modules = profile.modulesBuilders()
|
2024-09-23 13:02:26 +00:00
|
|
|
removedModules.removeAll()
|
|
|
|
|
|
|
|
return profile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-08 15:05:23 +00:00
|
|
|
// MARK: - Load/Save
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
extension ProfileEditor {
|
2024-12-09 01:00:55 +00:00
|
|
|
public func load(_ profile: EditableProfile, isShared: Bool) {
|
2024-12-08 15:05:23 +00:00
|
|
|
editableProfile = profile
|
|
|
|
self.isShared = isShared
|
2024-12-09 01:00:55 +00:00
|
|
|
removedModules = [:]
|
|
|
|
}
|
|
|
|
|
|
|
|
public func preferences(forModuleWithId moduleId: UUID, manager: PreferencesManager) -> ModulePreferences {
|
2024-12-08 15:05:23 +00:00
|
|
|
do {
|
2024-12-09 01:00:55 +00:00
|
|
|
pp_log(.App.profiles, .debug, "Track preferences for module \(moduleId)")
|
|
|
|
let observable = try trackedPreferences[moduleId] ?? manager.preferences(forModuleWithId: moduleId)
|
|
|
|
trackedPreferences[moduleId] = observable
|
|
|
|
return observable
|
2024-12-08 15:05:23 +00:00
|
|
|
} catch {
|
2024-12-09 01:00:55 +00:00
|
|
|
pp_log(.App.profiles, .error, "Unable to track preferences for module \(moduleId): \(error)")
|
|
|
|
return ModulePreferences()
|
2024-12-08 15:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-18 16:43:01 +00:00
|
|
|
|
|
|
|
@discardableResult
|
2024-12-08 15:05:23 +00:00
|
|
|
public func save(
|
|
|
|
to profileManager: ProfileManager,
|
|
|
|
preferencesManager: PreferencesManager
|
|
|
|
) async throws -> Profile {
|
2024-09-23 13:02:26 +00:00
|
|
|
do {
|
|
|
|
let newProfile = try build()
|
2024-11-21 23:46:58 +00:00
|
|
|
try await profileManager.save(newProfile, isLocal: true, remotelyShared: isShared)
|
2024-12-09 01:00:55 +00:00
|
|
|
trackedPreferences.forEach {
|
|
|
|
do {
|
|
|
|
pp_log(.App.profiles, .debug, "Save tracked preferences for module \($0.key)")
|
|
|
|
try $0.value.save()
|
|
|
|
} catch {
|
|
|
|
pp_log(.App.profiles, .error, "Unable to save preferences for profile \(profile.id): \(error)")
|
|
|
|
}
|
2024-12-08 15:05:23 +00:00
|
|
|
}
|
2024-12-09 01:00:55 +00:00
|
|
|
trackedPreferences.removeAll()
|
2024-11-18 16:43:01 +00:00
|
|
|
return newProfile
|
2024-09-23 13:02:26 +00:00
|
|
|
} catch {
|
2024-12-08 17:56:39 +00:00
|
|
|
pp_log(.App.profiles, .fault, "Unable to save edited profile: \(error)")
|
2024-09-23 13:02:26 +00:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
2024-12-08 15:05:23 +00:00
|
|
|
|
|
|
|
public func discard() {
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-10-06 11:41:02 +00:00
|
|
|
|
|
|
|
// MARK: - Testing
|
|
|
|
|
|
|
|
extension ProfileEditor {
|
|
|
|
var activeModulesIds: Set<UUID> {
|
|
|
|
editableProfile.activeModulesIds
|
|
|
|
}
|
|
|
|
}
|