106 lines
3.6 KiB
Swift
106 lines
3.6 KiB
Swift
//
|
||
// WorkoutAddEditView.swift
|
||
// Workouts
|
||
//
|
||
// Created by rzen on 7/13/25 at 9:13 PM.
|
||
//
|
||
// 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
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|