add ExerciseTemplate conecpt, fix UI elements
This commit is contained in:
@@ -24,20 +24,22 @@ struct WorkoutDetail: View {
|
||||
Section(header: Text("Exercises")) {
|
||||
List {
|
||||
ForEach(workout.exercises) { exercise in
|
||||
Text(exercise.name)
|
||||
ExerciseListItem(workout, exercise)
|
||||
}
|
||||
.onDelete(perform: deleteExerciseFromWorkout)
|
||||
.onMove(perform: move)
|
||||
}
|
||||
.environment(\.editMode, .constant(.active)) // Always active drag mode
|
||||
Button(action: addExerciseToWorkout) {
|
||||
Text("Add")
|
||||
Text("Add Exercise")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.navigationBarTitle("Edit \(workout.name)")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button("Done") {
|
||||
dismiss()
|
||||
}
|
||||
ToolbarItem {
|
||||
EditButton()
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $isPresenting) {
|
||||
@@ -64,11 +66,54 @@ struct WorkoutDetail: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
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 {
|
||||
Text(String(workout.exercises.filter { $0 == exercise }.count))
|
||||
.font(.system(size: 14, weight: .bold))
|
||||
.foregroundColor(.white)
|
||||
.frame(width: 20, height: 10)
|
||||
.padding(8)
|
||||
.background(Color.blue)
|
||||
.clipShape(RoundedRectangle(cornerRadius: 8))
|
||||
Text(exercise.name)
|
||||
.foregroundColor(.black)
|
||||
Spacer()
|
||||
Image(systemName: "info.circle")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
NavigationStack {
|
||||
WorkoutDetail(workout: Workout(name: "RR"))
|
||||
.modelContainer(SampleData.shared.modelContainer)
|
||||
WorkoutDetail(workout: Workout.sampleData.first!)
|
||||
.modelContainer(SampleData.shared.modelContainer)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user