diff --git a/Nova/AppDelegate.swift b/Nova/AppDelegate.swift index 8e429f4..dc20381 100644 --- a/Nova/AppDelegate.swift +++ b/Nova/AppDelegate.swift @@ -35,6 +35,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { self.initRealm() self.menuBarView = MenuBarView() + self.fileNotifications() } func initRealm() { @@ -49,5 +50,23 @@ class AppDelegate: NSObject, NSApplicationDelegate { realm.deleteAll() } } + + func onWakeNote(note: NSNotification) { + self.menuBarView?.resume() + } + + func onSleepNote(note: NSNotification) { + self.menuBarView?.pause() + } + + func fileNotifications() { + NSWorkspace.shared().notificationCenter.addObserver( + self, selector: #selector(onWakeNote(note:)), + name: Notification.Name.NSWorkspaceDidWake, object: nil) + + NSWorkspace.shared().notificationCenter.addObserver( + self, selector: #selector(onSleepNote(note:)), + name: Notification.Name.NSWorkspaceWillSleep, object: nil) + } } diff --git a/Nova/view/menubar/MenuBarView.swift b/Nova/view/menubar/MenuBarView.swift index f8bfca1..2ba5a8b 100644 --- a/Nova/view/menubar/MenuBarView.swift +++ b/Nova/view/menubar/MenuBarView.swift @@ -88,7 +88,20 @@ class MenuBarView: NSObject { self.statusItem.attributedTitle = title } + func refresh() { + self.viewModel.subscribe() + } + + func pause() { + self.viewModel.unsubscribe() + } + + func resume() { + self.viewModel.subscribe() + } + private func bindUi() { + self.viewModel.subscribe() self.viewModel.menuBarText.asObservable().subscribe(onNext: { text in self.setStatusItemTitle(title: text) }) diff --git a/Nova/view/menubar/MenuBarViewModel.swift b/Nova/view/menubar/MenuBarViewModel.swift index 10b7a09..ee36086 100644 --- a/Nova/view/menubar/MenuBarViewModel.swift +++ b/Nova/view/menubar/MenuBarViewModel.swift @@ -35,9 +35,12 @@ class MenuBarViewModel { init(repo: DataRepository, prefs: Prefs) { self.repo = repo self.prefs = prefs - + } + + func subscribe() { // Watch when list with pinned tickers changes self.repo.getPinnedTickers() + .retry(5) .flatMap({ tickers -> Observable<[String]> in // If we don't have any pinned tickers, show app name @@ -71,7 +74,7 @@ class MenuBarViewModel { }) .filter { $0.count != 0 } .subscribe(onNext: { tickerSymbols in - + // Subscribe for updates for given ticker symbols self.repo.subscribeForTickerUpdates(baseSymbols: tickerSymbols) }) @@ -79,4 +82,8 @@ class MenuBarViewModel { self.repo.subscribeForGlobalUpdates() } + + func unsubscribe() { + self.repo.disposeRefreshSubscriptions() + } }