From 41028941602356202bcd83c32f798dd3cefad140 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sat, 5 Aug 2023 22:41:58 +0200 Subject: [PATCH] Always read profiles from fetched value (#340) There may be a mismatch between profiles and profile headers. For example, some profiles may appear in the main list, but the same ones may not appear in the existing profile names when adding a new profile (and vice versa). That's because they are being fetched from different sources. Unify that. --- .../Managers/ProfileManager+Extensions.swift | 2 +- .../Managers/ProfileManager.swift | 4 +- .../Strategies/ProfileRepository.swift | 2 - .../Strategies/CDProfileRepository.swift | 39 +------------------ 4 files changed, 4 insertions(+), 43 deletions(-) diff --git a/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/ProfileManager+Extensions.swift b/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/ProfileManager+Extensions.swift index bd8f48e0..785a1b63 100644 --- a/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/ProfileManager+Extensions.swift +++ b/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/ProfileManager+Extensions.swift @@ -28,7 +28,7 @@ import PassepartoutCore extension ProfileManager { public var hasProfiles: Bool { - !profiles.isEmpty + !allProfiles.isEmpty } public var activeProfile: Profile? { diff --git a/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/ProfileManager.swift b/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/ProfileManager.swift index 25327e28..ed3b06a7 100644 --- a/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/ProfileManager.swift +++ b/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/ProfileManager.swift @@ -113,12 +113,12 @@ public final class ProfileManager: ObservableObject { // MARK: Index extension ProfileManager { - private var allProfiles: [UUID: Profile] { + public var allProfiles: [UUID: Profile] { profileRepository.allProfiles() } public var profiles: [Profile] { - profileRepository.profiles() + Array(allProfiles.values) } public var headers: [Profile.Header] { diff --git a/PassepartoutLibrary/Sources/PassepartoutVPN/Strategies/ProfileRepository.swift b/PassepartoutLibrary/Sources/PassepartoutVPN/Strategies/ProfileRepository.swift index f1da941f..fefe0707 100644 --- a/PassepartoutLibrary/Sources/PassepartoutVPN/Strategies/ProfileRepository.swift +++ b/PassepartoutLibrary/Sources/PassepartoutVPN/Strategies/ProfileRepository.swift @@ -30,8 +30,6 @@ import PassepartoutCore public protocol ProfileRepository { func allProfiles() -> [UUID: Profile] - func profiles() -> [Profile] - func profile(withId id: UUID) -> Profile? func saveProfiles(_ profiles: [Profile]) throws diff --git a/PassepartoutLibrary/Sources/PassepartoutVPNImpl/Strategies/CDProfileRepository.swift b/PassepartoutLibrary/Sources/PassepartoutVPNImpl/Strategies/CDProfileRepository.swift index 6557bd56..8d524a62 100644 --- a/PassepartoutLibrary/Sources/PassepartoutVPNImpl/Strategies/CDProfileRepository.swift +++ b/PassepartoutLibrary/Sources/PassepartoutVPNImpl/Strategies/CDProfileRepository.swift @@ -44,45 +44,8 @@ final class CDProfileRepository: ProfileRepository { observableProfiles.value } - func profiles() -> [Profile] { - let request = CDProfile.fetchRequest() - request.sortDescriptors = [ - .init(keyPath: \CDProfile.lastUpdate, ascending: true) - ] - do { - let results = try context.fetch(request) - let map = try results.reduce(into: [UUID: Profile]()) { - guard let profile = try ProfileMapper.toModel($1) else { - return - } - $0[profile.id] = profile - } - return Array(map.values) - } catch { - pp_log.error("Unable to fetch profiles: \(error)") - return [] - } - } - func profile(withId id: UUID) -> Profile? { - let request = CDProfile.fetchRequest() - request.predicate = NSPredicate( - format: "uuid == %@", - id.uuidString - ) - request.sortDescriptors = [ - .init(keyPath: \CDProfile.lastUpdate, ascending: false) - ] - do { - let results = try context.fetch(request) - guard let recent = results.first else { - return nil - } - return try ProfileMapper.toModel(recent) - } catch { - pp_log.error("Unable to fetch profile: \(error)") - return nil - } + observableProfiles.value[id] } func saveProfiles(_ profiles: [Profile]) throws {