create ER diagram, refactor to conform to diagram, simplify session management
This commit is contained in:
@@ -10,6 +10,8 @@ import SwiftData
|
||||
|
||||
struct WorkoutLibrary: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Binding var activeWorkoutSession: WorkoutSession?
|
||||
|
||||
@Query(sort: \Workout.name) private var workouts: [Workout]
|
||||
|
||||
@State private var newWorkout: Workout = Workout(name: "")
|
||||
@@ -25,41 +27,48 @@ struct WorkoutLibrary: View {
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
|
||||
Group {
|
||||
List {
|
||||
ForEach(filteredItems) { workout in
|
||||
NavigationLink {
|
||||
WorkoutDetail(workout: workout)
|
||||
} label: {
|
||||
Image(systemName: workout.workoutIconSystemName)
|
||||
.foregroundStyle(workout.workoutIconColorName.color)
|
||||
Text(workout.name)
|
||||
|
||||
Group {
|
||||
List {
|
||||
ForEach(filteredItems) { workout in
|
||||
NavigationLink {
|
||||
WorkoutDetail(activeWorkoutSession: $activeWorkoutSession, workout: workout)
|
||||
} label: {
|
||||
Button(action: {
|
||||
activeWorkoutSession = workout.start()
|
||||
}) {
|
||||
Image(systemName: "play.fill")
|
||||
.foregroundStyle(.green)
|
||||
}
|
||||
// TODO: Decide if icon should appear here/create custom view
|
||||
// Image(systemName: workout.workoutIconSystemName)
|
||||
// .foregroundStyle(workout.workoutIconColorName.color)
|
||||
Text(workout.name)
|
||||
}
|
||||
.onDelete(perform: deleteWorkout)
|
||||
if filteredItems.isEmpty {
|
||||
ContentUnavailableView.search(text: searchText)
|
||||
}
|
||||
if isAddingWorkout {
|
||||
// TODO: On tap-out of the text field, it should lose focus
|
||||
TextField("New Workout", text: $newWorkoutName, onCommit: {
|
||||
save(workout: newWorkout)
|
||||
})
|
||||
.textInputAutocapitalization(.words)
|
||||
.focused($isInputFieldFocused)
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
.navigationTitle("Workouts")
|
||||
.toolbar {
|
||||
ToolbarItem() {
|
||||
EditButton()
|
||||
.onDelete(perform: deleteWorkout)
|
||||
if filteredItems.isEmpty {
|
||||
ContentUnavailableView.search(text: searchText)
|
||||
}
|
||||
if isAddingWorkout {
|
||||
// TODO: On tap-out of the text field, it should lose focus
|
||||
TextField("New Workout", text: $newWorkoutName, onCommit: {
|
||||
save(workout: newWorkout)
|
||||
})
|
||||
.textInputAutocapitalization(.words)
|
||||
.focused($isInputFieldFocused)
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
.navigationTitle("Workouts")
|
||||
.toolbar {
|
||||
ToolbarItem() {
|
||||
EditButton()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func addWorkout() {
|
||||
@@ -93,16 +102,18 @@ struct WorkoutLibrary: View {
|
||||
}
|
||||
|
||||
#Preview("With Sample Data") {
|
||||
@Previewable @State var activeWorkoutSession: WorkoutSession?
|
||||
NavigationStack {
|
||||
WorkoutLibrary()
|
||||
WorkoutLibrary(activeWorkoutSession: $activeWorkoutSession)
|
||||
}
|
||||
.modelContainer(SampleData.shared.modelContainer)
|
||||
.modelContainer(SampleData.shared.modelContainer)
|
||||
}
|
||||
|
||||
#Preview("Empty Database") {
|
||||
@Previewable @State var activeWorkoutSession: WorkoutSession?
|
||||
NavigationStack {
|
||||
WorkoutLibrary()
|
||||
WorkoutLibrary(activeWorkoutSession: $activeWorkoutSession)
|
||||
}
|
||||
.modelContainer(for: Workout.self, inMemory: true)
|
||||
.modelContainer(for: Workout.self, inMemory: true)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user