2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// ProfileManager.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/19/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 AppData
|
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
2024-09-30 12:56:20 +00:00
|
|
|
import UtilsLibrary
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
@MainActor
|
|
|
|
public final class ProfileManager: ObservableObject {
|
2024-09-30 12:56:20 +00:00
|
|
|
public enum Event {
|
|
|
|
case save(Profile)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-09-30 12:56:20 +00:00
|
|
|
case remove([Profile.ID])
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:56:20 +00:00
|
|
|
public var beforeSave: ((Profile) async throws -> Void)?
|
|
|
|
|
|
|
|
public var afterRemove: (([Profile.ID]) async -> Void)?
|
|
|
|
|
2024-10-03 16:41:27 +00:00
|
|
|
private let repository: any ProfileRepository
|
|
|
|
|
|
|
|
private let remoteRepository: (any ProfileRepository)?
|
2024-09-30 12:56:20 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
@Published
|
2024-09-30 12:56:20 +00:00
|
|
|
private var profiles: [Profile]
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-03 16:41:27 +00:00
|
|
|
private var allProfiles: [Profile.ID: Profile] {
|
|
|
|
didSet {
|
2024-10-04 18:58:11 +00:00
|
|
|
reloadFilteredProfiles(with: searchSubject.value)
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var allRemoteProfiles: [Profile.ID: Profile]
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-03 16:41:27 +00:00
|
|
|
public let didChange: PassthroughSubject<Event, Never>
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
private let searchSubject: CurrentValueSubject<String, Never>
|
|
|
|
|
|
|
|
private var subscriptions: Set<AnyCancellable>
|
|
|
|
|
|
|
|
// for testing/previews
|
2024-09-26 21:13:55 +00:00
|
|
|
public init(profiles: [Profile]) {
|
2024-09-23 13:02:26 +00:00
|
|
|
repository = MockProfileRepository(profiles: profiles)
|
2024-10-03 16:41:27 +00:00
|
|
|
remoteRepository = nil
|
|
|
|
self.profiles = []
|
|
|
|
allProfiles = profiles.reduce(into: [:]) {
|
|
|
|
$0[$1.id] = $1
|
|
|
|
}
|
|
|
|
allRemoteProfiles = [:]
|
|
|
|
|
|
|
|
didChange = PassthroughSubject()
|
2024-09-23 13:02:26 +00:00
|
|
|
searchSubject = CurrentValueSubject("")
|
|
|
|
subscriptions = []
|
|
|
|
}
|
|
|
|
|
2024-10-03 16:41:27 +00:00
|
|
|
public init(repository: any ProfileRepository, remoteRepository: (any ProfileRepository)?) {
|
2024-09-23 13:02:26 +00:00
|
|
|
self.repository = repository
|
2024-10-03 16:41:27 +00:00
|
|
|
self.remoteRepository = remoteRepository
|
|
|
|
profiles = []
|
|
|
|
allProfiles = [:]
|
|
|
|
allRemoteProfiles = [:]
|
|
|
|
|
|
|
|
didChange = PassthroughSubject()
|
2024-09-23 13:02:26 +00:00
|
|
|
searchSubject = CurrentValueSubject("")
|
|
|
|
subscriptions = []
|
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - CRUD
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-09-30 12:56:20 +00:00
|
|
|
extension ProfileManager {
|
2024-09-23 13:02:26 +00:00
|
|
|
public var hasProfiles: Bool {
|
|
|
|
!profiles.isEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
public var isSearching: Bool {
|
|
|
|
!searchSubject.value.isEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
public var headers: [ProfileHeader] {
|
|
|
|
profiles.map {
|
|
|
|
$0.header()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func search(byName name: String) {
|
|
|
|
searchSubject.send(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func profile(withId profileId: Profile.ID) -> Profile? {
|
|
|
|
profiles.first {
|
|
|
|
$0.id == profileId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-04 18:58:11 +00:00
|
|
|
public func save(_ profile: Profile, isShared: Bool? = nil) async throws {
|
|
|
|
pp_log(.app, .notice, "Save profile \(profile.id)...")
|
2024-09-23 13:02:26 +00:00
|
|
|
do {
|
2024-10-04 19:28:56 +00:00
|
|
|
let existingProfile = allProfiles[profile.id]
|
|
|
|
if existingProfile == nil || profile != existingProfile {
|
2024-10-04 18:58:11 +00:00
|
|
|
try await beforeSave?(profile)
|
|
|
|
try await repository.saveEntities([profile])
|
|
|
|
|
|
|
|
allProfiles[profile.id] = profile
|
|
|
|
didChange.send(.save(profile))
|
|
|
|
} else {
|
|
|
|
pp_log(.app, .notice, "Profile \(profile.id) not modified, not saving")
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
} catch {
|
|
|
|
pp_log(.app, .fault, "Unable to save profile \(profile.id): \(error)")
|
|
|
|
throw error
|
|
|
|
}
|
2024-10-04 18:58:11 +00:00
|
|
|
do {
|
|
|
|
if let isShared, let remoteRepository {
|
|
|
|
if isShared {
|
|
|
|
pp_log(.app, .notice, "Enable remote sharing of profile \(profile.id)...")
|
|
|
|
try await remoteRepository.saveEntities([profile])
|
|
|
|
} else {
|
|
|
|
pp_log(.app, .notice, "Disable remote sharing of profile \(profile.id)...")
|
|
|
|
try await remoteRepository.removeEntities(withIds: [profile.id])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .fault, "Unable to save/remove remote profile \(profile.id): \(error)")
|
|
|
|
throw error
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func remove(withId profileId: Profile.ID) async {
|
|
|
|
await remove(withIds: [profileId])
|
|
|
|
}
|
|
|
|
|
|
|
|
public func remove(withIds profileIds: [Profile.ID]) async {
|
2024-10-04 18:58:11 +00:00
|
|
|
pp_log(.app, .notice, "Remove profiles \(profileIds)...")
|
2024-09-23 13:02:26 +00:00
|
|
|
do {
|
2024-10-03 23:26:52 +00:00
|
|
|
// remove local profiles
|
2024-10-03 16:41:27 +00:00
|
|
|
var newAllProfiles = allProfiles
|
2024-09-23 13:02:26 +00:00
|
|
|
try await repository.removeEntities(withIds: profileIds)
|
2024-10-04 18:58:11 +00:00
|
|
|
await afterRemove?(profileIds)
|
2024-10-03 16:41:27 +00:00
|
|
|
profileIds.forEach {
|
|
|
|
newAllProfiles.removeValue(forKey: $0)
|
|
|
|
}
|
2024-10-03 23:26:52 +00:00
|
|
|
|
|
|
|
// remove remote counterpart too
|
|
|
|
try? await remoteRepository?.removeEntities(withIds: profileIds)
|
|
|
|
profileIds.forEach {
|
|
|
|
allRemoteProfiles.removeValue(forKey: $0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// publish update
|
2024-10-03 16:41:27 +00:00
|
|
|
allProfiles = newAllProfiles
|
2024-09-30 12:56:20 +00:00
|
|
|
didChange.send(.remove(profileIds))
|
2024-09-23 13:02:26 +00:00
|
|
|
} catch {
|
|
|
|
pp_log(.app, .fault, "Unable to remove profiles \(profileIds): \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func exists(withId profileId: Profile.ID) -> Bool {
|
2024-10-03 16:41:27 +00:00
|
|
|
allProfiles.keys.contains(profileId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Remote
|
|
|
|
|
|
|
|
extension ProfileManager {
|
|
|
|
public func isRemotelyShared(profileWithId profileId: Profile.ID) -> Bool {
|
|
|
|
allRemoteProfiles.keys.contains(profileId)
|
|
|
|
}
|
|
|
|
|
2024-10-04 18:58:11 +00:00
|
|
|
public func eraseRemotelySharedProfiles() async throws {
|
|
|
|
pp_log(.app, .notice, "Erase remotely shared profiles...")
|
2024-10-03 21:25:51 +00:00
|
|
|
try await remoteRepository?.removeEntities(withIds: Array(allRemoteProfiles.keys))
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:56:20 +00:00
|
|
|
// MARK: - Shortcuts
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
extension ProfileManager {
|
|
|
|
public func new(withName name: String) -> Profile {
|
|
|
|
var builder = Profile.Builder()
|
|
|
|
builder.name = firstUniqueName(from: name)
|
|
|
|
do {
|
|
|
|
return try builder.tryBuild()
|
|
|
|
} catch {
|
|
|
|
fatalError("Unable to build new empty profile: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func duplicate(profileWithId profileId: Profile.ID) async throws {
|
|
|
|
guard let profile = profile(withId: profileId) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var builder = profile.builder(withNewId: true)
|
|
|
|
builder.name = firstUniqueName(from: profile.name)
|
2024-10-04 18:58:11 +00:00
|
|
|
pp_log(.app, .notice, "Duplicate profile [\(profileId), \(profile.name)] -> [\(builder.id), \(builder.name)]...")
|
2024-09-23 13:02:26 +00:00
|
|
|
let copy = try builder.tryBuild()
|
|
|
|
|
|
|
|
try await save(copy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProfileManager {
|
2024-09-30 12:56:20 +00:00
|
|
|
func firstUniqueName(from name: String) -> String {
|
|
|
|
let allNames = profiles.map(\.name)
|
|
|
|
var newName = name
|
|
|
|
var index = 1
|
|
|
|
while true {
|
|
|
|
if !allNames.contains(newName) {
|
|
|
|
return newName
|
|
|
|
}
|
|
|
|
newName = [name, index.description].joined(separator: ".")
|
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Observation
|
|
|
|
|
|
|
|
extension ProfileManager {
|
|
|
|
public func observeObjects(searchDebounce: Int = 200) {
|
2024-09-23 13:02:26 +00:00
|
|
|
repository
|
|
|
|
.entitiesPublisher
|
2024-10-03 16:41:27 +00:00
|
|
|
.first()
|
2024-09-23 13:02:26 +00:00
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] in
|
2024-10-04 18:58:11 +00:00
|
|
|
self?.reloadLocalProfiles($0)
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
|
|
|
remoteRepository?
|
|
|
|
.entitiesPublisher
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] in
|
2024-10-04 18:58:11 +00:00
|
|
|
self?.reloadRemoteProfiles($0)
|
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
|
|
|
remoteRepository?
|
|
|
|
.entitiesPublisher
|
|
|
|
.dropFirst()
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] in
|
|
|
|
self?.importRemoteProfiles($0)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
|
|
|
searchSubject
|
|
|
|
.debounce(for: .milliseconds(searchDebounce), scheduler: DispatchQueue.main)
|
2024-09-30 12:56:20 +00:00
|
|
|
.sink { [weak self] in
|
|
|
|
self?.performSearch($0)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-09-30 12:56:20 +00:00
|
|
|
private extension ProfileManager {
|
2024-10-04 18:58:11 +00:00
|
|
|
func reloadLocalProfiles(_ result: EntitiesResult<Profile>) {
|
|
|
|
pp_log(.app, .info, "Reload local profiles: \(result.entities.map(\.id))")
|
2024-10-03 16:41:27 +00:00
|
|
|
allProfiles = result.entities.reduce(into: [:]) {
|
2024-09-30 12:56:20 +00:00
|
|
|
$0[$1.id] = $1
|
|
|
|
}
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
|
|
|
|
2024-10-04 18:58:11 +00:00
|
|
|
func reloadRemoteProfiles(_ result: EntitiesResult<Profile>) {
|
|
|
|
pp_log(.app, .info, "Reload remote profiles: \(result.entities.map(\.id))")
|
2024-10-03 16:41:27 +00:00
|
|
|
allRemoteProfiles = result.entities.reduce(into: [:]) {
|
|
|
|
$0[$1.id] = $1
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
2024-10-03 21:25:51 +00:00
|
|
|
objectWillChange.send()
|
2024-10-04 18:58:11 +00:00
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
|
2024-10-04 18:58:11 +00:00
|
|
|
// pull remote updates into local profiles (best-effort)
|
|
|
|
func importRemoteProfiles(_ result: EntitiesResult<Profile>) {
|
|
|
|
let profilesToImport = result.entities
|
|
|
|
pp_log(.app, .info, "Try to import remote profiles: \(result.entities.map(\.id))")
|
2024-10-04 13:50:45 +00:00
|
|
|
|
2024-10-03 20:25:59 +00:00
|
|
|
Task.detached { [weak self] in
|
|
|
|
for remoteProfile in profilesToImport {
|
2024-10-03 16:41:27 +00:00
|
|
|
do {
|
|
|
|
pp_log(.app, .notice, "Import remote profile \(remoteProfile.id)...")
|
|
|
|
try await self?.save(remoteProfile)
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .error, "Unable to import remote profile: \(error)")
|
|
|
|
}
|
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func performSearch(_ search: String) {
|
2024-10-04 18:58:11 +00:00
|
|
|
pp_log(.app, .notice, "Filter profiles with '\(search)'")
|
2024-10-03 16:41:27 +00:00
|
|
|
reloadFilteredProfiles(with: search)
|
|
|
|
}
|
|
|
|
|
2024-10-04 18:58:11 +00:00
|
|
|
func reloadFilteredProfiles(with search: String) {
|
2024-10-03 16:41:27 +00:00
|
|
|
profiles = allProfiles
|
|
|
|
.values
|
|
|
|
.filter {
|
2024-10-04 18:58:11 +00:00
|
|
|
if !search.isEmpty {
|
2024-10-03 16:41:27 +00:00
|
|
|
return $0.name.lowercased().contains(search.lowercased())
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
.sorted {
|
|
|
|
$0.name.lowercased() < $1.name.lowercased()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|