add sheet to add exercises
This commit is contained in:
@@ -10,7 +10,10 @@ import SwiftData
|
||||
|
||||
struct ContentView: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Query private var items: [Exercise]
|
||||
@Query private var exercises: [Exercise]
|
||||
|
||||
@State private var isAddingNewExercise = false
|
||||
@State private var selectedExercise: Exercise?
|
||||
|
||||
let initialDataSet = [
|
||||
"Pull-up",
|
||||
@@ -23,47 +26,59 @@ struct ContentView: View {
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List {
|
||||
ForEach(items) { item in
|
||||
ForEach(exercises) { exercise in
|
||||
NavigationLink {
|
||||
Text("Item at \(item.name)")
|
||||
Text(exercise.name)
|
||||
} label: {
|
||||
Text(item.name)
|
||||
Text(exercise.name)
|
||||
}
|
||||
}
|
||||
.onDelete(perform: deleteItems)
|
||||
.onDelete(perform: deleteExercises)
|
||||
}
|
||||
.onAppear {
|
||||
if items.isEmpty {
|
||||
loadInitialData(exercises: initialDataSet )
|
||||
if exercises.isEmpty {
|
||||
loadInitialData(exercises: initialDataSet)
|
||||
}
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
EditButton()
|
||||
}
|
||||
ToolbarItem {
|
||||
Button(action: addItem) {
|
||||
Label("Add Item", systemImage: "plus")
|
||||
Button {
|
||||
addExercise()
|
||||
} label: {
|
||||
Label("Add Exercise", systemImage: "plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $isAddingNewExercise) {
|
||||
ExerciseDetailsView(exercise: $selectedExercise, isEditing: false)
|
||||
.onDisappear {
|
||||
if let selectedExercise = selectedExercise, !selectedExercise.name.isEmpty {
|
||||
saveNewExercise()
|
||||
}
|
||||
}
|
||||
}
|
||||
} detail: {
|
||||
Text("Select an item")
|
||||
Text("Select an exercise")
|
||||
}
|
||||
}
|
||||
|
||||
private func addItem() {
|
||||
withAnimation {
|
||||
let newItem = Exercise(name: "New Exercise")
|
||||
modelContext.insert(newItem)
|
||||
}
|
||||
private func addExercise() {
|
||||
selectedExercise = Exercise(name: "")
|
||||
isAddingNewExercise = true
|
||||
}
|
||||
|
||||
private func deleteItems(offsets: IndexSet) {
|
||||
private func saveNewExercise() {
|
||||
guard let newExercise = selectedExercise else { return }
|
||||
modelContext.insert(newExercise)
|
||||
try? modelContext.save()
|
||||
}
|
||||
|
||||
private func deleteExercises(offsets: IndexSet) {
|
||||
withAnimation {
|
||||
for index in offsets {
|
||||
modelContext.delete(items[index])
|
||||
modelContext.delete(exercises[index])
|
||||
}
|
||||
try? modelContext.save()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,12 +94,10 @@ struct ContentView: View {
|
||||
modelContext.insert(item)
|
||||
}
|
||||
|
||||
try? modelContext.save() // Daten speichern
|
||||
try? modelContext.save()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#Preview {
|
||||
ContentView()
|
||||
.modelContainer(for: Exercise.self, inMemory: true)
|
||||
|
||||
62
WorkoutsPlus/ExerciseDetailsView.swift
Normal file
62
WorkoutsPlus/ExerciseDetailsView.swift
Normal file
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// ExerciseDetailsView.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 10.08.24.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ExerciseDetailsView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
|
||||
@Binding var exercise: Exercise?
|
||||
var isEditing: Bool
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
Form {
|
||||
TextField("Exercise Name", text: Binding(
|
||||
get: { exercise?.name ?? "" },
|
||||
set: { newName in
|
||||
if exercise != nil {
|
||||
exercise?.name = newName
|
||||
}
|
||||
}
|
||||
))
|
||||
}
|
||||
.navigationTitle(isEditing ? "Edit Exercise" : "Add Exercise")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarLeading) {
|
||||
Button("Cancel") {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
Button("Save") {
|
||||
saveItem()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func saveItem() {
|
||||
if modelContext.hasChanges {
|
||||
do {
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
print("Failed to save item: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ExerciseDetailsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
let sampleItem = Exercise(name: "Sample Item")
|
||||
ExerciseDetailsView(exercise: .constant(sampleItem), isEditing: false)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user