Impressia/CoreData/StatusDataHandler.swift

137 lines
5.3 KiB
Swift
Raw Normal View History

2023-01-01 18:13:36 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-01 18:13:36 +01:00
//
import Foundation
2023-01-02 08:11:38 +01:00
import CoreData
2023-02-19 10:32:38 +01:00
import PixelfedKit
2023-01-01 18:13:36 +01:00
class StatusDataHandler {
2023-01-05 11:55:20 +01:00
public static let shared = StatusDataHandler()
private init() { }
func getAllStatuses(accountId: String, viewContext: NSManagedObjectContext? = nil) -> [StatusData] {
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
2023-01-20 13:47:38 +01:00
let fetchRequest = StatusData.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: "id", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = NSPredicate(format: "pixelfedAccount.id = %@", accountId)
2023-01-20 13:47:38 +01:00
do {
return try context.fetch(fetchRequest)
} catch {
2023-03-11 18:30:33 +01:00
CoreDataError.shared.handle(error, message: "Error during fetching status (getStatusData).")
2023-01-20 13:47:38 +01:00
return []
}
}
func getAllOlderStatuses(accountId: String, statusId: String, viewContext: NSManagedObjectContext? = nil) -> [StatusData] {
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
let fetchRequest = StatusData.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: "id", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
let predicate1 = NSPredicate(format: "id < %@", statusId)
let predicate2 = NSPredicate(format: "pixelfedAccount.id = %@", accountId)
fetchRequest.predicate = NSCompoundPredicate.init(type: .and, subpredicates: [predicate1, predicate2])
do {
return try context.fetch(fetchRequest)
} catch {
CoreDataError.shared.handle(error, message: "Error during fetching status (getStatusData).")
return []
}
}
2023-04-14 16:50:47 +02:00
func getStatusData(accountId: String, statusId: String, viewContext: NSManagedObjectContext? = nil) -> StatusData? {
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
2023-01-05 11:55:20 +01:00
let fetchRequest = StatusData.fetchRequest()
2023-01-05 11:55:20 +01:00
fetchRequest.fetchLimit = 1
2023-01-11 13:16:43 +01:00
let predicate1 = NSPredicate(format: "id = %@", statusId)
let predicate2 = NSPredicate(format: "pixelfedAccount.id = %@", accountId)
fetchRequest.predicate = NSCompoundPredicate.init(type: .and, subpredicates: [predicate1, predicate2])
2023-01-05 11:55:20 +01:00
do {
return try context.fetch(fetchRequest).first
} catch {
2023-03-11 18:30:33 +01:00
CoreDataError.shared.handle(error, message: "Error during fetching status (getStatusData).")
2023-01-05 11:55:20 +01:00
return nil
}
}
2023-01-11 13:16:43 +01:00
func getMaximumStatus(accountId: String, viewContext: NSManagedObjectContext? = nil) -> StatusData? {
2023-01-02 08:11:38 +01:00
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
2023-01-01 18:13:36 +01:00
let fetchRequest = StatusData.fetchRequest()
fetchRequest.fetchLimit = 1
2023-01-01 18:13:36 +01:00
let sortDescriptor = NSSortDescriptor(key: "id", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
2023-01-11 13:16:43 +01:00
fetchRequest.predicate = NSPredicate(format: "pixelfedAccount.id = %@", accountId)
2023-01-01 18:13:36 +01:00
do {
let statuses = try context.fetch(fetchRequest)
return statuses.first
} catch {
2023-03-11 18:30:33 +01:00
CoreDataError.shared.handle(error, message: "Error during fetching maximum status (getMaximumStatus).")
2023-01-01 18:13:36 +01:00
return nil
}
}
2023-01-11 13:16:43 +01:00
func getMinimumStatus(accountId: String, viewContext: NSManagedObjectContext? = nil) -> StatusData? {
2023-01-02 08:11:38 +01:00
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
let fetchRequest = StatusData.fetchRequest()
fetchRequest.fetchLimit = 1
2023-01-02 08:11:38 +01:00
let sortDescriptor = NSSortDescriptor(key: "id", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
2023-01-11 13:16:43 +01:00
fetchRequest.predicate = NSPredicate(format: "pixelfedAccount.id = %@", accountId)
2023-01-02 08:11:38 +01:00
do {
let statuses = try context.fetch(fetchRequest)
return statuses.first
} catch {
2023-03-11 18:30:33 +01:00
CoreDataError.shared.handle(error, message: "Error during fetching minimum status (getMinimumtatus).")
2023-01-02 08:11:38 +01:00
return nil
}
}
func remove(accountId: String, statusId: String, viewContext: NSManagedObjectContext? = nil) {
2023-01-15 12:41:55 +01:00
let status = self.getStatusData(accountId: accountId, statusId: statusId)
guard let status else {
return
}
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
2023-01-15 12:41:55 +01:00
context.delete(status)
do {
try context.save()
} catch {
2023-03-11 18:30:33 +01:00
CoreDataError.shared.handle(error, message: "Error during deleting status (remove).")
2023-01-15 12:41:55 +01:00
}
}
2023-04-14 16:50:47 +02:00
func setFavourited(accountId: String, statusId: String) {
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
if let statusData = self.getStatusData(accountId: accountId, statusId: statusId, viewContext: backgroundContext) {
statusData.favourited = true
CoreDataHandler.shared.save(viewContext: backgroundContext)
}
}
2023-01-02 08:11:38 +01:00
func createStatusDataEntity(viewContext: NSManagedObjectContext? = nil) -> StatusData {
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
2023-01-01 18:13:36 +01:00
return StatusData(context: context)
}
}