112 lines
3.0 KiB
Swift
112 lines
3.0 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 isPresentingWorkoutItemLibrarySheet = false
|
|
|
|
var body: some View {
|
|
List {
|
|
Section(header: Text("Name & Icon")) {
|
|
NavigationLink(destination: WorkoutIconSelector(workout: workout)) {
|
|
TextField("Workout Name", text: $workout.name)
|
|
Image(systemName: workout.workoutIconSystemName)
|
|
.scaledToFit()
|
|
.foregroundStyle(workout.workoutIconColorName.color)
|
|
}
|
|
}
|
|
Section(
|
|
header: Text("Exercises"),
|
|
footer: Text("Drag and drop to re-arrange or swipe to delete exercises.")) {
|
|
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: presentWorkoutItemLibrarySheet)
|
|
}
|
|
}
|
|
.navigationBarTitle("\(workout.name)")
|
|
.toolbar {
|
|
// TODO: Add proper Sharing for workouts.
|
|
ToolbarItem() { ShareLink(item: URL(filePath: "felixfoertsch.de")!) }
|
|
ToolbarItem() { EditButton() }
|
|
}
|
|
.sheet(isPresented: $isPresentingWorkoutItemLibrarySheet) {
|
|
WorkoutItemLibrarySheet(workout: workout)
|
|
}
|
|
}
|
|
|
|
private func presentWorkoutItemLibrarySheet() {
|
|
withAnimation {
|
|
isPresentingWorkoutItemLibrarySheet = 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)
|
|
}
|