Files
keevault/MyPass/ContentView.swift
T

79 lines
2.5 KiB
Swift
Raw Normal View History

2026-05-21 22:04:13 +02:00
//
// ContentView.swift
// MyPass
//
// Created by Christophe Vila on 21/05/2026.
//
import SwiftUI
import MyPassCore
#if os(macOS)
import AppKit
#endif
2026-05-21 22:04:13 +02:00
struct ContentView: View {
@StateObject private var session = VaultSession()
2026-05-21 22:04:13 +02:00
var body: some View {
SwiftUI.Group {
if session.isLocked {
UnlockView(vm: UnlockViewModel(session: session))
} else {
VaultRootView(session: session)
2026-05-21 22:04:13 +02:00
}
}
.onReceive(
NotificationCenter.default.publisher(for: sceneBackgroundNotification)
) { _ in
session.lock()
}
2026-05-21 22:04:13 +02:00
}
private var sceneBackgroundNotification: Notification.Name {
#if os(iOS)
UIScene.didEnterBackgroundNotification
#else
NSApplication.didResignActiveNotification
#endif
2026-05-21 22:04:13 +02:00
}
}
/// 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))
}
2026-05-21 22:04:13 +02:00
var body: some View {
NavigationSplitView(columnVisibility: $columnVisibility) {
if let root = vaultVM.session.database?.root {
GroupFilterView(vm: filterVM, rootGroup: root)
}
2026-05-21 22:04:13 +02:00
} 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
2026-05-21 22:04:13 +02:00
}
}
}