change folders to the "feature" mindset
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// 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 = Exercise("")
|
||||
@State private var newExerciseName: String = ""
|
||||
@State private var isAddingExercise: Bool = false
|
||||
@FocusState private var isInputFieldFocused: Bool
|
||||
|
||||
@State private var searchText: String = ""
|
||||
var filteredItems: [Exercise] {
|
||||
if searchText.isEmpty {
|
||||
return exercises
|
||||
} else {
|
||||
return exercises.filter { $0.name.localizedCaseInsensitiveContains(searchText) }
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
List {
|
||||
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)
|
||||
}
|
||||
}
|
||||
Section {
|
||||
AddItemButton(label: "Exercise", action: addExercise)
|
||||
}
|
||||
}
|
||||
.searchable(text: $searchText)
|
||||
}
|
||||
.navigationTitle("Exercises")
|
||||
.toolbar {
|
||||
ToolbarItem {
|
||||
EditButton()
|
||||
}
|
||||
ToolbarItem {
|
||||
Button(action: addExercise) {
|
||||
Image(systemName: "plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $isAddingExercise) {
|
||||
ExerciseEditor(isPresentedAsSheet: true)
|
||||
}
|
||||
}
|
||||
|
||||
private func addExercise() {
|
||||
withAnimation {
|
||||
isAddingExercise = true
|
||||
isInputFieldFocused = true
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteExercise(offsets: IndexSet) {
|
||||
withAnimation {
|
||||
for index in offsets {
|
||||
modelContext.delete(exercises[index])
|
||||
}
|
||||
try? modelContext.save()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview("With Sample Data") {
|
||||
NavigationStack {
|
||||
ExerciseLibrary()
|
||||
}
|
||||
.modelContainer(SampleData.shared.modelContainer)
|
||||
|
||||
}
|
||||
|
||||
#Preview("Empty Database") {
|
||||
NavigationStack {
|
||||
ExerciseLibrary()
|
||||
}
|
||||
.modelContainer(for: WorkoutItem.self, inMemory: true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user