From 7abd5e3ba47aebe7ca140d3520d3ed2459de28f7 Mon Sep 17 00:00:00 2001 From: breadone Date: Mon, 24 Jun 2024 11:57:05 +1200 Subject: [PATCH] implemented search for projects failed to make a colour filter --- OnCue/View/MainView.swift | 87 ++++++++++++++++++++++++--------------- Shared/Constants.swift | 1 + 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/OnCue/View/MainView.swift b/OnCue/View/MainView.swift index d9c5a56..4382be4 100644 --- a/OnCue/View/MainView.swift +++ b/OnCue/View/MainView.swift @@ -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, 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]) } } } diff --git a/Shared/Constants.swift b/Shared/Constants.swift index 131af55..cfdb924 100644 --- a/Shared/Constants.swift +++ b/Shared/Constants.swift @@ -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"] }