// // ExerciseView.swift // Workouts // // Created by rzen on 7/18/25 at 5:44 PM. // // Copyright 2025 Rouslan Zenetl. All Rights Reserved. // import SwiftUI import SwiftData import Charts struct ExerciseView: View { @Environment(\.modelContext) private var modelContext @Environment(\.dismiss) private var dismiss @Bindable var workoutLog: WorkoutLog @State var allLogs: [WorkoutLog] var currentIndex: Int = 0 @State private var progress: Int = 0 @State private var navigateTo: WorkoutLog? = nil let notStartedColor = Color.white let completedColor = Color.green var body: some View { Form { Section(header: Text("Navigation")) { HStack { Button(action: navigateToPrevious) { HStack { Image(systemName: "chevron.left") Text("Previous") } } .disabled(currentIndex <= 0) Spacer() Text("\(currentIndex)") Spacer() Button(action: navigateToNext) { HStack { Text("Next") Image(systemName: "chevron.right") } } .disabled(currentIndex >= allLogs.count - 1) } .padding(.vertical, 8) } Section (header: Text("Progress")) { LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: workoutLog.sets), spacing: 2) { ForEach (1...workoutLog.sets, id: \.self) { index in ZStack { let completed = index <= progress let color = completed ? completedColor : notStartedColor RoundedRectangle(cornerRadius: 8) .fill( LinearGradient( gradient: Gradient(colors: [color, color.darker(by: 0.2)]), startPoint: .topLeading, endPoint: .bottomTrailing ) ) .aspectRatio(0.618, contentMode: .fit) .shadow(radius: 2) Text("\(index)") .foregroundColor(.primary) .colorInvert() } .onTapGesture { if progress == index { progress = 0 } else { progress = index } let _ = print("progress set to \(progress)") } } } } Section (header: Text("Plan")) { Stepper("\(workoutLog.sets) sets", value: $workoutLog.sets, in: 1...10) .font(.title) Stepper("\(workoutLog.reps) reps", value: $workoutLog.reps, in: 1...25) .font(.title) HStack { Text("\(workoutLog.weight) lbs") VStack (alignment: .trailing) { Stepper("", value: $workoutLog.weight, in: 1...200) Stepper("", value: $workoutLog.weight, in: 1...200, step: 5) } } .font(.title) } Section(header: Text("Progress Tracking")) { WeightProgressionChartView(exerciseName: workoutLog.exerciseName) } } .navigationTitle("\(workoutLog.exerciseName)") .navigationDestination(item: $navigateTo) { nextLog in ExerciseView( workoutLog: nextLog, allLogs: allLogs, currentIndex: allLogs.firstIndex(of: nextLog) ?? 0 ) } // .onAppear { // allLogs = modelContext.fetch(FetchDescriptor(sortBy: [ // SortDescriptor(\WorkoutLog.order), // SortDescriptor(\WorkoutLog.name) // ])) // } } private func navigateToPrevious() { guard currentIndex > 0 else { return } let previousIndex = currentIndex - 1 navigateTo = allLogs[previousIndex] } private func navigateToNext() { guard currentIndex < allLogs.count - 1 else { return } let nextIndex = currentIndex + 1 navigateTo = allLogs[nextIndex] } }