85 lines
2.3 KiB
Swift
85 lines
2.3 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 selectedExercises: [Exercise] = [Exercise("Selected")]
|
|
|
|
var body: some View {
|
|
Group {
|
|
if !exercises.isEmpty {
|
|
List {
|
|
Section(header: Text("Utility")) {
|
|
// TODO: Loop, Warm-up, Cool-down are not unique yet. They are special utility types
|
|
AddExerciseToWorkoutListItem(Exercise("Loop"), workout)
|
|
AddExerciseToWorkoutListItem(Exercise("Warm-up"),workout)
|
|
AddExerciseToWorkoutListItem(Exercise("Cool-down"),workout)
|
|
}
|
|
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 {
|
|
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(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: "plus.circle.fill")
|
|
.foregroundColor(.green)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview("With Sample Data") {
|
|
AddExerciseToWorkout(workout: Workout(name: "New Workout"))
|
|
.modelContainer(SampleData.shared.modelContainer)
|
|
}
|
|
|
|
#Preview("Empty Database") {
|
|
AddExerciseToWorkout(workout: Workout(name: "New Workout"))
|
|
.modelContainer(for: Exercise.self, inMemory: true)
|
|
}
|