// // SplitExerciseAssignment.swift // Workouts // // Created by rzen on 7/15/25 at 7:12 AM. // // Copyright 2025 Rouslan Zenetl. All Rights Reserved. // import SwiftUI struct ExerciseAddEditView: View { @Environment(\.modelContext) private var modelContext @Environment(\.dismiss) private var dismiss @State private var showingExercisePicker = false @State var model: Exercise @State var originalWeight: Int? = nil var body: some View { NavigationStack { Form { Section(header: Text("Exercise")) { let exerciseName = model.name if exerciseName.isEmpty { Button(action: { showingExercisePicker = true }) { HStack { Text(model.name.isEmpty ? "Select Exercise" : model.name) Spacer() Image(systemName: "chevron.right") .foregroundColor(.gray) } } } else { ListItem(title: exerciseName) } } Section (header: Text("Sets/Reps")) { Stepper("Sets: \(model.sets)", value: $model.sets, in: 1...10) Stepper("Reps: \(model.reps)", value: $model.reps, in: 1...50) } // Weight section Section (header: Text("Weight")) { HStack { VStack(alignment: .center) { Text("\(model.weight) lbs") .font(.headline) } Spacer() VStack(alignment: .trailing) { Stepper("±1", value: $model.weight, in: 0...1000) Stepper("±5", value: $model.weight, in: 0...1000, step: 5) } .frame(width: 130) } } Section (header: Text("Weight Increase")) { HStack { Text("Remind every \(model.weightReminderTimeIntervalWeeks) weeks") Spacer() Stepper("", value: $model.weightReminderTimeIntervalWeeks, in: 0...366) } HStack { Text("Last weight change \(Date().humanTimeInterval(to: model.weightLastUpdated)) ago") } } } .onAppear { originalWeight = model.weight } .sheet(isPresented: $showingExercisePicker) { ExercisePickerView { exerciseNames in model.name = exerciseNames.first ?? "Exercise.unnamed" } } .navigationTitle(model.name.isEmpty ? "New Exercise" : model.name) .toolbar { ToolbarItem(placement: .navigationBarLeading) { Button("Cancel") { dismiss() } } ToolbarItem(placement: .navigationBarTrailing) { Button("Save") { if let originalWeight = originalWeight { if originalWeight != model.weight { model.weightLastUpdated = Date() } } try? modelContext.save() dismiss() } } } } } }