feat: add group + search filtering to VaultViewModel
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WYqycDFsynHH9VnnK7LNSf
This commit is contained in:
@@ -1,8 +1,4 @@
|
|||||||
//
|
// MyPass/ViewModels/VaultViewModel.swift
|
||||||
// VaultViewModel.swift
|
|
||||||
// MyPass
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import Combine
|
import Combine
|
||||||
import MyPassCore
|
import MyPassCore
|
||||||
@@ -10,6 +6,7 @@ import MyPassCore
|
|||||||
@MainActor
|
@MainActor
|
||||||
final class VaultViewModel: ObservableObject {
|
final class VaultViewModel: ObservableObject {
|
||||||
@Published var searchQuery: String = ""
|
@Published var searchQuery: String = ""
|
||||||
|
@Published var selectedGroupIds: Set<UUID> = []
|
||||||
@Published var errorMessage: String?
|
@Published var errorMessage: String?
|
||||||
|
|
||||||
let session: VaultSession
|
let session: VaultSession
|
||||||
@@ -18,21 +15,32 @@ final class VaultViewModel: ObservableObject {
|
|||||||
self.session = session
|
self.session = session
|
||||||
}
|
}
|
||||||
|
|
||||||
var filteredEntries: [Entry] {
|
/// All entries in the vault, flattened with their group breadcrumb, before any filtering.
|
||||||
guard !searchQuery.isEmpty else { return [] }
|
private var allFlatEntries: [FlatEntry] {
|
||||||
|
guard let root = session.database?.root else { return [] }
|
||||||
|
return flattenEntries(in: root)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Entries after applying the group filter, then the search filter (AND semantics).
|
||||||
|
var displayedEntries: [FlatEntry] {
|
||||||
|
var entries = allFlatEntries
|
||||||
|
if !selectedGroupIds.isEmpty {
|
||||||
|
entries = entries.filter { selectedGroupIds.contains($0.parentGroupId) }
|
||||||
|
}
|
||||||
|
if !searchQuery.isEmpty {
|
||||||
let q = searchQuery.lowercased()
|
let q = searchQuery.lowercased()
|
||||||
return session.allEntries().filter {
|
entries = entries.filter {
|
||||||
$0.title.lowercased().contains(q)
|
$0.entry.title.lowercased().contains(q)
|
||||||
|| $0.username.lowercased().contains(q)
|
|| $0.entry.username.lowercased().contains(q)
|
||||||
|| $0.url.lowercased().contains(q)
|
|| $0.entry.url.lowercased().contains(q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
|
||||||
var isSearching: Bool { !searchQuery.isEmpty }
|
func deleteEntry(_ flatEntry: FlatEntry) {
|
||||||
|
|
||||||
func deleteEntry(_ entry: Entry, fromGroup group: Group) {
|
|
||||||
Task {
|
Task {
|
||||||
do { try session.deleteEntry(id: entry.id, fromGroupId: group.id) }
|
do { try session.deleteEntry(id: flatEntry.entry.id, fromGroupId: flatEntry.parentGroupId) }
|
||||||
catch { errorMessage = error.localizedDescription }
|
catch { errorMessage = error.localizedDescription }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user