109 lines
4.0 KiB
Swift
109 lines
4.0 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
|
||
|
||
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
|
||
|
||
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: .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
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|