// // 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 { @Environment(WorkoutStateManager.self) private var workoutStateManager @EnvironmentObject private var navigationManager: NavigationManager var body: some View { if let session = workoutStateManager.activeWorkoutSession { VStack { List { Section { ForEach(session.workoutSessionItems.reversed()) { workoutSessionItem in ActiveWorkoutSessionListItem(workoutSessionItem: workoutSessionItem) } } } .listStyle(.plain) ActiveWorkoutSessionControls() .frame(height: 200) } .background(.bar) .toolbar { if workoutStateManager.isWorkoutInProgress { Button(action: { workoutStateManager.stopWorkout() navigationManager.popToRoot() }) { HStack { Image(systemName: "stop.fill") Text("Stop") } } .fontWeight(.bold) .fontDesign(.rounded) .tint(.red) } } } else { ContentUnavailableView { Label("No Session in Progress ", systemImage: WorkoutSession.systemImage) } description: { Text("Start a session to track your progress.") } } } // .navigationTitle(session.name) // .navigationBarTitleDisplayMode(.inline) private func getActiveWorkoutItems(activeWorkout: Workout?) -> [WorkoutItem] { guard let activeWorkout else { return [] } return activeWorkout.getWorkoutItems() } private func getItem(from array: [Item], by id: String) -> Item? { let filteredItems = array.filter { $0.id == UUID(uuidString: id) } return filteredItems.count == 1 ? filteredItems.first : nil } } #Preview("Active Session") { @Previewable @State var stateManager = WorkoutStateManager(modelContext: SampleData.shared.modelContainer.mainContext) let _ = { stateManager.startWorkout(Workout.sampleData.first!) }() NavigationStack { ActiveWorkoutSession() } .modelContainer(SampleData.shared.modelContainer) .environment(stateManager) } #Preview("No Session (should never be displayed)") { @Previewable @State var stateManager = WorkoutStateManager(modelContext: SampleData.shared.modelContainer.mainContext) NavigationStack { ActiveWorkoutSession() } .modelContainer(SampleData.shared.modelContainer) .environment(stateManager) }