From 01e0559060bb494f9e85a769d9c9a2c07c7260b3 Mon Sep 17 00:00:00 2001 From: Davide Date: Sun, 17 Nov 2024 15:28:31 +0100 Subject: [PATCH] Fix some minor bugs - Observe profiles on Simulator - Dismiss on error when fetching migratable profiles - Improve migration logs ambiguity --- .../AppUIMain/Views/Migration/MigrateView.swift | 5 +++-- .../LegacyV2/Strategy/CDProfileRepositoryV2.swift | 10 +++++----- .../LegacyV2/Strategy/ProfileV2MigrationStrategy.swift | 2 +- Passepartout/Shared/AppContext+Shared.swift | 10 +++++----- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Passepartout/Library/Sources/AppUIMain/Views/Migration/MigrateView.swift b/Passepartout/Library/Sources/AppUIMain/Views/Migration/MigrateView.swift index bcd546c8..020c7771 100644 --- a/Passepartout/Library/Sources/AppUIMain/Views/Migration/MigrateView.swift +++ b/Passepartout/Library/Sources/AppUIMain/Views/Migration/MigrateView.swift @@ -149,8 +149,9 @@ private extension MigrateView { model.step = .fetched(model.profiles) } catch { pp_log(.App.migration, .error, "Unable to fetch migratable profiles: \(error)") - errorHandler.handle(error, title: title) - model.step = .initial + errorHandler.handle(error, title: title) { + dismiss() + } } } diff --git a/Passepartout/Library/Sources/LegacyV2/Strategy/CDProfileRepositoryV2.swift b/Passepartout/Library/Sources/LegacyV2/Strategy/CDProfileRepositoryV2.swift index 98355896..08012fbc 100644 --- a/Passepartout/Library/Sources/LegacyV2/Strategy/CDProfileRepositoryV2.swift +++ b/Passepartout/Library/Sources/LegacyV2/Strategy/CDProfileRepositoryV2.swift @@ -50,7 +50,7 @@ final class CDProfileRepositoryV2: Sendable { 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") + pp_log(.App.migration, .error, "ProfileV2 \($0.key) is not migratable: missing JSON") return nil } return MigratableProfile( @@ -78,13 +78,13 @@ final class CDProfileRepositoryV2: Sendable { 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") + pp_log(.App.migration, .error, "ProfileV2 \($0.key) is not migratable: 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)") + pp_log(.App.migration, .error, "Unable to decode ProfileV2 \($0.key): \(error)") return nil } } @@ -99,7 +99,7 @@ final class CDProfileRepositoryV2: Sendable { return } let request = CDProfile.fetchRequest() - request.predicate = NSPredicate(format: "any uuid in %@", profileIds.map(\.uuidString)) + request.predicate = NSPredicate(format: "any uuid in %@", profileIds) let existing = try context.fetch(request) existing.forEach(context.delete) try context.save() @@ -130,7 +130,7 @@ extension CDProfileRepositoryV2 { return } guard !deduped.keys.contains(uuid) else { - pp_log(.App.migration, .info, "Skip older duplicate of profile \(uuid)") + pp_log(.App.migration, .info, "Skip older duplicate of ProfileV2 \(uuid)") return } deduped[uuid] = $0 diff --git a/Passepartout/Library/Sources/LegacyV2/Strategy/ProfileV2MigrationStrategy.swift b/Passepartout/Library/Sources/LegacyV2/Strategy/ProfileV2MigrationStrategy.swift index 7284b647..28eaa167 100644 --- a/Passepartout/Library/Sources/LegacyV2/Strategy/ProfileV2MigrationStrategy.swift +++ b/Passepartout/Library/Sources/LegacyV2/Strategy/ProfileV2MigrationStrategy.swift @@ -67,7 +67,7 @@ extension ProfileV2MigrationStrategy { } return try mapper.toProfileV3(profile) } catch { - pp_log(.App.migration, .error, "Unable to migrate profile \(profileId): \(error)") + pp_log(.App.migration, .error, "Unable to fetch and map migratable profile \(profileId): \(error)") return nil } } diff --git a/Passepartout/Shared/AppContext+Shared.swift b/Passepartout/Shared/AppContext+Shared.swift index daf0e151..f83e8d84 100644 --- a/Passepartout/Shared/AppContext+Shared.swift +++ b/Passepartout/Shared/AppContext+Shared.swift @@ -136,7 +136,7 @@ private extension Configuration { @MainActor private extension Configuration.ProfileManager { static var mainProfileRepository: ProfileRepository { - coreDataProfileRepository + coreDataProfileRepository(observingResults: true) } static var backupProfileRepository: ProfileRepository? { @@ -161,7 +161,7 @@ private extension Configuration.ProfileManager { } static var backupProfileRepository: ProfileRepository? { - coreDataProfileRepository + coreDataProfileRepository(observingResults: false) } } @@ -192,7 +192,7 @@ private extension Configuration.ProfileManager { ) }() - static let coreDataProfileRepository: ProfileRepository = { + static func coreDataProfileRepository(observingResults: Bool) -> ProfileRepository { let store = CoreDataPersistentStore( logger: .default, containerName: Constants.shared.containers.local, @@ -204,12 +204,12 @@ private extension Configuration.ProfileManager { registry: .shared, coder: CodableProfileCoder(), context: store.context, - observingResults: false + observingResults: observingResults ) { error in pp_log(.app, .error, "Unable to decode local result: \(error)") return .ignore } - }() + } } // MARK: - Logging