Files
workouts/Workouts/Views/WorkoutLog/WorkoutLogEditView.swift
2025-07-25 17:42:25 -04:00

106 lines
3.6 KiB
Swift
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// WorkoutAddEditView.swift
// Workouts
//
// Created by rzen on 7/13/25 at 9:13PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
import SwiftData
struct WorkoutLogEditView: View {
@Environment(\.modelContext) private var modelContext
@Environment(\.dismiss) private var dismiss
@State var workoutLog: WorkoutLog
@State private var showingSaveConfirmation = false
var body: some View {
NavigationStack {
Form {
Section (header: Text("Exercise")) {
Text(workoutLog.exerciseName)
.font(.headline)
}
Section(header: Text("Sets/Reps")) {
Stepper("Sets: \(workoutLog.sets)", value: $workoutLog.sets, in: 1...10)
Stepper("Reps: \(workoutLog.reps)", value: $workoutLog.reps, in: 1...50)
}
Section(header: Text("Weight")) {
HStack {
VStack(alignment: .center) {
Text("\(workoutLog.weight) lbs")
.font(.headline)
}
Spacer()
VStack(alignment: .trailing) {
Stepper("±1", value: $workoutLog.weight, in: 0...1000)
Stepper("±5", value: $workoutLog.weight, in: 0...1000, step: 5)
}
.frame(width: 130)
}
}
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
dismiss()
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("Save") {
showingSaveConfirmation = true
}
}
}
.confirmationDialog("Save Options", isPresented: $showingSaveConfirmation) {
Button("Save Workout Log Only") {
try? modelContext.save()
dismiss()
}
Button("Save Workout Log and Update Split") {
// Save the workout log
try? modelContext.save()
// Update the split with this workout log's data
// Note: Implementation depends on how splits are updated in your app
updateSplit(from: workoutLog)
dismiss()
}
Button("Cancel", role: .cancel) {
// Do nothing, dialog will dismiss
}
}
}
}
private func updateSplit(from workoutLog: WorkoutLog) {
let split = workoutLog.workout?.split
// Find the matching exercise in split.exercises by name
if let exercises = split?.exercises {
for exercise in exercises {
if exercise.name == workoutLog.exerciseName {
// Update the sets, reps, and weight in the split exercise assignment
exercise.sets = workoutLog.sets
exercise.reps = workoutLog.reps
exercise.weight = workoutLog.weight
// Save the changes to the split
try? modelContext.save()
break
}
}
}
}
}