Added a timer view

This commit is contained in:
VincentMeilinger
2024-01-11 11:16:32 +01:00
parent 772ee6b0e0
commit b069555950
6 changed files with 171 additions and 1 deletions

View File

@@ -16,10 +16,12 @@ import UIKit
@Published var categories: [Category] = []
@Published var recipes: [String: [Recipe]] = [:]
@Published var recipeDetails: [Int: RecipeDetail] = [:]
@Published var timers: [String: RecipeTimer] = [:]
var recipeImages: [Int: [String: UIImage]] = [:]
var imagesNeedUpdate: [Int: [String: Bool]] = [:]
var lastUpdates: [String: Date] = [:]
private let api: CookbookApi.Type
private let dataStore: DataStore
@@ -608,3 +610,37 @@ extension DateFormatter {
return dateFormatter.string(from: date)
}
}
// Timer logic
extension MainViewModel {
func createTimer(forRecipe recipeId: String, timeTotal: Double) -> RecipeTimer {
let timer = RecipeTimer(timeTotal: timeTotal)
timers[recipeId] = timer
return timer
}
func getTimer(forRecipe recipeId: String, timeTotal: Double) -> RecipeTimer {
return timers[recipeId] ?? createTimer(forRecipe: recipeId, timeTotal: timeTotal)
}
func startTimer(forRecipe recipeId: String, timeTotal: Double) {
let timer = RecipeTimer(timeTotal: timeTotal)
timer.start()
timers[recipeId] = timer
}
func pauseTimer(forRecipe recipeId: String) {
timers[recipeId]?.pause()
}
func resumeTimer(forRecipe recipeId: String) {
timers[recipeId]?.resume()
}
func cancelTimer(forRecipe recipeId: String) {
timers[recipeId]?.cancel()
timers[recipeId] = nil
}
}