add ExerciseEditor, Picker skeletons, AutocompleteTextfield
This commit is contained in:
@@ -12,7 +12,7 @@ struct ExerciseLibrary: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Query(sort: \Exercise.name) private var exercises: [Exercise]
|
||||
|
||||
@State private var newExercise: Exercise = Exercise("")
|
||||
@State private var newExercise: Exercise = Exercise("", .reps)
|
||||
@State private var newExerciseName: String = ""
|
||||
@State private var isAddingExercise: Bool = false
|
||||
@FocusState private var isInputFieldFocused: Bool
|
||||
@@ -26,37 +26,33 @@ struct ExerciseLibrary: View {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add search bar to the top
|
||||
|
||||
var body: some View {
|
||||
|
||||
Group {
|
||||
List {
|
||||
ForEach(filteredItems) { exercise in
|
||||
NavigationLink {
|
||||
ExerciseDetail(exercise: exercise)
|
||||
} label: {
|
||||
Text(exercise.name)
|
||||
// TODO: show exercise.metric in gray (eg Dips = reps, Intervall = time or = distance?)
|
||||
Section {
|
||||
ForEach(filteredItems) { exercise in
|
||||
NavigationLink {
|
||||
ExerciseEditor(exercise: exercise)
|
||||
} label: {
|
||||
HStack {
|
||||
Text(exercise.name)
|
||||
Spacer()
|
||||
Text(exercise.metric.rawValue)
|
||||
.font(.footnote)
|
||||
.foregroundStyle(.gray)
|
||||
}
|
||||
}
|
||||
}
|
||||
.onDelete(perform: deleteExercise)
|
||||
if filteredItems.isEmpty {
|
||||
ContentUnavailableView.search(text: searchText)
|
||||
}
|
||||
}
|
||||
.onDelete(perform: deleteExercise)
|
||||
if isAddingExercise {
|
||||
TextField("New Exercise", text: $newExerciseName, onCommit: {
|
||||
newExercise.name = newExerciseName
|
||||
save(exercise: newExercise)
|
||||
isAddingExercise = false
|
||||
})
|
||||
.textInputAutocapitalization(.words)
|
||||
.focused($isInputFieldFocused)
|
||||
Section {
|
||||
AddItemButton(label: "Exercise", action: addExercise)
|
||||
}
|
||||
if filteredItems.isEmpty {
|
||||
ContentUnavailableView.search(text: searchText)
|
||||
}
|
||||
AddItemButton(label: "Exercise", action: addExercise)
|
||||
}
|
||||
.searchable(text: $searchText)
|
||||
|
||||
}
|
||||
.navigationTitle("Exercises")
|
||||
.toolbar {
|
||||
@@ -64,27 +60,20 @@ struct ExerciseLibrary: View {
|
||||
EditButton()
|
||||
}
|
||||
ToolbarItem {
|
||||
Button(action: {}) {
|
||||
Button(action: addExercise) {
|
||||
Image(systemName: "plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func addExercise() {
|
||||
withAnimation {
|
||||
newExercise = Exercise("")
|
||||
newExerciseName = ""
|
||||
isAddingExercise = true
|
||||
isInputFieldFocused = true
|
||||
.sheet(isPresented: $isAddingExercise) {
|
||||
ExerciseEditor(isPresentedAsSheet: true)
|
||||
}
|
||||
}
|
||||
|
||||
private func save(exercise: Exercise) {
|
||||
if !exercise.name.isEmpty {
|
||||
modelContext.insert(exercise)
|
||||
try? modelContext.save()
|
||||
private func addExercise() {
|
||||
withAnimation {
|
||||
isAddingExercise = true
|
||||
isInputFieldFocused = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user