- GroupFilterViewModel now tracks lastToggledGroupId (the single group most recently toggled to a non-empty selection), since toggling a group with subgroups expands selectedGroupIds to include the whole subtree, breaking the previous count==1 check used to infer the active filter group. - VaultViewModel exposes lastToggledGroupId, forwarded from GroupFilterViewModel by VaultRootView (ContentView.swift), and EntryListView.defaultGroupId now reads it instead of the broken selectedGroupIds.count == 1 check, so the add-entry sheet defaults new entries into the group actually being filtered by, not silently back to root. - EntryListView's search toggle button now clears vm.searchQuery when hiding the search bar, so a hidden bar can no longer keep filtering the list invisibly. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WYqycDFsynHH9VnnK7LNSf
53 lines
1.7 KiB
Swift
53 lines
1.7 KiB
Swift
// MyPass/ViewModels/VaultViewModel.swift
|
|
import Foundation
|
|
import Combine
|
|
import MyPassCore
|
|
|
|
@MainActor
|
|
final class VaultViewModel: ObservableObject {
|
|
@Published var searchQuery: String = ""
|
|
@Published var selectedGroupIds: Set<UUID> = []
|
|
@Published var lastToggledGroupId: UUID?
|
|
@Published var errorMessage: String?
|
|
|
|
let session: VaultSession
|
|
private var sessionCancellable: AnyCancellable?
|
|
|
|
init(session: VaultSession) {
|
|
self.session = session
|
|
sessionCancellable = session.objectWillChange.sink { [weak self] in
|
|
self?.objectWillChange.send()
|
|
}
|
|
}
|
|
|
|
/// All entries in the vault, flattened with their group breadcrumb, before any filtering.
|
|
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()
|
|
entries = entries.filter {
|
|
$0.entry.title.lowercased().contains(q)
|
|
|| $0.entry.username.lowercased().contains(q)
|
|
|| $0.entry.url.lowercased().contains(q)
|
|
}
|
|
}
|
|
return entries
|
|
}
|
|
|
|
func deleteEntry(_ flatEntry: FlatEntry) {
|
|
Task {
|
|
do { try session.deleteEntry(id: flatEntry.entry.id, fromGroupId: flatEntry.parentGroupId) }
|
|
catch { errorMessage = error.localizedDescription }
|
|
}
|
|
}
|
|
}
|