146 lines
5.5 KiB
Swift
146 lines
5.5 KiB
Swift
//
|
||
// SplitExercisesListView.swift
|
||
// Workouts
|
||
//
|
||
// Created by rzen on 7/18/25 at 8:38 AM.
|
||
//
|
||
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
|
||
//
|
||
|
||
import SwiftUI
|
||
import SwiftData
|
||
|
||
struct SplitExercisesListView: View {
|
||
@Environment(\.modelContext) private var modelContext
|
||
@Environment(\.dismiss) private var dismiss
|
||
|
||
var model: Split
|
||
|
||
@State private var showingAddSheet: Bool = false
|
||
@State private var itemToEdit: SplitExerciseAssignment? = nil
|
||
@State private var itemToDelete: SplitExerciseAssignment? = nil
|
||
@State private var createdWorkout: Workout? = nil
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
Form {
|
||
List {
|
||
if let assignments = model.exercises, !assignments.isEmpty {
|
||
let sortedAssignments = assignments.sorted(by: { $0.order == $1.order ? $0.exerciseName < $1.exerciseName : $0.order < $1.order })
|
||
|
||
ForEach(sortedAssignments) { item in
|
||
ListItem(
|
||
title: item.exerciseName,
|
||
subtitle: "\(item.sets) × \(item.reps) × \(item.weight) lbs \(item.order)"
|
||
)
|
||
.swipeActions {
|
||
Button {
|
||
itemToDelete = item
|
||
} label: {
|
||
Label("Delete", systemImage: "circle")
|
||
}
|
||
Button {
|
||
itemToEdit = item
|
||
} label: {
|
||
Label("Edit", systemImage: "pencil")
|
||
}
|
||
.tint(.indigo)
|
||
}
|
||
}
|
||
.onMove(perform: { indices, destination in
|
||
var exerciseArray = Array(sortedAssignments)
|
||
exerciseArray.move(fromOffsets: indices, toOffset: destination)
|
||
for (index, exercise) in exerciseArray.enumerated() {
|
||
exercise.order = index
|
||
}
|
||
if let modelContext = exerciseArray.first?.modelContext {
|
||
do {
|
||
try modelContext.save()
|
||
} catch {
|
||
print("Error saving after reordering: \(error)")
|
||
}
|
||
}
|
||
})
|
||
} else {
|
||
Text("No exercises added yet.")
|
||
}
|
||
}
|
||
}
|
||
.navigationTitle("\(model.name)")
|
||
}
|
||
.toolbar {
|
||
ToolbarItem(placement: .primaryAction) {
|
||
Button("Start This Split") {
|
||
let split = model
|
||
let workout = Workout(start: Date(), split: split)
|
||
modelContext.insert(workout)
|
||
if let exercises = split.exercises {
|
||
for assignment in exercises {
|
||
let workoutLog = WorkoutLog(
|
||
workout: workout,
|
||
exerciseName: assignment.exerciseName,
|
||
date: Date(),
|
||
order: assignment.order,
|
||
sets: assignment.sets,
|
||
reps: assignment.reps,
|
||
weight: assignment.weight
|
||
)
|
||
modelContext.insert(workoutLog)
|
||
}
|
||
}
|
||
try? modelContext.save()
|
||
|
||
// Set the created workout to trigger navigation
|
||
createdWorkout = workout
|
||
}
|
||
}
|
||
}
|
||
.navigationDestination(item: $createdWorkout, destination: { workout in
|
||
WorkoutLogView(workout: workout)
|
||
})
|
||
// .sheet(item: $createdWorkout) { workout in
|
||
// NavigationStack {
|
||
// WorkoutLogView(workout: workout)
|
||
// }
|
||
// }
|
||
// .toolbar {
|
||
// ToolbarItem(placement: .navigationBarTrailing) {
|
||
// Button(action: { showingAddSheet.toggle() }) {
|
||
// Image(systemName: "plus")
|
||
// }
|
||
// }
|
||
// }
|
||
// .sheet (isPresented: $showingAddSheet) {
|
||
// ExercisePickerView { exerciseName in
|
||
// itemToEdit = SplitExerciseAssignment(
|
||
// split: model,
|
||
// exerciseName: exerciseName,
|
||
// order: 0,
|
||
// sets: 3,
|
||
// reps: 10,
|
||
// weight: 40
|
||
// )
|
||
// }
|
||
// }
|
||
.sheet(item: $itemToEdit) { item in
|
||
SplitExerciseAssignmentAddEditView(model: item)
|
||
}
|
||
.confirmationDialog(
|
||
"Delete Exercise?",
|
||
isPresented: .constant(itemToDelete != nil),
|
||
titleVisibility: .visible
|
||
) {
|
||
Button("Delete", role: .destructive) {
|
||
if let item = itemToDelete {
|
||
withAnimation {
|
||
modelContext.delete(item)
|
||
try? modelContext.save()
|
||
itemToDelete = nil
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|