add nothing found view to WorkoutIconSelector, add search to ExerciseLibrary
This commit is contained in:
@@ -15,53 +15,52 @@ struct ExerciseLibrary: View {
|
||||
@State private var newExerciseTemplate: ExerciseTemplate = ExerciseTemplate("")
|
||||
@State private var newExerciseName: String = ""
|
||||
@State private var isAddingExerciseTemplate: Bool = false
|
||||
|
||||
@FocusState private var isInputFieldFocused: Bool
|
||||
|
||||
@State private var searchText: String = ""
|
||||
var filteredItems: [ExerciseTemplate] {
|
||||
if searchText.isEmpty {
|
||||
return exerciseTemplates
|
||||
} else {
|
||||
return exerciseTemplates.filter { $0.name.localizedCaseInsensitiveContains(searchText) }
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add search bar to the top
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
Group {
|
||||
if !exerciseTemplates.isEmpty {
|
||||
List {
|
||||
ForEach(exerciseTemplates) { exerciseTemplate in
|
||||
NavigationLink {
|
||||
ExerciseDetail(exerciseTemplate: exerciseTemplate)
|
||||
} label: {
|
||||
Text(exerciseTemplate.name)
|
||||
}
|
||||
}
|
||||
.onDelete(perform: deleteExercise)
|
||||
if isAddingExerciseTemplate {
|
||||
TextField("New Exercise", text: $newExerciseName, onCommit: {
|
||||
newExerciseTemplate.name = newExerciseName
|
||||
saveExercise(exerciseTemplate: newExerciseTemplate)
|
||||
isAddingExerciseTemplate = false
|
||||
})
|
||||
.focused($isInputFieldFocused)
|
||||
}
|
||||
Button(action: {
|
||||
withAnimation {
|
||||
newExerciseTemplate = ExerciseTemplate("")
|
||||
newExerciseName = ""
|
||||
isAddingExerciseTemplate = true
|
||||
isInputFieldFocused = true
|
||||
}
|
||||
}) {
|
||||
HStack {
|
||||
Image(systemName: "plus.circle.fill")
|
||||
Text("Add Item")
|
||||
}
|
||||
List {
|
||||
ForEach(filteredItems) { exerciseTemplate in
|
||||
NavigationLink {
|
||||
ExerciseDetail(exerciseTemplate: exerciseTemplate)
|
||||
} label: {
|
||||
Text(exerciseTemplate.name)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ContentUnavailableView {
|
||||
Label("No Exercises", systemImage: ExerciseTemplate.systemImage)
|
||||
.onDelete(perform: deleteExercise)
|
||||
if isAddingExerciseTemplate {
|
||||
TextField("New Exercise", text: $newExerciseName, onCommit: {
|
||||
newExerciseTemplate.name = newExerciseName
|
||||
saveExercise(exerciseTemplate: newExerciseTemplate)
|
||||
isAddingExerciseTemplate = false
|
||||
})
|
||||
.focused($isInputFieldFocused)
|
||||
}
|
||||
Button(action: {
|
||||
addExerciseTemplate()
|
||||
}) {
|
||||
HStack {
|
||||
Image(systemName: "plus.circle.fill")
|
||||
.foregroundStyle(.green)
|
||||
Text("Add Exercise")
|
||||
}
|
||||
}
|
||||
}
|
||||
.searchable(text: $searchText)
|
||||
}
|
||||
.navigationBarTitle("Exercises")
|
||||
.navigationBarTitle("Exercise Library")
|
||||
.toolbar {
|
||||
ToolbarItem {
|
||||
EditButton()
|
||||
@@ -70,6 +69,15 @@ struct ExerciseLibrary: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func addExerciseTemplate() {
|
||||
withAnimation {
|
||||
newExerciseTemplate = ExerciseTemplate("")
|
||||
newExerciseName = ""
|
||||
isAddingExerciseTemplate = true
|
||||
isInputFieldFocused = true
|
||||
}
|
||||
}
|
||||
|
||||
private func saveExercise(exerciseTemplate: ExerciseTemplate) {
|
||||
if !exerciseTemplate.name.isEmpty {
|
||||
modelContext.insert(exerciseTemplate)
|
||||
|
||||
@@ -11,8 +11,8 @@ struct WorkoutIconSelector: View {
|
||||
|
||||
@State private var selectedColor: Color = .black
|
||||
@State private var selectedIcon: String?
|
||||
@State private var searchText: String = ""
|
||||
|
||||
@State private var searchText: String = ""
|
||||
var filteredIcons: [String] {
|
||||
if searchText.isEmpty {
|
||||
return fitnessIcons
|
||||
@@ -55,6 +55,11 @@ struct WorkoutIconSelector: View {
|
||||
.padding()
|
||||
.searchable(text: $searchText)
|
||||
}
|
||||
.overlay {
|
||||
if filteredIcons.isEmpty {
|
||||
ContentUnavailableView.search
|
||||
}
|
||||
}
|
||||
|
||||
.navigationTitle("Select a Workout Icon")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user