Skip to content

Commit

Permalink
changed version checker logic
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Sep 4, 2019
1 parent 35bcec8 commit e16bb5c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

### [v1.2.13]
- changed version checker logic

### [v1.2.12]
- module settings are unavailable if the module is turned off
- fixed battery module visibility when disabled
Expand Down Expand Up @@ -78,6 +81,7 @@ All notable changes to this project will be documented in this file.
### [v1.0.0]
- first release

[v1.2.13]: https:/exelban/stats/releases/tag/v1.2.13
[v1.2.12]: https:/exelban/stats/releases/tag/v1.2.12
[v1.2.11]: https:/exelban/stats/releases/tag/v1.2.11
[v1.2.10]: https:/exelban/stats/releases/tag/v1.2.10
Expand Down
2 changes: 1 addition & 1 deletion Stats/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.2.12</string>
<string>1.2.13</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
Expand Down
37 changes: 28 additions & 9 deletions Stats/libs/macAppUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ struct version {
let url: String
}

struct Version {
var major: Int = 0
var minor: Int = 0
var patch: Int = 0
}

public class macAppUpdater {
let user: String
let repo: String
Expand Down Expand Up @@ -61,16 +67,29 @@ public class macAppUpdater {
task.resume()
}

func checkIfNewer(current: String, latest: String) -> Bool {
guard let currentNumber: Int64 = Int64(current.replacingOccurrences(of: "[v.]", with: "", options: [.regularExpression])) else {
print("Error: wrong version tag \(current)")
return false
func checkIfNewer(currentVersion: String, latestVersion: String) -> Bool {
let currentNumber = currentVersion.replacingOccurrences(of: "v", with: "")
let latestNumber = latestVersion.replacingOccurrences(of: "v", with: "")

let currentArray = currentNumber.condenseWhitespace().split(separator: ".")
let latestArray = latestNumber.condenseWhitespace().split(separator: ".")

let current = Version(major: Int(currentArray[0]) ?? 0, minor: Int(currentArray[1]) ?? 0, patch: Int(currentArray[2]) ?? 0)
let latest = Version(major: Int(latestArray[0]) ?? 0, minor: Int(latestArray[1]) ?? 0, patch: Int(latestArray[2]) ?? 0)

if latest.major > current.major {
return true
}
guard let latestNumber: Int64 = Int64(latest.replacingOccurrences(of: "[v.]", with: "", options: [.regularExpression])) else {
print("Error: wrong version tag \(latest)")
return false

if latest.minor > current.minor && latest.major >= current.major {
return true
}
return latestNumber>currentNumber

if latest.patch > current.patch && latest.minor >= current.minor && latest.major >= current.major {
return true
}

return false
}

func check(completionHandler: @escaping (_ result: version?, _ error: Error?) -> Void) {
Expand All @@ -92,7 +111,7 @@ public class macAppUpdater {

let downloadURL: String = result![1]
let lastVersion: String = result![0]
let newVersion: Bool = self.checkIfNewer(current: self.currentVersion, latest: lastVersion)
let newVersion: Bool = self.checkIfNewer(currentVersion: self.currentVersion, latestVersion: lastVersion)

completionHandler(version(current: self.currentVersion, latest: lastVersion, newest: newVersion, url: downloadURL), nil)
}
Expand Down

0 comments on commit e16bb5c

Please sign in to comment.