feat: unify iOS/macOS navigation as NavigationSplitView, fix VM lifecycle
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WYqycDFsynHH9VnnK7LNSf
This commit is contained in:
+28
-83
@@ -19,7 +19,7 @@ struct ContentView: View {
|
|||||||
if session.isLocked {
|
if session.isLocked {
|
||||||
UnlockView(vm: UnlockViewModel(session: session))
|
UnlockView(vm: UnlockViewModel(session: session))
|
||||||
} else {
|
} else {
|
||||||
vaultView
|
VaultRootView(session: session)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onReceive(
|
.onReceive(
|
||||||
@@ -29,20 +29,6 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ViewBuilder
|
|
||||||
private var vaultView: some View {
|
|
||||||
let vaultVM = VaultViewModel(session: session)
|
|
||||||
#if os(macOS)
|
|
||||||
MacVaultView(vm: vaultVM)
|
|
||||||
#else
|
|
||||||
NavigationStack {
|
|
||||||
if let root = session.database?.root {
|
|
||||||
GroupBrowserView(vm: vaultVM, group: root)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private var sceneBackgroundNotification: Notification.Name {
|
private var sceneBackgroundNotification: Notification.Name {
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
UIScene.didEnterBackgroundNotification
|
UIScene.didEnterBackgroundNotification
|
||||||
@@ -52,82 +38,41 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - macOS 3-column layout
|
/// 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
|
||||||
|
|
||||||
#if os(macOS)
|
init(session: VaultSession) {
|
||||||
private struct MacVaultView: View {
|
_vaultVM = StateObject(wrappedValue: VaultViewModel(session: session))
|
||||||
@ObservedObject var vm: VaultViewModel
|
_filterVM = StateObject(wrappedValue: GroupFilterViewModel(session: session))
|
||||||
@State private var selectedGroup: MyPassCore.Group?
|
}
|
||||||
@State private var selectedEntry: Entry?
|
|
||||||
@State private var showAddEntry = false
|
|
||||||
|
|
||||||
private var activeGroup: MyPassCore.Group? { selectedGroup ?? vm.session.database?.root }
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationSplitView {
|
NavigationSplitView(columnVisibility: $columnVisibility) {
|
||||||
GroupSidebarView(vm: vm, selectedGroup: $selectedGroup)
|
if let root = vaultVM.session.database?.root {
|
||||||
} content: {
|
GroupFilterView(vm: filterVM, rootGroup: root)
|
||||||
if vm.isSearching {
|
|
||||||
SearchView(vm: vm, selectedEntry: $selectedEntry)
|
|
||||||
} else if let group = activeGroup {
|
|
||||||
entryList(for: group)
|
|
||||||
} else {
|
|
||||||
Text("Select a group").foregroundStyle(.secondary)
|
|
||||||
}
|
}
|
||||||
} detail: {
|
} detail: {
|
||||||
if let entry = selectedEntry {
|
NavigationStack {
|
||||||
EntryDetailView(entry: entry, session: vm.session)
|
EntryListView(vm: vaultVM)
|
||||||
} else {
|
|
||||||
Text("Select an entry").foregroundStyle(.secondary)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.searchable(text: $vm.searchQuery, prompt: "Search all entries…")
|
|
||||||
.sheet(isPresented: $showAddEntry) {
|
|
||||||
if let group = activeGroup {
|
|
||||||
EntryEditView(vm: EntryEditViewModel(session: vm.session, groupId: group.id))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func entryList(for group: MyPassCore.Group) -> some View {
|
|
||||||
List(group.entries, selection: $selectedEntry) { entry in
|
|
||||||
EntryListRow(entry: entry).tag(entry)
|
|
||||||
}
|
|
||||||
.navigationTitle(group.name)
|
|
||||||
.toolbar {
|
.toolbar {
|
||||||
ToolbarItem {
|
ToolbarItem(placement: .navigation) {
|
||||||
Button { showAddEntry = true } label: { Image(systemName: "plus") }
|
Button {
|
||||||
}
|
columnVisibility = columnVisibility == .all ? .detailOnly : .all
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private struct GroupSidebarView: View {
|
|
||||||
@ObservedObject var vm: VaultViewModel
|
|
||||||
@Binding var selectedGroup: MyPassCore.Group?
|
|
||||||
|
|
||||||
var body: some View {
|
|
||||||
List(selection: $selectedGroup) {
|
|
||||||
if let root = vm.session.database?.root {
|
|
||||||
GroupNode(group: root)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.navigationTitle("Groups")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private struct GroupNode: View {
|
|
||||||
let group: MyPassCore.Group
|
|
||||||
var body: some View {
|
|
||||||
if group.subgroups.isEmpty {
|
|
||||||
Label(group.name, systemImage: "folder").tag(group)
|
|
||||||
} else {
|
|
||||||
DisclosureGroup {
|
|
||||||
ForEach(group.subgroups) { sub in GroupNode(group: sub) }
|
|
||||||
} label: {
|
} label: {
|
||||||
Label(group.name, systemImage: "folder").tag(group)
|
Image(systemName: "line.3.horizontal")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onChange(of: filterVM.selectedGroupIds) { _, newValue in
|
||||||
|
vaultVM.selectedGroupIds = newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|||||||
Reference in New Issue
Block a user