introduce WorkoutItem, refactor ExerciseTemplate to Exercise
This commit is contained in:
@@ -10,24 +10,24 @@ import SwiftData
|
||||
|
||||
struct AddExerciseToWorkout: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Query(sort: \ExerciseTemplate.name) private var exerciseTemplates: [ExerciseTemplate]
|
||||
@Query(sort: \Exercise.name) private var exercises: [Exercise]
|
||||
|
||||
@Bindable var workout: Workout
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
if !exerciseTemplates.isEmpty {
|
||||
if !exercises.isEmpty {
|
||||
List {
|
||||
Section(header: Text("Excersises")) {
|
||||
ForEach(exerciseTemplates) { exerciseTemplate in
|
||||
AddExerciseToWorkoutListItem(exerciseTemplate, workout)
|
||||
ForEach(exercises) { exercise in
|
||||
AddExerciseToWorkoutListItem(exercise, workout)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ContentUnavailableView {
|
||||
// TODO: Add Button that allows adding an exercise
|
||||
Label("No Exercises", systemImage: ExerciseTemplate.systemImage)
|
||||
Label("No Exercises", systemImage: Exercise.systemImage)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,20 +37,20 @@ struct AddExerciseToWorkout: View {
|
||||
struct AddExerciseToWorkoutListItem: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
|
||||
var exerciseTemplate: ExerciseTemplate
|
||||
var exercise: Exercise
|
||||
var workout: Workout
|
||||
|
||||
init(_ exerciseTemplate: ExerciseTemplate, _ workout: Workout) {
|
||||
self.exerciseTemplate = exerciseTemplate
|
||||
init(_ exercise: Exercise, _ workout: Workout) {
|
||||
self.exercise = exercise
|
||||
self.workout = workout
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Button(action: {
|
||||
workout.addExercise(from: exerciseTemplate)
|
||||
workout.addExercise(exercise)
|
||||
}) {
|
||||
HStack {
|
||||
Text(exerciseTemplate.name)
|
||||
Text(exercise.name)
|
||||
.foregroundStyle(.black)
|
||||
Spacer()
|
||||
Image(systemName: "plus.circle.fill")
|
||||
@@ -67,5 +67,5 @@ struct AddExerciseToWorkoutListItem: View {
|
||||
|
||||
#Preview("Empty Database") {
|
||||
AddExerciseToWorkout(workout: Workout.sampleData.first!)
|
||||
.modelContainer(for: ExerciseTemplate.self, inMemory: true)
|
||||
.modelContainer(for: Exercise.self, inMemory: true)
|
||||
}
|
||||
|
||||
@@ -17,23 +17,23 @@ final class Workout: Identifiable {
|
||||
// Other properties and methods
|
||||
var timestamp: Date = Date.now
|
||||
|
||||
@Relationship(deleteRule: .cascade) var exercises: [Exercise] = []
|
||||
@Relationship(deleteRule: .cascade) var exercises: [WorkoutItem] = []
|
||||
|
||||
init(name: String) {
|
||||
self.name = name
|
||||
}
|
||||
|
||||
func addExercise(from exerciseTemplate: ExerciseTemplate) {
|
||||
self.exercises.append(Exercise(from: exerciseTemplate))
|
||||
updateExercisePositions()
|
||||
}
|
||||
|
||||
func addExercise(_ exercise: Exercise) {
|
||||
self.exercises.append(exercise)
|
||||
self.exercises.append(WorkoutItem(from: exercise))
|
||||
updateExercisePositions()
|
||||
}
|
||||
|
||||
func addExercise(_ exercises: [Exercise]) {
|
||||
// func addExercise(_ exercise: WorkoutItem) {
|
||||
// self.exercises.append(exercise)
|
||||
// updateExercisePositions()
|
||||
// }
|
||||
|
||||
func addExercise(_ exercises: [WorkoutItem]) {
|
||||
for exercise in exercises {
|
||||
self.exercises.append(exercise)
|
||||
}
|
||||
@@ -54,13 +54,13 @@ final class Workout: Identifiable {
|
||||
}
|
||||
|
||||
extension Workout {
|
||||
private convenience init(name: String, exercises: [Exercise]) {
|
||||
private convenience init(name: String, exercises: [WorkoutItem]) {
|
||||
self.init(name: name)
|
||||
self.exercises = exercises
|
||||
}
|
||||
|
||||
static let sampleData: [Workout] = [
|
||||
Workout(name: "Recommended Routine", exercises: Exercise.sampleData),
|
||||
Workout(name: "Marathon Plan", exercises: Exercise.sampleData)
|
||||
Workout(name: "Recommended Routine", exercises: WorkoutItem.sampleData),
|
||||
Workout(name: "Marathon Plan", exercises: WorkoutItem.sampleData)
|
||||
]
|
||||
}
|
||||
|
||||
@@ -82,9 +82,9 @@ struct WorkoutDetail: View {
|
||||
|
||||
struct ExerciseListItem: View {
|
||||
var workout: Workout
|
||||
@State var exercise: Exercise
|
||||
@State var exercise: WorkoutItem
|
||||
|
||||
init(_ workout: Workout, _ exercise: Exercise ) {
|
||||
init(_ workout: Workout, _ exercise: WorkoutItem ) {
|
||||
self.workout = workout
|
||||
self.exercise = exercise
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Exercise.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 10.08.24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class WorkoutItem: Identifiable {
|
||||
var id = UUID()
|
||||
var name: String
|
||||
var workoutItemType: WorkoutItemType
|
||||
|
||||
var workout: Workout?
|
||||
var children: [WorkoutItem] = []
|
||||
var position: Int = 0
|
||||
|
||||
var exercise: Exercise? {
|
||||
didSet {
|
||||
self.name = exercise?.name ?? "self.name"
|
||||
}
|
||||
}
|
||||
var reps: Int = 0
|
||||
|
||||
init(_ reps: Int, _ exercise: String) {
|
||||
self.workoutItemType = .exercise
|
||||
|
||||
self.name = exercise
|
||||
self.reps = reps
|
||||
self.exercise = Exercise(exercise)
|
||||
}
|
||||
|
||||
init(from exercise: Exercise) {
|
||||
self.workoutItemType = .exercise
|
||||
|
||||
self.name = exercise.name
|
||||
self.exercise = exercise
|
||||
}
|
||||
|
||||
func addChild(_ child: WorkoutItem) {
|
||||
self.children.append(child)
|
||||
}
|
||||
}
|
||||
|
||||
extension WorkoutItem {
|
||||
enum WorkoutItemType: Codable {
|
||||
case set
|
||||
case workout
|
||||
case exercise
|
||||
}
|
||||
}
|
||||
|
||||
extension WorkoutItem {
|
||||
static let sampleData: [WorkoutItem] = {
|
||||
var exercises = [WorkoutItem]()
|
||||
|
||||
for exercise in Exercise.sampleData {
|
||||
exercises.append(WorkoutItem(from: exercise))
|
||||
}
|
||||
|
||||
return exercises
|
||||
}()
|
||||
}
|
||||
@@ -41,23 +41,13 @@ struct WorkoutLibrary: View {
|
||||
if isAddingWorkout {
|
||||
// TODO: On tap-out of the text field, it should lose focus
|
||||
TextField("New Workout", text: $newWorkoutName, onCommit: {
|
||||
saveWorkout(workout: newWorkout)
|
||||
save(workout: newWorkout)
|
||||
})
|
||||
.textInputAutocapitalization(.words)
|
||||
.focused($isInputFieldFocused)
|
||||
}
|
||||
Button(action: {
|
||||
if isAddingWorkout {
|
||||
saveWorkout(workout: newWorkout)
|
||||
}
|
||||
addWorkout()
|
||||
}) {
|
||||
HStack {
|
||||
Image(systemName: "plus.circle.fill")
|
||||
.foregroundStyle(.green)
|
||||
Text("Add Workout")
|
||||
}
|
||||
}
|
||||
// TODO: When pressing the button again, it should check if something is being added already and if yes, save it.
|
||||
AddItemButton(label: "Workout", action: addWorkout)
|
||||
}
|
||||
.searchable(text: $searchText)
|
||||
}
|
||||
@@ -80,7 +70,7 @@ struct WorkoutLibrary: View {
|
||||
}
|
||||
|
||||
// TODO: Brauchen wir das?
|
||||
private func saveWorkout(workout: Workout) {
|
||||
private func save(workout: Workout) {
|
||||
withAnimation {
|
||||
newWorkout.name = newWorkoutName
|
||||
if !workout.name.isEmpty {
|
||||
|
||||
Reference in New Issue
Block a user