Files
keevault/MyPass/ContentView.swift
T
krissandClaude Sonnet 5 f454b4594a fix: correct add-entry default group and clear search on hide
- 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
2026-09-19 17:25:06 +02:00

82 lines
2.6 KiB
Swift

//
// ContentView.swift
// MyPass
//
// Created by Christophe Vila on 21/05/2026.
//
import SwiftUI
import MyPassCore
#if os(macOS)
import AppKit
#endif
struct ContentView: View {
@StateObject private var session = VaultSession()
var body: some View {
SwiftUI.Group {
if session.isLocked {
UnlockView(vm: UnlockViewModel(session: session))
} else {
VaultRootView(session: session)
}
}
.onReceive(
NotificationCenter.default.publisher(for: sceneBackgroundNotification)
) { _ in
session.lock()
}
}
private var sceneBackgroundNotification: Notification.Name {
#if os(iOS)
UIScene.didEnterBackgroundNotification
#else
NSApplication.didResignActiveNotification
#endif
}
}
/// Owns the vault/group-filter view models for as long as the vault stays unlocked -- their
/// state (search query, group filter selection) persists across re-renders but resets if the
/// vault locks and is unlocked again (a fresh VaultRootView is created), which matches the
/// expected "locking clears your filter" behavior.
private struct VaultRootView: View {
@StateObject private var vaultVM: VaultViewModel
@StateObject private var filterVM: GroupFilterViewModel
@State private var columnVisibility: NavigationSplitViewVisibility = .all
init(session: VaultSession) {
_vaultVM = StateObject(wrappedValue: VaultViewModel(session: session))
_filterVM = StateObject(wrappedValue: GroupFilterViewModel(session: session))
}
var body: some View {
NavigationSplitView(columnVisibility: $columnVisibility) {
if let root = vaultVM.session.database?.root {
GroupFilterView(vm: filterVM, rootGroup: root)
}
} detail: {
NavigationStack {
EntryListView(vm: vaultVM)
.toolbar {
ToolbarItem(placement: .navigation) {
Button {
columnVisibility = columnVisibility == .all ? .detailOnly : .all
} label: {
Image(systemName: "line.3.horizontal")
}
}
}
}
}
.onChange(of: filterVM.selectedGroupIds) { _, newValue in
vaultVM.selectedGroupIds = newValue
}
.onChange(of: filterVM.lastToggledGroupId) { _, newValue in
vaultVM.lastToggledGroupId = newValue
}
}
}