2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// ProfileRepository.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/19/22.
|
|
|
|
// Copyright (c) 2022 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 Foundation
|
|
|
|
import CoreData
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutCore
|
2022-04-12 13:09:14 +00:00
|
|
|
import PassepartoutUtils
|
|
|
|
|
|
|
|
class ProfileRepository: Repository {
|
|
|
|
private let context: NSManagedObjectContext
|
|
|
|
|
|
|
|
required init(_ context: NSManagedObjectContext) {
|
|
|
|
self.context = context
|
|
|
|
}
|
|
|
|
|
2022-04-16 09:21:33 +00:00
|
|
|
func fetchedHeaders() -> FetchedValueHolder<[UUID: Profile.Header]> {
|
2022-04-12 13:09:14 +00:00
|
|
|
let request: NSFetchRequest<NSFetchRequestResult> = CDProfile.fetchRequest()
|
|
|
|
request.sortDescriptors = [
|
|
|
|
.init(keyPath: \CDProfile.uuid, ascending: true),
|
|
|
|
.init(keyPath: \CDProfile.lastUpdate, ascending: false)
|
|
|
|
]
|
|
|
|
request.propertiesToFetch = [
|
|
|
|
"uuid",
|
2022-07-14 15:06:07 +00:00
|
|
|
"lastUpdate",
|
2022-04-12 13:09:14 +00:00
|
|
|
"name",
|
|
|
|
"providerName"
|
|
|
|
]
|
|
|
|
return .init(
|
|
|
|
context: context,
|
|
|
|
request: request,
|
|
|
|
mapping: {
|
|
|
|
$0.reduce(into: [UUID: Profile.Header]()) {
|
|
|
|
guard let dto = $1 as? CDProfile else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let header = ProfileHeaderMapper.toModel(dto) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$0[header.id] = header
|
|
|
|
}
|
|
|
|
},
|
|
|
|
initial: [:]
|
|
|
|
)
|
|
|
|
}
|
2022-07-14 15:06:33 +00:00
|
|
|
|
|
|
|
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 []
|
|
|
|
}
|
|
|
|
}
|
2022-04-16 09:21:33 +00:00
|
|
|
|
|
|
|
func profile(withId id: UUID) -> Profile? {
|
|
|
|
let request = CDProfile.fetchRequest()
|
2022-04-12 13:09:14 +00:00
|
|
|
request.predicate = NSPredicate(
|
|
|
|
format: "uuid == %@",
|
|
|
|
id.uuidString
|
|
|
|
)
|
2022-05-05 08:55:40 +00:00
|
|
|
request.sortDescriptors = [
|
|
|
|
.init(keyPath: \CDProfile.lastUpdate, ascending: false)
|
|
|
|
]
|
2022-04-16 09:21:33 +00:00
|
|
|
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
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func saveProfiles(_ profiles: [Profile]) throws {
|
|
|
|
let request = CDProfile.fetchRequest()
|
|
|
|
request.predicate = NSPredicate(
|
|
|
|
format: "uuid in %@ OR name in %@", // replace with same name
|
|
|
|
profiles.map(\.id.uuidString),
|
|
|
|
profiles.map(\.header.name)
|
|
|
|
)
|
|
|
|
do {
|
|
|
|
// dedup
|
|
|
|
let existing = try context.fetch(request)
|
|
|
|
existing.forEach(context.delete)
|
|
|
|
|
|
|
|
try profiles.forEach {
|
|
|
|
_ = try ProfileMapper(context).toDTO($0)
|
|
|
|
}
|
|
|
|
try context.save()
|
|
|
|
} catch {
|
|
|
|
context.rollback()
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeProfiles(withIds ids: [UUID]) {
|
|
|
|
let request = CDProfile.fetchRequest()
|
|
|
|
request.predicate = NSPredicate(
|
|
|
|
format: "any uuid in %@",
|
|
|
|
ids.map(\.uuidString)
|
|
|
|
)
|
|
|
|
do {
|
|
|
|
try context.fetch(request).forEach {
|
|
|
|
context.delete($0)
|
|
|
|
}
|
|
|
|
try context.save()
|
|
|
|
} catch {
|
|
|
|
pp_log.error("Unable to remove profiles: \(error)")
|
|
|
|
context.rollback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|