passepartout-apple/Passepartout/Library/Sources/AppUI/Business/ProfileEditor.swift

210 lines
5.4 KiB
Swift
Raw Normal View History

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 AppLibrary
2024-09-23 13:02:26 +00:00
import Combine
import CommonLibrary
import Foundation
import PassepartoutKit
@MainActor
final class ProfileEditor: ObservableObject {
@Published
private var editableProfile: EditableProfile
2024-09-23 13:02:26 +00:00
@Published
var isShared: Bool
private(set) var removedModules: [UUID: any ModuleBuilder]
2024-09-23 13:02:26 +00:00
convenience init() {
self.init(modules: [])
}
init(modules: [any ModuleBuilder]) {
editableProfile = EditableProfile(
modules: modules,
activeModulesIds: Set(modules.map(\.id))
)
isShared = false
removedModules = [:]
2024-09-23 13:02:26 +00:00
}
init(profile: Profile) {
editableProfile = profile.editable()
isShared = false
removedModules = [:]
2024-09-23 13:02:26 +00:00
}
func editProfile(_ profile: Profile, isShared: Bool) {
editableProfile = profile.editable()
self.isShared = isShared
removedModules = [:]
2024-09-23 13:02:26 +00:00
}
}
// MARK: - Types
2024-09-23 13:02:26 +00:00
extension ProfileEditor {
var moduleTypes: [ModuleType] {
editableProfile.modules
2024-09-23 13:02:26 +00:00
.compactMap {
$0 as? ModuleTypeProviding
}
.map(\.moduleType)
}
var availableModuleTypes: [ModuleType] {
ModuleType
.allCases
.filter {
2024-10-01 13:45:25 +00:00
// TODO: #657, hide manual OpenVPN/WireGuard until editable
2024-09-23 13:02:26 +00:00
$0 != .openVPN && $0 != .wireGuard
}
.filter {
!moduleTypes.contains($0)
}
.sorted {
$0.localizedDescription < $1.localizedDescription
}
}
}
2024-09-23 13:02:26 +00:00
// MARK: - Editing
extension ProfileEditor {
var profile: EditableProfile {
get {
editableProfile
}
set {
editableProfile = newValue
2024-09-23 13:02:26 +00:00
}
}
}
extension ProfileEditor {
var modules: [any ModuleBuilder] {
editableProfile.modules
2024-09-23 13:02:26 +00:00
}
func module(withId moduleId: UUID) -> (any ModuleBuilder)? {
editableProfile.modules.first {
2024-09-23 13:02:26 +00:00
$0.id == moduleId
} ?? removedModules[moduleId]
}
func isActiveModule(withId moduleId: UUID) -> Bool {
editableProfile.isActiveModule(withId: moduleId)
}
func toggleModule(withId moduleId: UUID) {
guard let existingModule = module(withId: moduleId) else {
return
}
if isActiveModule(withId: moduleId) {
editableProfile.activeModulesIds.remove(moduleId)
} else {
activateModule(existingModule)
}
}
2024-09-23 13:02:26 +00:00
func moveModules(from offsets: IndexSet, to newOffset: Int) {
editableProfile.modules.move(fromOffsets: offsets, toOffset: newOffset)
2024-09-23 13:02:26 +00:00
}
func removeModules(at offsets: IndexSet) {
offsets.forEach {
let module = editableProfile.modules[$0]
2024-09-23 13:02:26 +00:00
removedModules[module.id] = module
editableProfile.modules.remove(at: $0)
2024-09-23 13:02:26 +00:00
}
}
func removeModule(withId moduleId: UUID) {
guard let index = editableProfile.modules.firstIndex(where: { $0.id == moduleId }) else {
2024-09-23 13:02:26 +00:00
return
}
let module = editableProfile.modules[index]
2024-09-23 13:02:26 +00:00
removedModules[module.id] = module
editableProfile.modules.remove(at: index)
2024-09-23 13:02:26 +00:00
}
func saveModule(_ module: any ModuleBuilder, activating: Bool) {
if let index = editableProfile.modules.firstIndex(where: { $0.id == module.id }) {
editableProfile.modules[index] = module
2024-09-23 13:02:26 +00:00
} else {
editableProfile.modules.append(module)
2024-09-23 13:02:26 +00:00
}
if activating {
activateModule(module)
}
}
}
private extension ProfileEditor {
func activateModule(_ module: any ModuleBuilder) {
editableProfile.activeModulesIds.insert(module.id)
2024-09-23 13:02:26 +00:00
}
}
// MARK: - Building
extension ProfileEditor {
func build() throws -> Profile {
let builder = try editableProfile.builder()
2024-09-23 13:02:26 +00:00
let profile = try builder.tryBuild()
// update local view
editableProfile.modules = profile.modulesBuilders
2024-09-23 13:02:26 +00:00
removedModules.removeAll()
return profile
}
}
// MARK: - Saving
extension ProfileEditor {
func save(to profileManager: ProfileManager) async throws {
do {
let newProfile = try build()
try await profileManager.save(newProfile, isShared: isShared)
2024-09-23 13:02:26 +00:00
} catch {
pp_log(.app, .fault, "Unable to save edited profile: \(error)")
throw error
}
}
}
// MARK: - Testing
extension ProfileEditor {
var activeModulesIds: Set<UUID> {
editableProfile.activeModulesIds
}
}