passepartout-apple/Passepartout/Library/Sources/LegacyV2/Strategy/CDProfileRepositoryV2.swift
Davide 114e1abe12
Add initial migration UI (#866)
Repurpose LegacyManager as MigrationManager. Present initial migration
UI from "+" menu in app home.

Different styles:

- iOS → Section / ForEach
- macOS → Table
2024-11-14 11:02:26 +01:00

130 lines
4.3 KiB
Swift

//
// CDProfileRepositoryV2.swift
// Passepartout
//
// Created by Davide De Rosa on 10/1/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
@preconcurrency import CoreData
import Foundation
import PassepartoutKit
final class CDProfileRepositoryV2: Sendable {
static var model: NSManagedObjectModel {
guard let model: NSManagedObjectModel = .mergedModel(from: [.module]) else {
fatalError("Unable to build Core Data model (Profiles v2)")
}
return model
}
private let context: NSManagedObjectContext
init(context: NSManagedObjectContext) {
self.context = context
}
func migratableProfiles() async throws -> [MigratableProfile] {
try await fetchProfiles(
prefetch: {
$0.propertiesToFetch = ["uuid", "name", "lastUpdate"]
},
map: {
$0.compactMap {
guard $0.value.encryptedJSON ?? $0.value.json != nil else {
pp_log(.App.migration, .error, "Unable to migrate profile \($0.key): missing JSON")
return nil
}
return MigratableProfile(
id: $0.key,
name: $0.value.name ?? $0.key.uuidString,
lastUpdate: $0.value.lastUpdate
)
}
}
)
}
func profile(withId profileId: UUID) async throws -> ProfileV2? {
try await profiles(withIds: [profileId]).first
}
func profiles(withIds profileIds: Set<UUID>?) async throws -> [ProfileV2] {
let decoder = JSONDecoder()
let profiles: [ProfileV2] = try await fetchProfiles(
prefetch: {
if let profileIds {
$0.predicate = NSPredicate(format: "any uuid in %@", profileIds)
}
},
map: {
$0.compactMap {
guard let json = $0.value.encryptedJSON ?? $0.value.json else {
pp_log(.App.migration, .error, "Unable to migrate profile \($0.key): missing JSON")
return nil
}
do {
return try decoder.decode(ProfileV2.self, from: json)
} catch {
pp_log(.App.migration, .error, "Unable to migrate profile \($0.key): \(error)")
return nil
}
}
}
)
return profiles
}
}
extension CDProfileRepositoryV2 {
func fetchProfiles<T>(
prefetch: ((NSFetchRequest<CDProfile>) -> Void)? = nil,
map: @escaping ([UUID: CDProfile]) -> [T]
) async throws -> [T] {
try await context.perform { [weak self] in
guard let self else {
return []
}
let request = CDProfile.fetchRequest()
request.sortDescriptors = [
.init(key: "lastUpdate", ascending: false)
]
prefetch?(request)
let existing = try context.fetch(request)
var deduped: [UUID: CDProfile] = [:]
existing.forEach {
guard let uuid = $0.uuid else {
return
}
guard !deduped.keys.contains(uuid) else {
pp_log(.App.migration, .info, "Skip older duplicate of profile \(uuid)")
return
}
deduped[uuid] = $0
}
return map(deduped)
}
}
}