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 Combine
|
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
public final class ProfileManager: ObservableObject {
|
2024-11-11 11:08:22 +00:00
|
|
|
private enum Observer: CaseIterable {
|
|
|
|
case local
|
|
|
|
|
|
|
|
case remote
|
|
|
|
}
|
|
|
|
|
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-11-11 11:08:22 +00:00
|
|
|
// MARK: Dependencies
|
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
private let repository: ProfileRepository
|
2024-10-03 16:41:27 +00:00
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
private let backupRepository: ProfileRepository?
|
2024-10-26 19:41:07 +00:00
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
private let remoteRepositoryBlock: ((Bool) -> ProfileRepository)?
|
2024-09-30 12:56:20 +00:00
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
private var remoteRepository: ProfileRepository?
|
|
|
|
|
|
|
|
private let mirrorsRemoteRepository: Bool
|
2024-11-03 22:35:45 +00:00
|
|
|
|
2024-11-04 22:34:22 +00:00
|
|
|
private let processor: ProfileProcessor?
|
2024-11-03 22:35:45 +00:00
|
|
|
|
2024-11-11 11:08:22 +00:00
|
|
|
// MARK: State
|
|
|
|
|
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-11-19 18:44:31 +00:00
|
|
|
reloadRequiredFeatures()
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-11 11:08:22 +00:00
|
|
|
private var allRemoteProfiles: [Profile.ID: Profile]
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
private var filteredProfiles: [Profile]
|
|
|
|
|
2024-11-19 07:55:41 +00:00
|
|
|
@Published
|
|
|
|
private var requiredFeatures: [Profile.ID: Set<AppFeature>]
|
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
@Published
|
|
|
|
public private(set) var isRemoteImportingEnabled: Bool
|
|
|
|
|
2024-11-11 11:08:22 +00:00
|
|
|
private var waitingObservers: Set<Observer>
|
|
|
|
|
|
|
|
// MARK: Publishers
|
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>
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
private var localSubscription: AnyCancellable?
|
|
|
|
|
|
|
|
private var remoteSubscription: AnyCancellable?
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
private var searchSubscription: AnyCancellable?
|
|
|
|
|
|
|
|
private var remoteImportTask: Task<Void, Never>?
|
2024-11-09 14:20:59 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
// for testing/previews
|
2024-09-26 21:13:55 +00:00
|
|
|
public init(profiles: [Profile]) {
|
2024-10-10 14:03:02 +00:00
|
|
|
repository = InMemoryProfileRepository(profiles: profiles)
|
2024-10-26 19:41:07 +00:00
|
|
|
backupRepository = nil
|
2024-11-10 19:54:00 +00:00
|
|
|
remoteRepositoryBlock = { _ in
|
|
|
|
InMemoryProfileRepository()
|
|
|
|
}
|
2024-11-09 14:20:59 +00:00
|
|
|
mirrorsRemoteRepository = false
|
2024-11-04 22:34:22 +00:00
|
|
|
processor = nil
|
2024-11-11 11:08:22 +00:00
|
|
|
|
2024-10-03 16:41:27 +00:00
|
|
|
allProfiles = profiles.reduce(into: [:]) {
|
|
|
|
$0[$1.id] = $1
|
|
|
|
}
|
|
|
|
allRemoteProfiles = [:]
|
2024-11-17 10:34:43 +00:00
|
|
|
filteredProfiles = []
|
2024-11-19 07:55:41 +00:00
|
|
|
requiredFeatures = [:]
|
2024-11-11 11:08:22 +00:00
|
|
|
isRemoteImportingEnabled = false
|
|
|
|
waitingObservers = []
|
2024-10-03 16:41:27 +00:00
|
|
|
|
|
|
|
didChange = PassthroughSubject()
|
2024-09-23 13:02:26 +00:00
|
|
|
searchSubject = CurrentValueSubject("")
|
2024-11-17 10:34:43 +00:00
|
|
|
|
|
|
|
observeSearch()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-26 19:41:07 +00:00
|
|
|
public init(
|
2024-11-09 14:20:59 +00:00
|
|
|
repository: ProfileRepository,
|
|
|
|
backupRepository: ProfileRepository? = nil,
|
|
|
|
remoteRepositoryBlock: ((Bool) -> ProfileRepository)?,
|
|
|
|
mirrorsRemoteRepository: Bool = false,
|
2024-11-04 22:34:22 +00:00
|
|
|
processor: ProfileProcessor? = nil
|
2024-10-26 19:41:07 +00:00
|
|
|
) {
|
2024-11-09 14:20:59 +00:00
|
|
|
precondition(!mirrorsRemoteRepository || remoteRepositoryBlock != nil, "mirrorsRemoteRepository requires a non-nil remoteRepositoryBlock")
|
2024-09-23 13:02:26 +00:00
|
|
|
self.repository = repository
|
2024-10-26 19:41:07 +00:00
|
|
|
self.backupRepository = backupRepository
|
2024-11-09 14:20:59 +00:00
|
|
|
self.remoteRepositoryBlock = remoteRepositoryBlock
|
|
|
|
self.mirrorsRemoteRepository = mirrorsRemoteRepository
|
2024-11-04 22:34:22 +00:00
|
|
|
self.processor = processor
|
2024-11-11 11:08:22 +00:00
|
|
|
|
2024-10-03 16:41:27 +00:00
|
|
|
allProfiles = [:]
|
|
|
|
allRemoteProfiles = [:]
|
2024-11-17 10:34:43 +00:00
|
|
|
filteredProfiles = []
|
2024-11-19 07:55:41 +00:00
|
|
|
requiredFeatures = [:]
|
2024-11-11 11:08:22 +00:00
|
|
|
isRemoteImportingEnabled = false
|
|
|
|
if remoteRepositoryBlock != nil {
|
|
|
|
waitingObservers = [.local, .remote]
|
|
|
|
} else {
|
|
|
|
waitingObservers = [.local]
|
|
|
|
}
|
2024-10-03 16:41:27 +00:00
|
|
|
|
|
|
|
didChange = PassthroughSubject()
|
2024-09-23 13:02:26 +00:00
|
|
|
searchSubject = CurrentValueSubject("")
|
2024-11-17 10:34:43 +00:00
|
|
|
|
|
|
|
observeSearch()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
// MARK: - View
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-09-30 12:56:20 +00:00
|
|
|
extension ProfileManager {
|
2024-11-17 10:34:43 +00:00
|
|
|
public var isReady: Bool {
|
|
|
|
waitingObservers.isEmpty
|
|
|
|
}
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
public var hasProfiles: Bool {
|
2024-11-17 10:34:43 +00:00
|
|
|
!filteredProfiles.isEmpty
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var isSearching: Bool {
|
|
|
|
!searchSubject.value.isEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
public var headers: [ProfileHeader] {
|
2024-11-17 10:34:43 +00:00
|
|
|
filteredProfiles.map {
|
2024-09-23 13:02:26 +00:00
|
|
|
$0.header()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func profile(withId profileId: Profile.ID) -> Profile? {
|
2024-11-17 10:34:43 +00:00
|
|
|
filteredProfiles.first {
|
2024-09-23 13:02:26 +00:00
|
|
|
$0.id == profileId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 07:55:41 +00:00
|
|
|
public func requiredFeatures(forProfileWithId profileId: Profile.ID) -> Set<AppFeature>? {
|
|
|
|
requiredFeatures[profileId]
|
|
|
|
}
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
public func search(byName name: String) {
|
|
|
|
searchSubject.send(name)
|
|
|
|
}
|
2024-11-19 18:44:31 +00:00
|
|
|
|
|
|
|
public func reloadRequiredFeatures() {
|
|
|
|
guard let processor else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
requiredFeatures = allProfiles.reduce(into: [:]) {
|
|
|
|
guard let ineligible = processor.verify($1.value), !ineligible.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$0[$1.key] = ineligible
|
|
|
|
}
|
|
|
|
pp_log(.App.profiles, .info, "Required features: \(requiredFeatures)")
|
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - CRUD
|
|
|
|
|
|
|
|
extension ProfileManager {
|
2024-11-09 14:20:59 +00:00
|
|
|
public func save(_ originalProfile: Profile, force: Bool = false, remotelyShared: Bool? = nil) async throws {
|
2024-11-04 09:10:17 +00:00
|
|
|
let profile: Profile
|
|
|
|
if force {
|
|
|
|
var builder = originalProfile.builder()
|
2024-11-04 22:34:22 +00:00
|
|
|
if let processor {
|
|
|
|
builder = try processor.willSave(builder)
|
|
|
|
}
|
2024-11-04 09:10:17 +00:00
|
|
|
builder.attributes.lastUpdate = Date()
|
|
|
|
builder.attributes.fingerprint = UUID()
|
|
|
|
profile = try builder.tryBuild()
|
|
|
|
} else {
|
|
|
|
profile = originalProfile
|
|
|
|
}
|
2024-10-04 18:58:11 +00:00
|
|
|
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .notice, "Save profile \(profile.id)...")
|
2024-11-03 22:35:45 +00:00
|
|
|
do {
|
2024-11-04 09:10:17 +00:00
|
|
|
let existingProfile = allProfiles[profile.id]
|
|
|
|
if existingProfile == nil || profile != existingProfile {
|
|
|
|
try await repository.saveProfile(profile)
|
|
|
|
if let backupRepository {
|
|
|
|
Task.detached {
|
|
|
|
try await backupRepository.saveProfile(profile)
|
|
|
|
}
|
2024-11-03 22:35:45 +00:00
|
|
|
}
|
2024-11-04 09:10:17 +00:00
|
|
|
didChange.send(.save(profile))
|
|
|
|
} else {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .notice, "\tProfile \(profile.id) not modified, not saving")
|
2024-10-04 18:58:11 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
} catch {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .fault, "\tUnable to save profile \(profile.id): \(error)")
|
2024-09-23 13:02:26 +00:00
|
|
|
throw error
|
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
if let remotelyShared, let remoteRepository {
|
|
|
|
do {
|
2024-11-09 14:20:59 +00:00
|
|
|
if remotelyShared {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .notice, "\tEnable remote sharing of profile \(profile.id)...")
|
2024-11-04 09:10:17 +00:00
|
|
|
try await remoteRepository.saveProfile(profile)
|
2024-10-04 18:58:11 +00:00
|
|
|
} else {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .notice, "\tDisable remote sharing of profile \(profile.id)...")
|
2024-11-04 09:10:17 +00:00
|
|
|
try await remoteRepository.removeProfiles(withIds: [profile.id])
|
2024-10-04 18:58:11 +00:00
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
} catch {
|
|
|
|
pp_log(.App.profiles, .fault, "\tUnable to save/remove remote profile \(profile.id): \(error)")
|
|
|
|
throw error
|
2024-10-04 18:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .notice, "Finished saving profile \(profile.id)")
|
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-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .notice, "Remove profiles \(profileIds)...")
|
2024-09-23 13:02:26 +00:00
|
|
|
do {
|
2024-10-10 14:20:36 +00:00
|
|
|
try await repository.removeProfiles(withIds: profileIds)
|
|
|
|
try? await remoteRepository?.removeProfiles(withIds: profileIds)
|
2024-09-30 12:56:20 +00:00
|
|
|
didChange.send(.remove(profileIds))
|
2024-09-23 13:02:26 +00:00
|
|
|
} catch {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .fault, "Unable to remove profiles \(profileIds): \(error)")
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func exists(withId profileId: Profile.ID) -> Bool {
|
2024-10-03 16:41:27 +00:00
|
|
|
allProfiles.keys.contains(profileId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-03 22:35:45 +00:00
|
|
|
// MARK: - Remote/Attributes
|
2024-10-03 16:41:27 +00:00
|
|
|
|
|
|
|
extension ProfileManager {
|
|
|
|
public func isRemotelyShared(profileWithId profileId: Profile.ID) -> Bool {
|
|
|
|
allRemoteProfiles.keys.contains(profileId)
|
|
|
|
}
|
|
|
|
|
2024-11-03 22:42:17 +00:00
|
|
|
public func isAvailableForTV(profileWithId profileId: Profile.ID) -> Bool {
|
|
|
|
profile(withId: profileId)?.attributes.isAvailableForTV == true
|
|
|
|
}
|
|
|
|
|
2024-10-04 18:58:11 +00:00
|
|
|
public func eraseRemotelySharedProfiles() async throws {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .notice, "Erase remotely shared profiles...")
|
2024-11-07 11:43:24 +00:00
|
|
|
try await remoteRepository?.removeAllProfiles()
|
2024-10-03 21:25:51 +00:00
|
|
|
}
|
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-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .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 {
|
2024-11-17 10:34:43 +00:00
|
|
|
let allNames = Set(allProfiles.values.map(\.name))
|
2024-09-30 12:56:20 +00:00
|
|
|
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 {
|
2024-11-17 10:34:43 +00:00
|
|
|
public func observeLocal() async throws {
|
|
|
|
localSubscription = nil
|
2024-11-10 16:51:28 +00:00
|
|
|
let initialProfiles = try await repository.fetchProfiles()
|
|
|
|
reloadLocalProfiles(initialProfiles)
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
localSubscription = repository
|
2024-10-10 14:20:36 +00:00
|
|
|
.profilesPublisher
|
2024-11-10 16:51:28 +00:00
|
|
|
.dropFirst()
|
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
|
|
|
}
|
2024-11-09 14:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
public func observeRemote(_ isRemoteImportingEnabled: Bool) async throws {
|
2024-11-09 14:20:59 +00:00
|
|
|
guard let remoteRepositoryBlock else {
|
|
|
|
// preconditionFailure("Missing remoteRepositoryBlock")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard remoteRepository == nil || isRemoteImportingEnabled != self.isRemoteImportingEnabled else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
self.isRemoteImportingEnabled = isRemoteImportingEnabled
|
2024-11-09 14:20:59 +00:00
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
remoteSubscription = nil
|
2024-11-11 11:08:22 +00:00
|
|
|
let newRepository = remoteRepositoryBlock(isRemoteImportingEnabled)
|
|
|
|
let initialProfiles = try await newRepository.fetchProfiles()
|
2024-11-13 11:11:29 +00:00
|
|
|
reloadRemoteProfiles(initialProfiles)
|
2024-11-11 11:08:22 +00:00
|
|
|
remoteRepository = newRepository
|
2024-10-04 18:58:11 +00:00
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
remoteSubscription = remoteRepository?
|
2024-11-09 14:20:59 +00:00
|
|
|
.profilesPublisher
|
2024-10-04 18:58:11 +00:00
|
|
|
.dropFirst()
|
2024-11-04 09:10:17 +00:00
|
|
|
.receive(on: DispatchQueue.main)
|
2024-09-30 12:56:20 +00:00
|
|
|
.sink { [weak self] in
|
2024-11-13 11:11:29 +00:00
|
|
|
self?.reloadRemoteProfiles($0)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProfileManager {
|
|
|
|
func observeSearch(debounce: Int = 200) {
|
|
|
|
searchSubscription = searchSubject
|
|
|
|
.debounce(for: .milliseconds(debounce), scheduler: DispatchQueue.main)
|
|
|
|
.sink { [weak self] in
|
|
|
|
self?.reloadFilteredProfiles(with: $0)
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
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-10 14:20:36 +00:00
|
|
|
func reloadLocalProfiles(_ result: [Profile]) {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .info, "Reload local profiles: \(result.map(\.id))")
|
2024-10-10 14:20:36 +00:00
|
|
|
allProfiles = result.reduce(into: [:]) {
|
2024-09-30 12:56:20 +00:00
|
|
|
$0[$1.id] = $1
|
|
|
|
}
|
2024-11-11 11:08:22 +00:00
|
|
|
if waitingObservers.contains(.local) {
|
2024-11-17 10:34:43 +00:00
|
|
|
waitingObservers.remove(.local) // @Published
|
2024-11-11 11:08:22 +00:00
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
deleteExcludedProfiles()
|
2024-11-03 22:35:45 +00:00
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
objectWillChange.send()
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 11:11:29 +00:00
|
|
|
func reloadRemoteProfiles(_ result: [Profile]) {
|
2024-11-10 16:51:28 +00:00
|
|
|
pp_log(.App.profiles, .info, "Reload remote profiles: \(result.map(\.id))")
|
2024-10-10 14:20:36 +00:00
|
|
|
allRemoteProfiles = result.reduce(into: [:]) {
|
2024-10-03 16:41:27 +00:00
|
|
|
$0[$1.id] = $1
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
2024-11-11 11:08:22 +00:00
|
|
|
if waitingObservers.contains(.remote) {
|
2024-11-17 10:34:43 +00:00
|
|
|
waitingObservers.remove(.remote) // @Published
|
2024-11-11 11:08:22 +00:00
|
|
|
}
|
2024-11-18 09:56:54 +00:00
|
|
|
Task { [weak self] in
|
|
|
|
await self?.importRemoteProfiles(result)
|
|
|
|
}
|
2024-11-03 22:35:45 +00:00
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
objectWillChange.send()
|
|
|
|
}
|
2024-11-06 17:42:42 +00:00
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
// should not be imported at all, but you never know
|
|
|
|
func deleteExcludedProfiles() {
|
|
|
|
guard let processor else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let idsToRemove: [Profile.ID] = allProfiles
|
|
|
|
.filter {
|
|
|
|
!processor.isIncluded($0.value)
|
|
|
|
}
|
|
|
|
.map(\.key)
|
2024-11-06 17:42:42 +00:00
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
if !idsToRemove.isEmpty {
|
|
|
|
pp_log(.App.profiles, .info, "Delete non-included local profiles: \(idsToRemove)")
|
|
|
|
Task.detached {
|
|
|
|
try await self.repository.removeProfiles(withIds: idsToRemove)
|
2024-11-06 17:42:42 +00:00
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-18 09:56:54 +00:00
|
|
|
func importRemoteProfiles(_ profiles: [Profile]) async {
|
2024-11-17 10:34:43 +00:00
|
|
|
guard !profiles.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-18 09:56:54 +00:00
|
|
|
if let previousTask = remoteImportTask {
|
|
|
|
pp_log(.App.profiles, .info, "Cancel ongoing remote import...")
|
|
|
|
previousTask.cancel()
|
|
|
|
await previousTask.value
|
|
|
|
remoteImportTask = nil
|
|
|
|
}
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.profiles, .info, "Start importing remote profiles: \(profiles.map(\.id)))")
|
|
|
|
assert(profiles.count == Set(profiles.map(\.id)).count, "Remote repository must not have duplicates")
|
|
|
|
|
|
|
|
pp_log(.App.profiles, .debug, "Local attributes:")
|
|
|
|
let localAttributes: [Profile.ID: ProfileAttributes] = allProfiles.values.reduce(into: [:]) {
|
|
|
|
$0[$1.id] = $1.attributes
|
|
|
|
pp_log(.App.profiles, .debug, "\t\($1.id) = \($1.attributes)")
|
|
|
|
}
|
|
|
|
pp_log(.App.profiles, .debug, "Remote attributes:")
|
|
|
|
let remoteAttributes: [Profile.ID: ProfileAttributes] = profiles.reduce(into: [:]) {
|
|
|
|
$0[$1.id] = $1.attributes
|
|
|
|
pp_log(.App.profiles, .debug, "\t\($1.id) = \($1.attributes)")
|
|
|
|
}
|
|
|
|
|
|
|
|
let remotelyDeletedIds = Set(allProfiles.keys).subtracting(Set(allRemoteProfiles.keys))
|
|
|
|
let mirrorsRemoteRepository = mirrorsRemoteRepository
|
|
|
|
|
|
|
|
remoteImportTask = Task.detached { [weak self] in
|
|
|
|
guard let self else {
|
|
|
|
return
|
2024-11-06 17:42:42 +00:00
|
|
|
}
|
|
|
|
|
2024-11-04 09:10:17 +00:00
|
|
|
var idsToRemove: [Profile.ID] = []
|
|
|
|
if !remotelyDeletedIds.isEmpty {
|
2024-11-09 14:20:59 +00:00
|
|
|
pp_log(.App.profiles, .info, "Will \(mirrorsRemoteRepository ? "delete" : "retain") local profiles not present in remote repository: \(remotelyDeletedIds)")
|
|
|
|
if mirrorsRemoteRepository {
|
2024-11-04 09:10:17 +00:00
|
|
|
idsToRemove.append(contentsOf: remotelyDeletedIds)
|
|
|
|
}
|
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
for remoteProfile in profiles {
|
2024-10-03 16:41:27 +00:00
|
|
|
do {
|
2024-11-18 16:43:01 +00:00
|
|
|
guard await processor?.isIncluded(remoteProfile) ?? true else {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .info, "Will delete non-included remote profile \(remoteProfile.id)")
|
2024-11-04 09:10:17 +00:00
|
|
|
idsToRemove.append(remoteProfile.id)
|
2024-11-03 22:35:45 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-11-13 20:46:14 +00:00
|
|
|
if let localFingerprint = localAttributes[remoteProfile.id]?.fingerprint {
|
|
|
|
guard let remoteFingerprint = remoteAttributes[remoteProfile.id]?.fingerprint,
|
|
|
|
remoteFingerprint != localFingerprint else {
|
|
|
|
pp_log(.App.profiles, .info, "Skip re-importing local profile \(remoteProfile.id)")
|
|
|
|
continue
|
|
|
|
}
|
2024-11-03 22:35:45 +00:00
|
|
|
}
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .notice, "Import remote profile \(remoteProfile.id)...")
|
2024-11-04 09:10:17 +00:00
|
|
|
try await save(remoteProfile)
|
2024-10-03 16:41:27 +00:00
|
|
|
} catch {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .error, "Unable to import remote profile: \(error)")
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
guard !Task.isCancelled else {
|
|
|
|
pp_log(.App.profiles, .info, "Cancelled import of remote profiles: \(profiles.map(\.id))")
|
|
|
|
return
|
|
|
|
}
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.profiles, .notice, "Finished importing remote profiles, delete stale profiles: \(idsToRemove)")
|
2024-11-17 10:34:43 +00:00
|
|
|
do {
|
|
|
|
try await repository.removeProfiles(withIds: idsToRemove)
|
|
|
|
} catch {
|
|
|
|
pp_log(.App.profiles, .error, "Unable to delete stale profiles: \(error)")
|
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
// yield a little bit
|
|
|
|
try? await Task.sleep(for: .milliseconds(100))
|
|
|
|
}
|
2024-11-18 09:56:54 +00:00
|
|
|
await remoteImportTask?.value
|
|
|
|
remoteImportTask = nil
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
|
|
|
|
2024-10-04 18:58:11 +00:00
|
|
|
func reloadFilteredProfiles(with search: String) {
|
2024-11-17 10:34:43 +00:00
|
|
|
filteredProfiles = allProfiles
|
2024-10-03 16:41:27 +00:00
|
|
|
.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
|
|
|
}
|
2024-11-17 10:34:43 +00:00
|
|
|
|
|
|
|
pp_log(.App.profiles, .notice, "Filter profiles with '\(search)' (\(filteredProfiles.count) results)")
|
|
|
|
|
|
|
|
objectWillChange.send()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|