Improved timer widget

This commit is contained in:
VincentMeilinger
2024-01-11 14:15:12 +01:00
parent b069555950
commit 9b225f63b5
5 changed files with 90 additions and 44 deletions

View File

@@ -61,8 +61,8 @@ struct RecipeDetailView: View {
}
Divider()
TimerView(timer: viewModel.getTimer(forRecipe: recipeDetail.id, timeTotal: 20))
RecipeDurationSection(recipeDetail: recipeDetail)
RecipeDurationSection(viewModel: viewModel, recipeDetail: recipeDetail)
LazyVGrid(columns: [GridItem(.adaptive(minimum: 400), alignment: .top)]) {
if(!recipeDetail.recipeIngredient.isEmpty) {
@@ -214,10 +214,11 @@ fileprivate struct ShareView: View {
fileprivate struct RecipeDurationSection: View {
@ObservedObject var viewModel: MainViewModel
@State var recipeDetail: RecipeDetail
var body: some View {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 150), alignment: .leading)]) {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 250), alignment: .leading)]) {
if let prepTime = recipeDetail.prepTime, let time = DurationComponents.ptToText(prepTime) {
VStack(alignment: .leading) {
HStack {
@@ -229,6 +230,12 @@ fileprivate struct RecipeDurationSection: View {
}.padding()
}
if let cookTime = recipeDetail.cookTime, let time = DurationComponents.ptToText(cookTime) {
TimerView(timer: viewModel.getTimer(forRecipe: recipeDetail.id, duration: DurationComponents.fromPTString(cookTime)))
.padding()
}
/*
if let cookTime = recipeDetail.cookTime, let time = DurationComponents.ptToText(cookTime) {
VStack(alignment: .leading) {
HStack {
@@ -238,7 +245,7 @@ fileprivate struct RecipeDurationSection: View {
Text(time)
.lineLimit(1)
}.padding()
}
}*/
if let totalTime = recipeDetail.totalTime, let time = DurationComponents.ptToText(totalTime) {
VStack(alignment: .leading) {

View File

@@ -27,29 +27,27 @@ struct TimerView: View {
} label: {
if timer.isRunning {
Image(systemName: "pause.fill")
.foregroundStyle(.blue)
} else {
Image(systemName: "play.fill")
.foregroundStyle(.blue)
}
}
}
.gaugeStyle(.accessoryCircularCapacity)
.animation(.easeInOut, value: timer.timeElapsed)
.tint(.white)
.tint(timer.isRunning ? .green : .nextcloudBlue)
.foregroundStyle(timer.isRunning ? Color.green : Color.nextcloudBlue)
VStack(alignment: .leading) {
HStack {
Text("Cooking time")
.padding(.horizontal)
Button {
timer.cancel()
} label: {
Image(systemName: "xmark.circle.fill")
}
}
Text("\(Int(timer.timeTotal - timer.timeElapsed))")
.padding(.horizontal)
Text("Cooking")
Text(timer.duration.toTimerText())
}
.padding(.horizontal)
Button {
timer.cancel()
} label: {
Image(systemName: "xmark.circle.fill")
.foregroundStyle(timer.isRunning ? Color.nextcloudBlue : Color.secondary)
}
}
.bold()
@@ -65,6 +63,7 @@ struct TimerView: View {
class RecipeTimer: ObservableObject {
var timeTotal: Double
@Published var duration: DurationComponents
private var startDate: Date?
private var pauseDate: Date?
@Published var timeElapsed: Double = 0
@@ -72,8 +71,9 @@ class RecipeTimer: ObservableObject {
private var timer: Timer.TimerPublisher?
private var timerCancellable: Cancellable?
init(timeTotal: Double) {
self.timeTotal = timeTotal
init(duration: DurationComponents) {
self.duration = duration
self.timeTotal = duration.toSeconds()
}
func start() {
@@ -93,8 +93,10 @@ class RecipeTimer: ObservableObject {
let elapsed = Date().timeIntervalSince(startTime)
if elapsed < self.timeTotal {
self.timeElapsed = elapsed
self.duration.fromSeconds(Int(self.timeTotal - self.timeElapsed))
} else {
self.timeElapsed = self.timeTotal
self.duration.fromSeconds(Int(self.timeTotal - self.timeElapsed))
self.pause()
}
}
@@ -123,5 +125,6 @@ class RecipeTimer: ObservableObject {
self.timeElapsed = 0
self.startDate = nil
self.pauseDate = nil
self.duration.fromSeconds(Int(timeTotal))
}
}