Skip to content

Commit

Permalink
History environment object shared between popover and window
Browse files Browse the repository at this point in the history
  • Loading branch information
antingle committed May 2, 2022
1 parent a422f24 commit b1b096f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
7 changes: 3 additions & 4 deletions Calculator/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand All @@ -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)
}
Expand All @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions Calculator/CalculatorApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
}
}
}
}
13 changes: 1 addition & 12 deletions Calculator/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
}
}
}

Expand Down

0 comments on commit b1b096f

Please sign in to comment.