prepare for async work

This commit is contained in:
Felix Förtsch
2018-10-12 15:23:24 +02:00
committed by Felix Förtsch
parent e72b0f3aba
commit 3b8c44d233
3 changed files with 9 additions and 16 deletions

View File

@@ -9,10 +9,6 @@
import Foundation
import SwiftSoup
protocol MatchesAPIDelegate {
}
class MatchesAPI {
func fetchMatches(for game: String) -> [Match]? {
@@ -26,13 +22,9 @@ class MatchesAPI {
private func fetchData(for game: String) -> String? {
let url = constructURL(for: game)
// DispatchQueue.global().async { [unowned self] in
if let url = URL(string: url) {
if let data = try? String(contentsOf: url) {
return data
}
}
// }
if let data = try? String(contentsOf: url) {
return data
}
return nil
}
@@ -73,10 +65,10 @@ class MatchesAPI {
}
}
private func constructURL(for game: String) -> String {
private func constructURL(for game: String) -> URL {
let baseURL = "https://liquipedia.net/"
let query = "/api.php?action=parse&page=Liquipedia:Upcoming_and_ongoing_matches&format=json&prop=text"
return baseURL + game + query
return URL(string: baseURL + game + query)!
}
}

View File

@@ -9,12 +9,13 @@
import Cocoa
class StatusBarController: NSObject, NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
return true
}
let statusBar = NSMenu()
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let matchesAPI = MatchesAPI()
var matches = [Match]()
@@ -22,8 +23,8 @@ class StatusBarController: NSObject, NSMenuItemValidation {
// Set the icon of the statusbar item and put it into the statusbar
let icon = NSImage(named: "statusBarIcon")
icon?.isTemplate = true
statusItem.image = icon
statusItem.menu = statusBar
statusBarItem.image = icon
statusBarItem.menu = statusBar
generateMenu()
}