61 lines
1.2 KiB
Swift
61 lines
1.2 KiB
Swift
//
|
|
// 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
|
|
|
|
var item: Exercise?
|
|
var isPresentedAsSheet: Bool = false
|
|
|
|
var body: some View {
|
|
|
|
Form {
|
|
TextField("Exercise Name", text: Binding(
|
|
get: { item?.name ?? "" },
|
|
set: { newName in
|
|
if item != nil {
|
|
item?.name = newName
|
|
}
|
|
}
|
|
))
|
|
.toolbar {
|
|
if (isPresentedAsSheet) {
|
|
ToolbarItem(placement: .topBarLeading) {
|
|
Button("Cancel") {
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
ToolbarItem(placement: .topBarTrailing) {
|
|
Button("Save") {
|
|
saveItem()
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func saveItem() {
|
|
if modelContext.hasChanges {
|
|
do {
|
|
try modelContext.save()
|
|
} catch {
|
|
print("Failed to save item: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
let sampleItem = Exercise(name: "Sample Item")
|
|
ExerciseDetailsView(item: sampleItem)
|
|
}
|