97 lines
2.5 KiB
Swift
97 lines
2.5 KiB
Swift
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct ActiveWorkoutSession: View {
|
|
@Environment(\.modelContext) private var modelContext
|
|
|
|
@Default(\.isWorkingOut) var isWorkingOut
|
|
@State var isTimerRunning: Bool = true
|
|
|
|
@Query(sort: \WorkoutSession.name) var workoutSessions: [WorkoutSession]
|
|
@Binding var activeWorkoutSession: WorkoutSession?
|
|
|
|
@Query(sort: \Workout.name) var workouts: [Workout]
|
|
@State var activeWorkout: Workout
|
|
|
|
var body: some View {
|
|
VStack {
|
|
List {
|
|
Section(header: Text("Workout")) {
|
|
Text(activeWorkout.name)
|
|
}
|
|
Section(header: Text("Exercises")) {
|
|
ForEach(getActiveWorkoutItems(activeWorkout: activeWorkout)) { workoutItem in
|
|
ActiveWorkoutSessionListItem(workoutItem: workoutItem)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Session")
|
|
.toolbar {
|
|
if (isWorkingOut) {
|
|
Button(action: {
|
|
isWorkingOut = false
|
|
activeWorkoutSession?.stop()
|
|
}) {
|
|
HStack {
|
|
Image(systemName: "stop.fill")
|
|
Text("Stop")
|
|
}
|
|
}
|
|
.bold()
|
|
.fontDesign(.rounded)
|
|
.tint(.red)
|
|
}
|
|
else {
|
|
Button(action: {
|
|
isWorkingOut = true
|
|
activeWorkoutSession = activeWorkout.start()
|
|
}) {
|
|
HStack {
|
|
Image(systemName: "play.fill")
|
|
Text("Start")
|
|
}
|
|
}
|
|
.bold()
|
|
.fontDesign(.rounded)
|
|
.tint(.green)
|
|
}
|
|
}
|
|
}
|
|
|
|
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: WorkoutSession?
|
|
@Previewable @State var workout = Workout.sampleData.first!
|
|
|
|
NavigationStack {
|
|
ActiveWorkoutSession(activeWorkoutSession: $activeWorkoutSession, activeWorkout: workout)
|
|
}
|
|
.onAppear {
|
|
Defaults.shared.isWorkingOut = false
|
|
}
|
|
.modelContainer(SampleData.shared.modelContainer)
|
|
}
|
|
|
|
//#Preview("Empty modelContainer") {
|
|
// @Previewable @State var activeWorkoutSession: WorkoutSession?
|
|
//
|
|
// NavigationStack {
|
|
// ActiveWorkoutSession(activeWorkoutSession: $activeWorkoutSession)
|
|
// }
|
|
// .onAppear {
|
|
// Defaults.shared.isWorkingOut = false
|
|
// }
|
|
//}
|