diff --git a/Calculator/AppDelegate.swift b/Calculator/AppDelegate.swift index 4a939f0..ea7f3d9 100644 --- a/Calculator/AppDelegate.swift +++ b/Calculator/AppDelegate.swift @@ -14,6 +14,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { var popover = NSPopover.init() var statusBarItem: NSStatusItem? static var shared : AppDelegate! + private var historyStore = HistoryStore() // Environment Object for History var window: NSWindow! @@ -30,7 +31,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { window.center() window.setFrameAutosaveName("Calculator") window.isReleasedWhenClosed = false - window.contentView = NSHostingView(rootView: ContentView()) + window.contentView = NSHostingView(rootView: ContentView().environmentObject(historyStore)) } window.makeKeyAndOrderFront(nil) } @@ -42,13 +43,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { window.close() } - let contentView = ContentView() - // Set the SwiftUI's ContentView to the Popover's ContentViewController popover.behavior = .transient popover.animates = false popover.contentViewController = NSViewController() - popover.contentViewController?.view = NSHostingView(rootView: contentView) + popover.contentViewController?.view = NSHostingView(rootView: ContentView().environmentObject(historyStore)) popover.contentViewController?.view.window?.makeKey() popover.contentSize = CGSize(width: 260, height: 400) diff --git a/Calculator/CalculatorApp.swift b/Calculator/CalculatorApp.swift index 97994ab..acba6d5 100644 --- a/Calculator/CalculatorApp.swift +++ b/Calculator/CalculatorApp.swift @@ -9,6 +9,10 @@ import SwiftUI @main struct CalculatorApp: App { + // MARK TODO: This Environment object is different than what is created in the AppDelegate + // however, the first window in WindowGroup is closed right away. Hacky solution... + @StateObject private var historyStore = HistoryStore() + @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate init() { AppDelegate.shared = self.appDelegate @@ -18,6 +22,17 @@ struct CalculatorApp: App { var body: some Scene { WindowGroup { ContentView() + .environmentObject(historyStore) + .onAppear { + HistoryStore.load { result in + switch result { + case .failure(let error): + fatalError(error.localizedDescription) + case .success(let history): + historyStore.history = history + } + } + } } } } diff --git a/Calculator/ContentView.swift b/Calculator/ContentView.swift index 8b0bd98..556d477 100644 --- a/Calculator/ContentView.swift +++ b/Calculator/ContentView.swift @@ -9,7 +9,7 @@ import SwiftUI import Expression struct ContentView: View { @State private var showingSettings = false - @StateObject private var store = HistoryStore() + @EnvironmentObject var historyStore: HistoryStore var body: some View { VStack { @@ -44,17 +44,6 @@ struct ContentView: View { } .padding([.horizontal, .bottom]) .frame(maxWidth: .infinity, maxHeight: .infinity) - .environmentObject(store) - .onAppear { - HistoryStore.load { result in - switch result { - case .failure(let error): - fatalError(error.localizedDescription) - case .success(let history): - store.history = history - } - } - } } }