Skip to content
This repository has been archived by the owner on Nov 12, 2021. It is now read-only.

Commit

Permalink
Added feature to alert user if there is an update on Github.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavo-IM committed Jan 20, 2019
1 parent f680d57 commit 3203ffd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion AGPMInjector/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>2.3</string>
<key>CFBundleVersion</key>
<string>3</string>
<string>4</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
64 changes: 64 additions & 0 deletions AGPMInjector/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,75 @@ class ViewController: NSViewController {
popButton.selectItem(at: 0)
yesChecked.state = NSControlStateValueOff

let versionNumberString = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
let buildNumberString = Bundle.main.infoDictionary?["CFBundleVersion"] as? String
struct Response: Codable {
let tag_name: String
}

var parsedVersion = ""

func parse(json: Data) {
let decoder = JSONDecoder()
if let latestRelease = try? decoder.decode(Response.self, from: json) {
parsedVersion = latestRelease.tag_name
}
}

let urlString = "https://api.github.com/repos/Pavo-IM/AGPMInjector/releases/latest"
if let url = URL(string: urlString) {
if let data = try? Data(contentsOf: url) {
parse(json: data)
}
}

let installedVersion = versionNumberString! + "." + buildNumberString!
let latestVersionComponents = parsedVersion.components(separatedBy: ".")
let installedVersionComponents = installedVersion.components(separatedBy: ".")

for (n, component) in latestVersionComponents.enumerated() {
guard let latestValue = Int(component) else {
let alert = NSAlert()
alert.alertStyle = .critical
alert.messageText = "Unrecognized version"
alert.informativeText = "Unrecognized version. Please issue a bug report at https://github.com/Pavo-IM/AGPMInjector/issues!"
alert.runModal()
break
}

guard let installedValue = Int(installedVersionComponents[n]) else {
let alert = NSAlert()
alert.alertStyle = .critical
alert.messageText = "Unrecognized version"
alert.informativeText = "Unrecognized version. Please issue a bug report at https://github.com/Pavo-IM/AGPMInjector/issues!"
alert.runModal()
break
}

if installedValue > latestValue {
let alert = NSAlert()
alert.alertStyle = .critical
alert.messageText = "Unrecognized version"
alert.informativeText = "Very interesting... installed version \(installedVersion) is newer than reported latest version \(parsedVersion)!. Please issue a bug report at https://github.com/Pavo-IM/AGPMInjector/issues!"
alert.runModal()
break
}

if latestValue > installedValue {
let alert = NSAlert()
alert.messageText = "Update Available!"
alert.informativeText = "Latest version is \(parsedVersion) but you are running \(installedVersion). Download the latest update at https://github.com/Pavo-IM/AGPMInjector/releases"
alert.runModal()
break
}
}
}

override var representedObject: Any? {
didSet {
}
}

@IBAction func generateButton(_ sender: Any) {
func saveAlert () {
let fileManager = FileManager.default
Expand Down

0 comments on commit 3203ffd

Please sign in to comment.