passepartout-apple/Passepartout/Library/Sources/UILibrary/Domain/EditableProfile.swift
Davide a22584c630
Fine-tune profile management with additional attributes (#807)
Additions to the domain:

- Update rather than replace existing Core Data profile
- Attach ProfileAttributes to Profile.userInfo
- Store one-off `fingerprint` UUID on each save

With the above in place, fix and improve ProfileManager to:

- Use `fingerprint` to compare local/remote profiles in history and thus
avoid local re-import of shared profiles
- Use `deletingRemotely` to delete local profiles when removed from the
remote repository (default false)
- Use `isIncluded` filter to exclude certain profiles from the local
repository (default nil)
2024-11-03 23:35:45 +01:00

116 lines
3.3 KiB
Swift

//
// 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 CommonLibrary
import Foundation
import PassepartoutKit
public struct EditableProfile: MutableProfileType {
public var id = UUID()
public var name: String = ""
public var modules: [any ModuleBuilder] = []
public var activeModulesIds: Set<UUID> = []
public var modulesMetadata: [UUID: ModuleMetadata]?
public var userInfo: [String: AnyHashable]?
public func builder() throws -> Profile.Builder {
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
builder.modulesMetadata = modulesMetadata?.reduce(into: [:]) {
var metadata = $1.value
if var trimmedName = metadata.name {
trimmedName = trimmedName.trimmingCharacters(in: .whitespaces)
metadata.name = !trimmedName.isEmpty ? trimmedName : nil
}
$0[$1.key] = metadata
}
builder.userInfo = userInfo
return builder
}
}
extension EditableProfile {
var attributes: ProfileAttributes {
get {
userInfo() ?? ProfileAttributes()
}
set {
setUserInfo(newValue)
}
}
}
extension Profile {
public func editable() -> EditableProfile {
EditableProfile(
id: id,
name: name,
modules: modulesBuilders,
activeModulesIds: activeModulesIds,
modulesMetadata: modulesMetadata,
userInfo: userInfo
)
}
public var modulesBuilders: [any ModuleBuilder] {
modules.compactMap {
guard let buildableModule = $0 as? any BuildableType else {
return nil
}
let builder = buildableModule.builder() as any BuilderType
return builder as? any ModuleBuilder
}
}
}
private extension EditableProfile {
var activeConnectionModule: (any ModuleBuilder)? {
modules.first {
isActiveModule(withId: $0.id) && $0.buildsConnectionModule
}
}
}