75 lines
2.0 KiB
Swift
75 lines
2.0 KiB
Swift
//
|
|
// ActiveWorkoutSession.swift
|
|
// WorkoutsPlus
|
|
//
|
|
// Created by Felix Förtsch on 12.09.24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
// This view can only be viewed, if there exists a WorkoutSession that can be considered active (a person working out).
|
|
struct ActiveWorkoutSession: View {
|
|
@Default(\.isWorkingOut) var isWorkingOut
|
|
|
|
@Binding var activeWorkoutSession: WorkoutSession
|
|
|
|
var body: some View {
|
|
VStack {
|
|
List {
|
|
Section {
|
|
ForEach(activeWorkoutSession.workoutSessionItems.reversed()) { workoutSessionItem in
|
|
ActiveWorkoutSessionListItem(workoutSessionItem: workoutSessionItem)
|
|
}
|
|
}
|
|
}.listStyle(.plain)
|
|
|
|
VStack {
|
|
Text(activeWorkoutSession.name)
|
|
ActiveWorkoutSessionControls(session: $activeWorkoutSession)
|
|
}
|
|
.frame(height: 200)
|
|
}
|
|
.background(.bar)
|
|
// .navigationTitle(activeWorkoutSession.name)
|
|
// .navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
Button(action: {
|
|
isWorkingOut = false
|
|
activeWorkoutSession.isPaused.toggle()
|
|
}) {
|
|
HStack {
|
|
Image(systemName: "stop.fill")
|
|
Text("Stop")
|
|
}
|
|
}
|
|
.fontWeight(.bold)
|
|
.fontDesign(.rounded)
|
|
.tint(.red)
|
|
}
|
|
}
|
|
|
|
private func getActiveWorkoutItems(activeWorkout: Workout?) -> [WorkoutItem] {
|
|
guard let activeWorkout else { return [] }
|
|
return activeWorkout.getWorkoutItems()
|
|
}
|
|
|
|
// TODO: Put this somewhere general
|
|
private func getItem<Item: Nameable>(from array: [Item], by id: String) -> Item? {
|
|
let filteredItems = array.filter { $0.id == UUID(uuidString: id) }
|
|
return filteredItems.count == 1 ? filteredItems.first : nil
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
@Previewable @State var activeWorkoutSession = Workout.sampleData.first!.start()
|
|
|
|
NavigationStack {
|
|
ActiveWorkoutSession(activeWorkoutSession: $activeWorkoutSession)
|
|
}
|
|
.onAppear {
|
|
Defaults.shared.isWorkingOut = false
|
|
}
|
|
.modelContainer(SampleData.shared.modelContainer)
|
|
}
|