mirror of
https://github.com/dscyrescotti/Memola.git
synced 2026-03-20 16:43:58 +01:00
feat: add search bar
This commit is contained in:
@@ -13,6 +13,7 @@ struct MemosView: View {
|
||||
@FetchRequest var memoObjects: FetchedResults<MemoObject>
|
||||
|
||||
@State var memo: MemoObject?
|
||||
@State var query: String = ""
|
||||
|
||||
@AppStorage("memola.memo-objects.sort") var sort: Sort = .recent
|
||||
|
||||
@@ -31,24 +32,28 @@ struct MemosView: View {
|
||||
NavigationStack {
|
||||
memoGrid
|
||||
.navigationTitle("Memos")
|
||||
.searchable(text: $query, placement: .toolbar, prompt: Text("Search"))
|
||||
.toolbar {
|
||||
ToolbarItemGroup(placement: .primaryAction) {
|
||||
Menu {
|
||||
Picker("", selection: $sort) {
|
||||
ForEach(Sort.all) { sort in
|
||||
Text(sort.name)
|
||||
.tag(sort)
|
||||
}
|
||||
HStack(spacing: 5) {
|
||||
Button {
|
||||
createMemo(title: "Untitled")
|
||||
} label: {
|
||||
Image(systemName: "square.and.pencil")
|
||||
}
|
||||
} label: {
|
||||
Image(systemName: "arrow.up.arrow.down.circle")
|
||||
.hoverEffect(.lift)
|
||||
Menu {
|
||||
Picker("", selection: $sort) {
|
||||
ForEach(Sort.all) { sort in
|
||||
Text(sort.name)
|
||||
.tag(sort)
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Image(systemName: "arrow.up.arrow.down.circle")
|
||||
}
|
||||
.hoverEffect(.lift)
|
||||
}
|
||||
Button {
|
||||
createMemo(title: "Untitled")
|
||||
} label: {
|
||||
Image(systemName: "square.and.pencil")
|
||||
}
|
||||
.hoverEffect()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,6 +69,9 @@ struct MemosView: View {
|
||||
.onChange(of: sort) { oldValue, newValue in
|
||||
memoObjects.sortDescriptors = newValue.memoSortDescriptors
|
||||
}
|
||||
.onChange(of: query) { oldValue, newValue in
|
||||
updatePredicate()
|
||||
}
|
||||
}
|
||||
|
||||
var memoGrid: some View {
|
||||
@@ -148,4 +156,15 @@ struct MemosView: View {
|
||||
func openMemo(for memo: MemoObject) {
|
||||
self.memo = memo
|
||||
}
|
||||
|
||||
func updatePredicate() {
|
||||
var predicates: [NSPredicate] = []
|
||||
if !query.isEmpty {
|
||||
predicates.append(NSPredicate(format: "title contains[c] %@", query))
|
||||
}
|
||||
// if filter == .favorites {
|
||||
// predicates.append(NSPredicate(format: "isFavorite = YES"))
|
||||
// }
|
||||
memoObjects.nsPredicate = NSCompoundPredicate(type: .and, subpredicates: predicates)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user