From 78dc4c0d93d30073994fb9e2140235e2ac59813b Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Thu, 10 Sep 2020 19:50:23 -0700 Subject: [PATCH] Switch to env objects --- Secretive/App.swift | 84 ++------------------------ Secretive/Views/ContentView.swift | 64 ++++++++++---------- Secretive/Views/CreateSecretView.swift | 2 +- 3 files changed, 37 insertions(+), 113 deletions(-) diff --git a/Secretive/App.swift b/Secretive/App.swift index dff359d..3d8d539 100644 --- a/Secretive/App.swift +++ b/Secretive/App.swift @@ -3,85 +3,6 @@ import SwiftUI import SecretKit import Brief -//@NSApplicationMain -//class AppDelegate: NSObject, NSApplicationDelegate { -// -// var window: NSWindow! -// @IBOutlet var newMenuItem: NSMenuItem! -// @IBOutlet var toolbar: NSToolbar! -// let storeList: SecretStoreList = { -// let list = SecretStoreList() -// list.add(store: SecureEnclave.Store()) -// list.add(store: SmartCard.Store()) -// return list -// }() -// let updater = Updater() -// let agentStatusChecker = AgentStatusChecker() -// let justUpdatedChecker = JustUpdatedChecker() -// -// func applicationDidFinishLaunching(_ aNotification: Notification) { -// let contentView = ContentView(storeList: storeList, updater: updater, agentStatusChecker: agentStatusChecker, runSetupBlock: { self.runSetup(sender: nil) }) -// // Create the window and set the content view. -// window = NSWindow( -// contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), -// styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], -// backing: .buffered, defer: false) -// window.center() -// window.setFrameAutosaveName("Main Window") -// window.contentView = NSHostingView(rootView: contentView) -// window.makeKeyAndOrderFront(nil) -// window.titleVisibility = .hidden -// window.toolbar = toolbar -// window.isReleasedWhenClosed = false -// if storeList.modifiableStore?.isAvailable ?? false { -// let plus = NSTitlebarAccessoryViewController() -// plus.view = NSButton(image: NSImage(named: NSImage.addTemplateName)!, target: self, action: #selector(add(sender:))) -// plus.layoutAttribute = .right -// window.addTitlebarAccessoryViewController(plus) -// newMenuItem.isEnabled = true -// } -// runSetupIfNeeded() -// relaunchAgentIfNeeded() -// } -// -// func applicationDidBecomeActive(_ notification: Notification) { -// agentStatusChecker.check() -// } -// -// func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { -// guard !flag else { return false } -// window.makeKeyAndOrderFront(self) -// return true -// } -// -// @IBAction func add(sender: AnyObject?) { -// var addWindow: NSWindow! -// let addView = CreateSecretView(store: storeList.modifiableStore!) { -// self.window.endSheet(addWindow) -// } -// addWindow = NSWindow( -// contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), -// styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], -// backing: .buffered, defer: false) -// addWindow.contentView = NSHostingView(rootView: addView) -// window.beginSheet(addWindow, completionHandler: nil) -// } -// -// @IBAction func runSetup(sender: AnyObject?) { -// let setupWindow = NSWindow( -// contentRect: NSRect(x: 0, y: 0, width: 0, height: 0), -// styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], -// backing: .buffered, defer: false) -// let setupView = SetupView() { success in -// self.window.endSheet(setupWindow) -// self.agentStatusChecker.check() -// } -// setupWindow.contentView = NSHostingView(rootView: setupView) -// window.beginSheet(setupWindow, completionHandler: nil) -// } -// -//} - @main struct AppDelegate: App { @@ -97,7 +18,10 @@ struct AppDelegate: App { @SceneBuilder var body: some Scene { WindowGroup { - ContentView(storeList: storeList, updater: updater, agentStatusChecker: agentStatusChecker, runSetupBlock: { self.runSetup(sender: nil) }) + ContentView(runSetupBlock: { self.runSetup(sender: nil) }) + .environmentObject(storeList) + .environmentObject(updater) + .environmentObject(agentStatusChecker) } WindowGroup { SetupView() { _ in diff --git a/Secretive/Views/ContentView.swift b/Secretive/Views/ContentView.swift index 59291ee..cdf5366 100644 --- a/Secretive/Views/ContentView.swift +++ b/Secretive/Views/ContentView.swift @@ -3,10 +3,10 @@ import SecretKit import Brief struct ContentView: View { - - @ObservedObject var storeList: SecretStoreList - @ObservedObject var updater: UpdaterType - @ObservedObject var agentStatusChecker: AgentStatusCheckerType + + @EnvironmentObject var storeList: SecretStoreList + @EnvironmentObject var updater: UpdaterType + @EnvironmentObject var agentStatusChecker: AgentStatusCheckerType var runSetupBlock: (() -> Void)? @State private var active: AnySecret.ID? @@ -68,7 +68,7 @@ struct ContentView