add Workout, Exercise and their Library, Add, and Details views
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// AddExercise.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 18.08.24.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct AddExercise: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@Bindable var exercise: Exercise
|
||||
|
||||
var body : some View {
|
||||
Form {
|
||||
TextField("Workout Name", text: $exercise.name)
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Cancel") {
|
||||
modelContext.delete(exercise)
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button("Save") {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
Color.clear
|
||||
.sheet(isPresented: .constant(true)) {
|
||||
AddExercise(exercise: Exercise(""))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Item.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 10.08.24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class Exercise {
|
||||
var name: String
|
||||
static var systemImage = "figure.run"
|
||||
|
||||
var timestamp: Date
|
||||
|
||||
init(_ name: String = "", timestamp: Date = Date.now) {
|
||||
self.name = name
|
||||
self.timestamp = timestamp
|
||||
}
|
||||
|
||||
static let sampleData = [
|
||||
Exercise("Pull-up"),
|
||||
Exercise("Push-up"),
|
||||
Exercise("Dips"),
|
||||
Exercise("Rows"),
|
||||
Exercise("Split Squat")
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// ExerciseDetailsView.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 10.08.24.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ExerciseDetail: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
|
||||
@Bindable var exercise: Exercise
|
||||
|
||||
var body: some View {
|
||||
|
||||
Form {
|
||||
TextField("Exercise Name", text: $exercise.name)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
Button("Save") {
|
||||
saveItem()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Exercise Details")
|
||||
}
|
||||
|
||||
private func saveItem() {
|
||||
if modelContext.hasChanges {
|
||||
do {
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
print("Failed to save exercise: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ExerciseDetail(exercise: Exercise("New Exercise"))
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// ExerciseLibraryView.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 10.08.24.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
struct ExerciseLibrary: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Query(sort: \Exercise.name) private var exercises: [Exercise]
|
||||
|
||||
@State private var newExercise: Exercise?
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
Group {
|
||||
if !exercises.isEmpty {
|
||||
List {
|
||||
ForEach(exercises) { exercise in
|
||||
NavigationLink {
|
||||
ExerciseDetail(exercise: exercise)
|
||||
} label: {
|
||||
Text(exercise.name)
|
||||
}
|
||||
}
|
||||
.onDelete(perform: deleteExercise)
|
||||
}
|
||||
} else {
|
||||
ContentUnavailableView {
|
||||
Label("No Exercises", systemImage: Exercise.systemImage)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationBarTitle("Exercises")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarLeading) {
|
||||
EditButton()
|
||||
}
|
||||
ToolbarItem {
|
||||
Button(action: addExercise) {
|
||||
Label("Add Exercise", systemImage: "plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
.sheet(item: $newExercise) { exercise in
|
||||
NavigationStack {
|
||||
AddExercise(exercise: exercise)
|
||||
}
|
||||
// TODO: It's possible to add a boolean here ("Terms accepted y/n"). Maybe add this for empty string
|
||||
.interactiveDismissDisabled()
|
||||
}
|
||||
} detail: {
|
||||
// TODO: What does this Detail do?
|
||||
Text("Select a workout")
|
||||
.navigationTitle("Movie")
|
||||
}
|
||||
}
|
||||
|
||||
private func addExercise() {
|
||||
withAnimation {
|
||||
let item = Exercise("")
|
||||
modelContext.insert(item)
|
||||
newExercise = item
|
||||
}
|
||||
}
|
||||
|
||||
private func saveExercise(exercise: Exercise) {
|
||||
if !exercise.name.isEmpty {
|
||||
modelContext.insert(exercise)
|
||||
try? modelContext.save()
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteExercise(offsets: IndexSet) {
|
||||
withAnimation {
|
||||
for index in offsets {
|
||||
modelContext.delete(exercises[index])
|
||||
}
|
||||
try? modelContext.save()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview("With Sample Data") {
|
||||
ExerciseLibrary()
|
||||
.modelContainer(SampleData.shared.modelContainer)
|
||||
}
|
||||
|
||||
#Preview("Empty Database") {
|
||||
ExerciseLibrary()
|
||||
.modelContainer(for: Exercise.self, inMemory: true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user