2024-10-06 11:41:02 +00:00
|
|
|
//
|
|
|
|
// EditableProfile.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/6/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 Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public struct EditableProfile: MutableProfileType {
|
2024-11-18 16:43:01 +00:00
|
|
|
public var id: UUID
|
2024-10-06 11:41:02 +00:00
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
public var name: String
|
2024-10-06 11:41:02 +00:00
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
public var modules: [any ModuleBuilder]
|
2024-10-06 11:41:02 +00:00
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
public var activeModulesIds: Set<UUID>
|
2024-10-06 11:41:02 +00:00
|
|
|
|
2024-12-06 09:26:12 +00:00
|
|
|
public var userInfo: AnyHashable?
|
2024-11-03 22:35:45 +00:00
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
public init(
|
|
|
|
id: UUID = UUID(),
|
|
|
|
name: String = "",
|
|
|
|
modules: [any ModuleBuilder] = [],
|
2024-11-26 18:14:56 +00:00
|
|
|
activeModulesIds: Set<UUID> = [],
|
2024-12-06 09:26:12 +00:00
|
|
|
userInfo: AnyHashable? = nil
|
2024-11-18 16:43:01 +00:00
|
|
|
) {
|
|
|
|
self.id = id
|
|
|
|
self.name = name
|
|
|
|
self.modules = modules
|
|
|
|
self.activeModulesIds = activeModulesIds
|
|
|
|
self.userInfo = userInfo
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public func builder() throws -> Profile.Builder {
|
2024-10-06 11:41:02 +00:00
|
|
|
var builder = Profile.Builder(id: id)
|
|
|
|
builder.modules = try modules.compactMap {
|
|
|
|
do {
|
|
|
|
return try $0.tryBuild()
|
|
|
|
} catch {
|
|
|
|
throw AppError.malformedModule($0, error: error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
builder.activeModulesIds = activeModulesIds
|
|
|
|
|
|
|
|
let trimmedName = name.trimmingCharacters(in: .whitespaces)
|
|
|
|
guard !trimmedName.isEmpty else {
|
|
|
|
throw AppError.emptyProfileName
|
|
|
|
}
|
|
|
|
builder.name = trimmedName
|
2024-11-03 22:35:45 +00:00
|
|
|
builder.userInfo = userInfo
|
|
|
|
|
2024-10-06 11:41:02 +00:00
|
|
|
return builder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-03 22:35:45 +00:00
|
|
|
extension EditableProfile {
|
2024-11-18 16:43:01 +00:00
|
|
|
public var attributes: ProfileAttributes {
|
2024-11-03 22:35:45 +00:00
|
|
|
get {
|
|
|
|
userInfo() ?? ProfileAttributes()
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
setUserInfo(newValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 11:41:02 +00:00
|
|
|
extension Profile {
|
2024-10-29 13:30:41 +00:00
|
|
|
public func editable() -> EditableProfile {
|
2024-10-06 11:41:02 +00:00
|
|
|
EditableProfile(
|
|
|
|
id: id,
|
|
|
|
name: name,
|
2024-11-17 21:30:22 +00:00
|
|
|
modules: modulesBuilders(),
|
2024-10-06 11:41:02 +00:00
|
|
|
activeModulesIds: activeModulesIds,
|
2024-11-03 22:35:45 +00:00
|
|
|
userInfo: userInfo
|
2024-10-06 11:41:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-11-17 21:30:22 +00:00
|
|
|
public func modulesBuilders() -> [any ModuleBuilder] {
|
2024-10-06 11:41:02 +00:00
|
|
|
modules.compactMap {
|
2024-11-17 21:30:22 +00:00
|
|
|
$0.moduleBuilder()
|
2024-10-06 11:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 21:27:02 +00:00
|
|
|
extension Module {
|
2024-11-17 21:30:22 +00:00
|
|
|
public func moduleBuilder() -> (any ModuleBuilder)? {
|
2024-11-17 21:27:02 +00:00
|
|
|
guard let buildableModule = self as? any BuildableType else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let builder = buildableModule.builder() as any BuilderType
|
|
|
|
return builder as? any ModuleBuilder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 11:41:02 +00:00
|
|
|
private extension EditableProfile {
|
|
|
|
var activeConnectionModule: (any ModuleBuilder)? {
|
|
|
|
modules.first {
|
|
|
|
isActiveModule(withId: $0.id) && $0.buildsConnectionModule
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|