2026-09-19 17:04:52 +02:00
|
|
|
// MyPass/ViewModels/VaultViewModel.swift
|
2026-09-19 15:58:41 +02:00
|
|
|
import Foundation
|
|
|
|
|
import Combine
|
|
|
|
|
import MyPassCore
|
|
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
|
final class VaultViewModel: ObservableObject {
|
|
|
|
|
@Published var searchQuery: String = ""
|
2026-09-19 17:04:52 +02:00
|
|
|
@Published var selectedGroupIds: Set<UUID> = []
|
2026-09-19 15:58:41 +02:00
|
|
|
@Published var errorMessage: String?
|
|
|
|
|
|
|
|
|
|
let session: VaultSession
|
2026-09-19 17:17:38 +02:00
|
|
|
private var sessionCancellable: AnyCancellable?
|
2026-09-19 15:58:41 +02:00
|
|
|
|
|
|
|
|
init(session: VaultSession) {
|
|
|
|
|
self.session = session
|
2026-09-19 17:17:38 +02:00
|
|
|
sessionCancellable = session.objectWillChange.sink { [weak self] in
|
|
|
|
|
self?.objectWillChange.send()
|
|
|
|
|
}
|
2026-09-19 15:58:41 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-19 17:04:52 +02:00
|
|
|
/// 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)
|
2026-09-19 15:58:41 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-19 17:04:52 +02:00
|
|
|
/// 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
|
|
|
|
|
}
|
2026-09-19 15:58:41 +02:00
|
|
|
|
2026-09-19 17:04:52 +02:00
|
|
|
func deleteEntry(_ flatEntry: FlatEntry) {
|
2026-09-19 15:58:41 +02:00
|
|
|
Task {
|
2026-09-19 17:04:52 +02:00
|
|
|
do { try session.deleteEntry(id: flatEntry.entry.id, fromGroupId: flatEntry.parentGroupId) }
|
2026-09-19 15:58:41 +02:00
|
|
|
catch { errorMessage = error.localizedDescription }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|