Skip to content

Commit

Permalink
Implement os sleep and awake notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrejJurkin committed Sep 28, 2017
1 parent 1baac3c commit 7c03a4e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
19 changes: 19 additions & 0 deletions Nova/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.initRealm()
self.menuBarView = MenuBarView()
self.fileNotifications()
}

func initRealm() {
Expand All @@ -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)
}
}

13 changes: 13 additions & 0 deletions Nova/view/menubar/MenuBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
11 changes: 9 additions & 2 deletions Nova/view/menubar/MenuBarViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,12 +74,16 @@ class MenuBarViewModel {
})
.filter { $0.count != 0 }
.subscribe(onNext: { tickerSymbols in

// Subscribe for updates for given ticker symbols
self.repo.subscribeForTickerUpdates(baseSymbols: tickerSymbols)
})
.addDisposableTo(disposeBag)

self.repo.subscribeForGlobalUpdates()
}

func unsubscribe() {
self.repo.disposeRefreshSubscriptions()
}
}

0 comments on commit 7c03a4e

Please sign in to comment.