Impressia/Vernissage/CoreData/StatusDataHandler.swift

77 lines
2.4 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.
// Licensed under the MIT License.
//
import Foundation
2023-01-02 08:11:38 +01:00
import CoreData
2023-01-05 11:55:20 +01:00
import MastodonSwift
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() { }
2023-01-01 18:13:36 +01:00
func getStatusesData() -> [StatusData] {
let context = CoreDataHandler.shared.container.viewContext
let fetchRequest = StatusData.fetchRequest()
do {
return try context.fetch(fetchRequest)
} catch {
print("Error during fetching accounts")
return []
}
}
2023-01-05 11:55:20 +01:00
func getStatusData(statusId: String) -> StatusData? {
let context = CoreDataHandler.shared.container.viewContext
let fetchRequest = StatusData.fetchRequest()
fetchRequest.fetchLimit = 1
fetchRequest.predicate = NSPredicate(format: "id = %@", statusId)
do {
return try context.fetch(fetchRequest).first
} catch {
print("Error during fetching accounts")
return nil
}
}
2023-01-02 08:11:38 +01:00
func getMaximumStatus(viewContext: NSManagedObjectContext? = nil) -> StatusData? {
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
2023-01-01 18:13:36 +01:00
let fetchRequest = StatusData.fetchRequest()
fetchRequest.fetchLimit = 1
let sortDescriptor = NSSortDescriptor(key: "id", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
do {
let statuses = try context.fetch(fetchRequest)
return statuses.first
} catch {
return nil
}
}
2023-01-02 08:11:38 +01:00
func getMinimumtatus(viewContext: NSManagedObjectContext? = nil) -> StatusData? {
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
let fetchRequest = StatusData.fetchRequest()
fetchRequest.fetchLimit = 1
let sortDescriptor = NSSortDescriptor(key: "id", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
do {
let statuses = try context.fetch(fetchRequest)
return statuses.first
} catch {
return nil
}
}
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)
}
}