72 lines
1.6 KiB
Swift
72 lines
1.6 KiB
Swift
//
|
|
// AddExerciseToWorkout.swift
|
|
// WorkoutsPlus
|
|
//
|
|
// Created by Felix Förtsch on 22.08.24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct AddExerciseToWorkout: View {
|
|
@Environment(\.modelContext) private var modelContext
|
|
@Query(sort: \Exercise.name) private var exercises: [Exercise]
|
|
|
|
@Bindable var workout: Workout
|
|
|
|
var body: some View {
|
|
Group {
|
|
if !exercises.isEmpty {
|
|
List {
|
|
Section(header: Text("Excersises")) {
|
|
ForEach(exercises) { exercise in
|
|
AddExerciseToWorkoutListItem(exercise, workout)
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
ContentUnavailableView {
|
|
// TODO: Add Button that allows adding an exercise
|
|
Label("No Exercises", systemImage: Exercise.systemImage)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct AddExerciseToWorkoutListItem: View {
|
|
@Environment(\.modelContext) private var modelContext
|
|
|
|
var exercise: Exercise
|
|
var workout: Workout
|
|
|
|
init(_ exercise: Exercise, _ workout: Workout) {
|
|
self.exercise = exercise
|
|
self.workout = workout
|
|
}
|
|
|
|
var body: some View {
|
|
Button(action: {
|
|
workout.addExercise(exercise)
|
|
}) {
|
|
HStack {
|
|
Text(exercise.name)
|
|
.foregroundStyle(.black)
|
|
Spacer()
|
|
Image(systemName: "plus.circle.fill")
|
|
.foregroundStyle(.green)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview("With Sample Data") {
|
|
AddExerciseToWorkout(workout: Workout.sampleData.first!)
|
|
.modelContainer(SampleData.shared.modelContainer)
|
|
}
|
|
|
|
#Preview("Empty Database") {
|
|
AddExerciseToWorkout(workout: Workout.sampleData.first!)
|
|
.modelContainer(for: Exercise.self, inMemory: true)
|
|
}
|