132 lines
3.3 KiB
Swift
132 lines
3.3 KiB
Swift
//
|
|
// WorkoutDetailsView.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
|
|
|
|
@Bindable var workout: Workout
|
|
@State private var isPresenting = false
|
|
|
|
var body: some View {
|
|
|
|
Form {
|
|
Section(header: Text("Workout Name")) {
|
|
TextField("Workout Name", text: $workout.name)
|
|
}
|
|
Section(header: Text("Exercises")) {
|
|
List {
|
|
ForEach(workout.exercises) { exercise in
|
|
ExerciseListItem(workout, exercise)
|
|
}
|
|
.onDelete(perform: deleteExerciseFromWorkout)
|
|
.onMove(perform: move)
|
|
}
|
|
.environment(\.editMode, .constant(.active)) // Always active drag mode
|
|
Button(action: addExerciseToWorkout) {
|
|
Text("Add Exercise")
|
|
}
|
|
}
|
|
}
|
|
|
|
.navigationBarTitle("Edit \(workout.name)")
|
|
.toolbar {
|
|
ToolbarItem() {
|
|
EditButton()
|
|
}
|
|
}
|
|
.sheet(isPresented: $isPresenting) {
|
|
NavigationStack {
|
|
AddExerciseToWorkout(workout: workout)
|
|
}
|
|
.presentationDetents([.medium, .large])
|
|
.presentationDragIndicator(.visible)
|
|
}
|
|
}
|
|
|
|
private func addExerciseToWorkout() {
|
|
withAnimation {
|
|
isPresenting = 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.exercises[index])
|
|
}
|
|
try? modelContext.save()
|
|
}
|
|
}
|
|
|
|
private func move(from source: IndexSet, to destination: Int) {
|
|
workout.exercises.move(fromOffsets: source, toOffset: destination)
|
|
}
|
|
}
|
|
|
|
struct ExerciseListItem: View {
|
|
var workout: Workout
|
|
@State var exercise: Exercise
|
|
|
|
init(_ workout: Workout, _ exercise: Exercise ) {
|
|
self.workout = workout
|
|
self.exercise = exercise
|
|
}
|
|
|
|
var body: some View {
|
|
Button(action: {
|
|
// workout.addExercise(from: exercise)
|
|
}) {
|
|
HStack {
|
|
|
|
// TextField("Enter Reps", text: $exercise.reps)
|
|
// .keyboardType(.numberPad) // Set the keyboard to number pad
|
|
// .padding()
|
|
// .frame(width: 100, height: 50)
|
|
// .background(Color(.systemGray6))
|
|
// .cornerRadius(8)
|
|
// .multilineTextAlignment(.center) // Center the text
|
|
// .overlay(
|
|
// RoundedRectangle(cornerRadius: 8)
|
|
// .stroke(Color.gray, lineWidth: 1)
|
|
// )
|
|
Text(String(workout.exercises.filter { $0 == exercise }.count))
|
|
.font(.system(size: 14, weight: .bold))
|
|
.foregroundStyle(.white)
|
|
.frame(width: 20, height: 10)
|
|
.padding(8)
|
|
.background(Color.blue)
|
|
.clipShape(RoundedRectangle(cornerRadius: 8))
|
|
Text(exercise.name)
|
|
.foregroundStyle(.black)
|
|
Spacer()
|
|
Image(systemName: "info.circle")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
NavigationStack {
|
|
WorkoutDetail(workout: Workout.sampleData.first!)
|
|
.modelContainer(SampleData.shared.modelContainer)
|
|
}
|
|
}
|