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.
This commit is contained in:
Davide De Rosa 2023-08-05 22:41:58 +02:00 committed by GitHub
parent 7a74525149
commit 4102894160
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 4 additions and 43 deletions

View File

@ -28,7 +28,7 @@ import PassepartoutCore
extension ProfileManager { extension ProfileManager {
public var hasProfiles: Bool { public var hasProfiles: Bool {
!profiles.isEmpty !allProfiles.isEmpty
} }
public var activeProfile: Profile? { public var activeProfile: Profile? {

View File

@ -113,12 +113,12 @@ public final class ProfileManager: ObservableObject {
// MARK: Index // MARK: Index
extension ProfileManager { extension ProfileManager {
private var allProfiles: [UUID: Profile] { public var allProfiles: [UUID: Profile] {
profileRepository.allProfiles() profileRepository.allProfiles()
} }
public var profiles: [Profile] { public var profiles: [Profile] {
profileRepository.profiles() Array(allProfiles.values)
} }
public var headers: [Profile.Header] { public var headers: [Profile.Header] {

View File

@ -30,8 +30,6 @@ import PassepartoutCore
public protocol ProfileRepository { public protocol ProfileRepository {
func allProfiles() -> [UUID: Profile] func allProfiles() -> [UUID: Profile]
func profiles() -> [Profile]
func profile(withId id: UUID) -> Profile? func profile(withId id: UUID) -> Profile?
func saveProfiles(_ profiles: [Profile]) throws func saveProfiles(_ profiles: [Profile]) throws

View File

@ -44,45 +44,8 @@ final class CDProfileRepository: ProfileRepository {
observableProfiles.value 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? { func profile(withId id: UUID) -> Profile? {
let request = CDProfile.fetchRequest() observableProfiles.value[id]
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
}
} }
func saveProfiles(_ profiles: [Profile]) throws { func saveProfiles(_ profiles: [Profile]) throws {