implemented search for projects

failed to make a colour filter
This commit is contained in:
breadone 2024-06-24 11:57:05 +12:00
parent 937344a1e9
commit 7abd5e3ba4
No known key found for this signature in database
2 changed files with 55 additions and 33 deletions

View File

@ -10,54 +10,42 @@ import SwiftData
struct MainView: View {
@Environment(\.modelContext) private var modelContext
@Query private var items: [OCProject]
@State private var showSettingsView = false
@State private var newProjectName = ""
@State private var addNewProject = false
@State private var filterColor = ""
@State private var searchText = ""
var body: some View {
NavigationSplitView {
List {
ForEach(items) { project in
NavigationLink {
CardsView(project: project)
} label: {
VStack(alignment: .leading) {
Text("\(project.name)")
.bold()
.font(.title2)
.foregroundStyle(Color(hex: project.color) ?? .white)
Text("\(project.cards.count) \(project.cards.count == 1 ? "Card" : "Cards")")
}
}
.padding(.vertical, 3)
}
.onDelete(perform: deleteItems)
ProjectListView(sort: SortDescriptor(\.creationDate, order: .reverse),
searchString: searchText,
color: filterColor)
}
.searchable(text: $searchText)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button(action: {showSettingsView.toggle()}) {
Image(systemName: "gear")
}
}
// ToolbarItem(placement: .topBarTrailing) {
// Menu {
// Picker(selection: $filterColor) {
// ForEach(Constants.colors, id: \.self) { clr in
// Circle()
// .foregroundStyle(clr)
// }
// } label: {
// Text("Color")
// }
// .pickerStyle(.menu)
// } label: {
// Image(systemName: "line.3.horizontal.decrease.circle")
// }
// }
ToolbarItem(placement: .topBarTrailing) {
Menu {
Picker(selection: $filterColor) {
ForEach(Constants.colorText, id: \.self) { clr in
Text(clr)
}
} label: {
Text("Color")
}
.pickerStyle(.menu)
} label: {
Image(systemName: "line.3.horizontal.decrease.circle")
}
}
ToolbarItem(placement: .topBarTrailing) {
Button(action: { addNewProject.toggle() }) {
Label("Add Item", systemImage: "plus")
@ -82,11 +70,44 @@ struct MainView: View {
modelContext.insert(newProject)
}
}
}
private struct ProjectListView: View {
@Environment(\.modelContext) private var modelContext
@Query private var projects: [OCProject]
init(sort: SortDescriptor<OCProject>, searchString: String, color: String) {
_projects = Query(filter: #Predicate {
if searchString.isEmpty {
return true
} else {
return $0.name.localizedStandardContains(searchString)
}
}, sort: [sort])
}
var body: some View {
ForEach(projects) { project in
NavigationLink {
CardsView(project: project)
} label: {
VStack(alignment: .leading) {
Text("\(project.name)")
.bold()
.font(.title2)
.foregroundStyle(Color(hex: project.color) ?? .white)
Text("\(project.cards.count) \(project.cards.count == 1 ? "Card" : "Cards")")
}
}
.padding(.vertical, 3)
}
.onDelete(perform: deleteItems)
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(items[index])
modelContext.delete(projects[index])
}
}
}

View File

@ -9,4 +9,5 @@ import SwiftUI
public struct Constants {
static let colors: [Color] = [.red, .orange, .yellow, .green, .blue, .indigo]
static let colorText = ["None", "Red", "Orange", "Yellow", "Green", "Blue", "Indigo"]
}