115 lines
2.9 KiB
Swift
115 lines
2.9 KiB
Swift
//
|
|
// WorkoutDetail.swift
|
|
// WorkoutsPlus
|
|
//
|
|
// Created by Felix Förtsch on 10.08.24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct WorkoutDetail: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
@Environment(\.modelContext) private var modelContext
|
|
|
|
@State var workout: Workout
|
|
@State private var isPresentingExerciseLibrary = false
|
|
|
|
var body: some View {
|
|
|
|
Form {
|
|
Section {
|
|
NavigationLink(destination: WorkoutIconSelector(workout: workout)) {
|
|
Image(systemName: workout.workoutIconSystemName)
|
|
.foregroundStyle(workout.workoutIconColorName.color)
|
|
TextField("Workout Name", text: $workout.name)
|
|
}
|
|
}
|
|
Section(header: Text("Exercises")) {
|
|
List {
|
|
ForEach(workout.workoutItems
|
|
.sorted(by: { $0.position < $1.position})) { workoutItem in
|
|
switch workoutItem.workoutItemType {
|
|
case .exercise:
|
|
ExerciseListItem(workout, workoutItem)
|
|
case .set:
|
|
SetListItem(workout, workoutItem)
|
|
case .workout:
|
|
Text(workoutItem.name)
|
|
}
|
|
}
|
|
.onDelete(perform: deleteExerciseFromWorkout)
|
|
.onMove(perform: move)
|
|
}
|
|
.environment(\.editMode, .constant(.active)) // Always active drag mode
|
|
AddItemButton(label: "Exercise", action: addWorkoutItemToWorkout)
|
|
}
|
|
}
|
|
.navigationBarTitle("Edit \(workout.name)")
|
|
.toolbar {
|
|
ToolbarItem() {
|
|
EditButton()
|
|
}
|
|
}
|
|
.sheet(isPresented: $isPresentingExerciseLibrary) {
|
|
AddWorkoutItemToWorkout(workout: workout)
|
|
.presentationDetents([.medium, .large])
|
|
.presentationDragIndicator(.visible)
|
|
}
|
|
|
|
}
|
|
|
|
private func addWorkoutItemToWorkout() {
|
|
withAnimation {
|
|
isPresentingExerciseLibrary = true
|
|
}
|
|
}
|
|
|
|
private func saveWorkout() {
|
|
if modelContext.hasChanges {
|
|
do {
|
|
try modelContext.save()
|
|
} catch {
|
|
print("Failed to save workout: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
}
|
|
|
|
private func deleteExerciseFromWorkout(offsets: IndexSet) {
|
|
withAnimation {
|
|
for index in offsets {
|
|
modelContext.delete(workout.workoutItems[index])
|
|
}
|
|
try? modelContext.save()
|
|
}
|
|
}
|
|
|
|
private func move(from source: IndexSet, to destination: Int) {
|
|
workout.moveWorkoutItem(from: source, to: destination)
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
NavigationStack {
|
|
WorkoutDetail(workout: Workout.sampleData)
|
|
.modelContainer(SampleData.shared.modelContainer)
|
|
}
|
|
}
|
|
|
|
#Preview("Debug") {
|
|
TabView {
|
|
WorkoutDetail(workout: Workout.sampleData)
|
|
.tabItem {
|
|
Image(systemName: "figure.run.square.stack")
|
|
Text("Workouts")
|
|
}
|
|
|
|
DebugList()
|
|
.tabItem {
|
|
Image(systemName: "hammer")
|
|
Text("Debug")
|
|
}
|
|
}
|
|
.modelContainer(SampleData.shared.modelContainer)
|
|
}
|