add draft views for sets of exercises
This commit is contained in:
@@ -23,18 +23,24 @@ struct WorkoutDetail: View {
|
||||
}
|
||||
Section(header: Text("Exercises")) {
|
||||
List {
|
||||
ForEach(workout.exercises
|
||||
.sorted(by: { $0.position < $1.position})) { exercise in
|
||||
ExerciseListItem(workout, exercise)
|
||||
}
|
||||
.onDelete(perform: deleteExerciseFromWorkout)
|
||||
.onMove(perform: move)
|
||||
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: addExerciseToWorkout)
|
||||
AddItemButton(label: "Exercise", action: addWorkoutItemToWorkout)
|
||||
}
|
||||
}
|
||||
|
||||
.navigationBarTitle("Edit \(workout.name)")
|
||||
.toolbar {
|
||||
ToolbarItem() {
|
||||
@@ -43,14 +49,14 @@ struct WorkoutDetail: View {
|
||||
}
|
||||
.sheet(isPresented: $isPresenting) {
|
||||
NavigationStack {
|
||||
AddExerciseToWorkout(workout: workout)
|
||||
AddWorkoutItemToWorkout(workout: workout)
|
||||
}
|
||||
.presentationDetents([.medium, .large])
|
||||
.presentationDragIndicator(.visible)
|
||||
}
|
||||
}
|
||||
|
||||
private func addExerciseToWorkout() {
|
||||
private func addWorkoutItemToWorkout() {
|
||||
withAnimation {
|
||||
isPresenting = true
|
||||
}
|
||||
@@ -69,54 +75,20 @@ struct WorkoutDetail: View {
|
||||
private func deleteExerciseFromWorkout(offsets: IndexSet) {
|
||||
withAnimation {
|
||||
for index in offsets {
|
||||
modelContext.delete(workout.exercises[index])
|
||||
modelContext.delete(workout.workoutItems[index])
|
||||
}
|
||||
try? modelContext.save()
|
||||
}
|
||||
}
|
||||
|
||||
private func move(from source: IndexSet, to destination: Int) {
|
||||
workout.moveExercise(from: source, to: destination)
|
||||
}
|
||||
}
|
||||
|
||||
struct ExerciseListItem: View {
|
||||
var workout: Workout
|
||||
@State var exercise: WorkoutItem
|
||||
|
||||
init(_ workout: Workout, _ exercise: WorkoutItem ) {
|
||||
self.workout = workout
|
||||
self.exercise = exercise
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Button(action: {
|
||||
// workout.addExercise(from: exercise)
|
||||
}) {
|
||||
HStack {
|
||||
Text(String(exercise.reps))
|
||||
.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()
|
||||
Stepper(
|
||||
value: $exercise.reps,
|
||||
in: 0...100,
|
||||
step: 1
|
||||
) {}
|
||||
}
|
||||
}
|
||||
workout.moveWorkoutItem(from: source, to: destination)
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
NavigationStack {
|
||||
WorkoutDetail(workout: Workout.sampleData.first!)
|
||||
WorkoutDetail(workout: Workout.sampleData)
|
||||
.modelContainer(SampleData.shared.modelContainer)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user