Skip to content

Commit

Permalink
add update check
Browse files Browse the repository at this point in the history
  • Loading branch information
streetturtle committed Feb 5, 2022
1 parent 45e4801 commit b629f28
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pullBar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ extension AppDelegate {
self.menu.addItem(withTitle: "Refresh", action: #selector(self.refreshMenu), keyEquivalent: "")
self.menu.addItem(.separator())
self.menu.addItem(withTitle: "Preferences...", action: #selector(self.openPrefecencesWindow), keyEquivalent: "")
self.menu.addItem(withTitle: "Check for updates...", action: #selector(self.checkForUpdates), keyEquivalent: "")
self.menu.addItem(withTitle: "About PullBar", action: #selector(self.openAboutWindow), keyEquivalent: "")
self.menu.addItem(withTitle: "Quit", action: #selector(self.quit), keyEquivalent: "")
}
Expand Down Expand Up @@ -327,6 +328,41 @@ extension AppDelegate {
}
}

@objc
func checkForUpdates(_: NSStatusBarButton?) {
let currentVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
ghClient.getLatestRelease { latestRelease in
if let latestRelease = latestRelease {
let versionComparison = currentVersion.compare(latestRelease.name.replacingOccurrences(of: "v", with: ""), options: .numeric)
if versionComparison == .orderedAscending {
self.downloadNewVersionDialog(link: latestRelease.assets[0].browserDownloadUrl)
} else {
self.dialogWithText(text: "You have the latest version installed!")
}
}
}
}

func dialogWithText(text: String) -> Void {
let alert = NSAlert()
alert.messageText = text
alert.alertStyle = .informational
alert.addButton(withTitle: "OK")
alert.runModal()
}

func downloadNewVersionDialog(link: String) -> Void {
let alert = NSAlert()
alert.messageText = "New version is available!"
alert.alertStyle = .informational
alert.addButton(withTitle: "Download")
alert.addButton(withTitle: "Cancel")
let pressedButton = alert.runModal()
if (pressedButton == .alertFirstButtonReturn) {
NSWorkspace.shared.open(URL(string: link)!)
}
}

@objc
func quit(_: NSStatusBarButton) {
NSLog("User click Quit")
Expand Down
26 changes: 26 additions & 0 deletions pullBar/GitHub/GitHubClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,32 @@ public class GitHubClient {
"""
}

func getLatestRelease(completion:@escaping (((LatestRelease?) -> Void))) -> Void {
let headers: HTTPHeaders = [
.authorization(username: githubUsername, password: githubToken),
.contentType("application/json"),
.accept("application/json")
]
AF.request("https://api.github.com/repos/menubar-apps/PullBar/releases/latest",
method: .get,
encoding: JSONEncoding.default,
headers: headers)
.validate(statusCode: 200..<300)
.responseDecodable(of: LatestRelease.self) { response in
switch response.result {
case .success(let latestRelease):
completion(latestRelease)
case .failure(let error):
completion(nil)
if let data = response.data {
let json = String(data: data, encoding: String.Encoding.utf8)
// print("Failure Response: \(json)")
}
sendNotification(body: error.localizedDescription)
}
}
}
}

class GithubDecoder: JSONDecoder {
Expand Down
20 changes: 20 additions & 0 deletions pullBar/GitHub/GitHubDtos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,23 @@ struct Check: Codable {
}
}

struct LatestRelease: Codable {

var name: String
var assets: [Asset]

enum CodingKeys: String, CodingKey {
case name
case assets
}
}

struct Asset: Codable {
var name: String
var browserDownloadUrl: String

enum CodingKeys: String, CodingKey {
case name
case browserDownloadUrl = "browser_download_url"
}
}

0 comments on commit b629f28

Please sign in to comment.