//
//  IntentDataProvider.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 25/01/2023.
//

import Foundation
import CoreData

class IntentDataProvider {
    
    static let main: IntentDataProvider = IntentDataProvider()
    
    func timers() throws -> [AbstractTimer] {
        let context = PersistenceController.shared.container.viewContext
        let request: NSFetchRequest<AbstractTimer> = AbstractTimer.fetchRequest()
        request.sortDescriptors = [NSSortDescriptor(keyPath: (\AbstractTimer.order), ascending: true)]
        return try context.fetch(request)
    }
    
    func timer(id: String) -> AbstractTimer? {
        let context = PersistenceController.shared.container.viewContext
        return context.object(stringId: id) as? AbstractTimer
    }
    
}
