Move generateMenu method to async background task.

This commit is contained in:
Felix Förtsch
2019-01-02 11:07:23 +01:00
parent c41cdb0f80
commit c28e932e84
7 changed files with 249 additions and 24 deletions
+22 -18
View File
@@ -31,7 +31,7 @@ class StatusBarController: NSObject, NSMenuItemValidation {
statusBar.addItem(NSMenuItem.init(title: "Set MatchView", action: #selector(updateView), keyEquivalent: ""))
statusBar.addItem(NSMenuItem.init(title: "Quit", action: #selector(quitClicked), keyEquivalent: ""))
// Set the target to self, so the selectors know what to select.
// Set the target to self, so the selectors know where to select.
for item in statusBar.items {
item.target = self
}
@@ -40,31 +40,35 @@ class StatusBarController: NSObject, NSMenuItemValidation {
statusBar.addItem(NSMenuItem.separator())
//--
statusBar.insertItem(NSMenuItem.init(title: "Refresh", action: #selector(generateMenu), keyEquivalent: ""), at: 3)
performSelector(inBackground: #selector(refreshClicked), with: nil)
}
@objc func generateMenu(for matches: [Match]) {
for item in statusBar.items {
if item.tag == 1 {
statusBar.removeItem(item)
}
}
for match in matches {
let newItem = NSMenuItem.init(title: match.league + match.team1name + match.team1score + ":" + match.team2score + match.team2name, action: nil, keyEquivalent: "")
newItem.tag = 1
newItem.target = self
statusBar.addItem(newItem)
}
}
@objc func refreshClicked(_ sender: NSMenuItem) {
DispatchQueue.global().async {
// TODO: Add Spinner to indicate that loading is going on.
for item in self.statusBar.items {
if item.tag == 1 {
self.statusBar.removeItem(item)
}
}
if let matches = self.matchesAPI.fetchMatches(for: "dota2") {
self.generateMenu(for: matches)
for match in matches {
let newItem = NSMenuItem.init(title: match.league + match.team1name + match.team1score + ":" + match.team2score + match.team2name, action: nil, keyEquivalent: "")
newItem.tag = 1
newItem.target = self
self.statusBar.addItem(newItem)
}
} else {
let newItem = NSMenuItem.init(title: "There was an error receiving ongoing matches.", action: nil, keyEquivalent: "")
newItem.tag = 1
newItem.target = self
self.statusBar.addItem(newItem)
}
}
}
@objc func updateView() {