Add ProfileManager methods to return full profiles

This commit is contained in:
Davide De Rosa 2022-07-14 17:06:33 +02:00
parent 9d9dc2b2f6
commit 7771b50b56
5 changed files with 27 additions and 0 deletions

View File

@ -39,6 +39,8 @@ public protocol ProfileManager {
var headers: [Profile.Header] { get } var headers: [Profile.Header] { get }
var profiles: [Profile] { get }
func isExistingProfile(withId id: UUID) -> Bool func isExistingProfile(withId id: UUID) -> Bool
func isExistingProfile(withName name: String) -> Bool func isExistingProfile(withName name: String) -> Bool

View File

@ -42,6 +42,10 @@ public class CoreDataProfileManagerStrategy: ProfileManagerStrategy {
fetchedHeaders.value fetchedHeaders.value
} }
public func profiles() -> [Profile] {
profileRepository.profiles()
}
public func profile(withId id: UUID) -> Profile? { public func profile(withId id: UUID) -> Profile? {
profileRepository.profile(withId: id) profileRepository.profile(withId: id)
} }

View File

@ -117,6 +117,10 @@ extension DefaultProfileManager {
Array(allHeaders.values) Array(allHeaders.values)
} }
public var profiles: [Profile] {
strategy.profiles()
}
public func isExistingProfile(withId id: UUID) -> Bool { public func isExistingProfile(withId id: UUID) -> Bool {
allHeaders[id] != nil allHeaders[id] != nil
} }

View File

@ -30,6 +30,8 @@ import PassepartoutCore
public protocol ProfileManagerStrategy { public protocol ProfileManagerStrategy {
var allHeaders: [UUID: Profile.Header] { get } var allHeaders: [UUID: Profile.Header] { get }
func profiles() -> [Profile]
func profile(withId: UUID) -> Profile? func profile(withId: UUID) -> Profile?
func saveProfiles(_ profiles: [Profile]) func saveProfiles(_ profiles: [Profile])

View File

@ -64,6 +64,21 @@ class ProfileRepository: Repository {
initial: [:] initial: [:]
) )
} }
func profiles() -> [Profile] {
let request = CDProfile.fetchRequest()
request.sortDescriptors = [
.init(keyPath: \CDProfile.uuid, ascending: true),
.init(keyPath: \CDProfile.lastUpdate, ascending: false)
]
do {
let results = try context.fetch(request)
return try results.compactMap(ProfileMapper.toModel)
} 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() let request = CDProfile.fetchRequest()