Skip to content

Commit

Permalink
#35: Add a check for update solution
Browse files Browse the repository at this point in the history
- Use the tag name instead of name to detect updates
  • Loading branch information
Timac committed Apr 11, 2024
1 parent 3db8d2a commit ebe82aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
26 changes: 13 additions & 13 deletions VPNAppTests/GitHubReleaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import XCTest
final class GitHubReleaseTests: XCTestCase {

func testGitHubReleaseComparison() throws {
let release_1_0 = GitHubRelease(name: "1.0", prerelease: false, draft: false, body: nil)
let release_1_1 = GitHubRelease(name: "1.1", prerelease: false, draft: false, body: nil)
let release_1_2 = GitHubRelease(name: "1.2", prerelease: false, draft: false, body: nil)
let release_1_10 = GitHubRelease(name: "1.10", prerelease: false, draft: false, body: nil)
let release_1_0_0 = GitHubRelease(name: "1.0.0", prerelease: false, draft: false, body: nil)
let release_1_0_1 = GitHubRelease(name: "1.0.1", prerelease: false, draft: false, body: nil)
let release_1_1_1 = GitHubRelease(name: "1.1.1", prerelease: false, draft: false, body: nil)
let release_1_10_1 = GitHubRelease(name: "1.10.1", prerelease: false, draft: false, body: nil)
let release_2_0 = GitHubRelease(name: "2.0", prerelease: false, draft: false, body: nil)
let release_3_0 = GitHubRelease(name: "3.0", prerelease: false, draft: false, body: nil)
let release_1_0 = GitHubRelease(tag_name: "1.0", prerelease: false, draft: false, body: nil)
let release_1_1 = GitHubRelease(tag_name: "1.1", prerelease: false, draft: false, body: nil)
let release_1_2 = GitHubRelease(tag_name: "1.2", prerelease: false, draft: false, body: nil)
let release_1_10 = GitHubRelease(tag_name: "1.10", prerelease: false, draft: false, body: nil)
let release_1_0_0 = GitHubRelease(tag_name: "1.0.0", prerelease: false, draft: false, body: nil)
let release_1_0_1 = GitHubRelease(tag_name: "1.0.1", prerelease: false, draft: false, body: nil)
let release_1_1_1 = GitHubRelease(tag_name: "1.1.1", prerelease: false, draft: false, body: nil)
let release_1_10_1 = GitHubRelease(tag_name: "1.10.1", prerelease: false, draft: false, body: nil)
let release_2_0 = GitHubRelease(tag_name: "2.0", prerelease: false, draft: false, body: nil)
let release_3_0 = GitHubRelease(tag_name: "3.0", prerelease: false, draft: false, body: nil)


XCTAssertTrue(release_1_0 < release_1_1)
Expand Down Expand Up @@ -60,9 +60,9 @@ final class GitHubReleaseTests: XCTestCase {
}

func testGitHubReleasePrereleaseDraft() throws {
let release_1_0 = GitHubRelease(name: "1.0", prerelease: false, draft: false, body: nil)
let release_1_1 = GitHubRelease(name: "1.1", prerelease: true, draft: false, body: nil)
let release_1_2 = GitHubRelease(name: "1.2", prerelease: true, draft: false, body: nil)
let release_1_0 = GitHubRelease(tag_name: "1.0", prerelease: false, draft: false, body: nil)
let release_1_1 = GitHubRelease(tag_name: "1.1", prerelease: true, draft: false, body: nil)
let release_1_2 = GitHubRelease(tag_name: "1.2", prerelease: true, draft: false, body: nil)

XCTAssertTrue(release_1_0 < release_1_1)
XCTAssertTrue(release_1_1 < release_1_2)
Expand Down
4 changes: 2 additions & 2 deletions VPNStatus/CheckForUpdate/GitHubRelease.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

public struct GitHubRelease: Decodable, Comparable {
let name: String
let tag_name: String // "1.0", "1.0.1", "1.2", "2.0"
let prerelease: Bool
let draft: Bool
let body: String?
Expand Down Expand Up @@ -43,7 +43,7 @@ public struct GitHubRelease: Decodable, Comparable {
}

public func components() -> (major: Int, minor: Int, patch: Int) {
var components = self.name.split(separator: ".").compactMap { Int($0) }
var components = self.tag_name.split(separator: ".").compactMap { Int($0) }
while components.count != 3 {
components.append(0)
}
Expand Down
14 changes: 7 additions & 7 deletions VPNStatus/CheckForUpdate/UpdateManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import Foundation
let currentRelease: GitHubRelease

override private init() {
let appShortVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
if let appShortVersion = appShortVersion {
currentRelease = GitHubRelease(name: appShortVersion, prerelease: false, draft: false, body: nil)
} else {
currentRelease = GitHubRelease(name: "1.0", prerelease: false, draft: false, body: nil)
var version = "1.0"
if let appShortVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
version = appShortVersion
}

currentRelease = GitHubRelease(tag_name: version, prerelease: false, draft: false, body: nil)
}

@objc public func checkForUpdate(skippedVersion: String?, completion: @escaping (String?, String?, String?) -> Void) {
Expand Down Expand Up @@ -56,7 +56,7 @@ import Foundation
debugPrint("A new version is available")

if let skippedVersion = skippedVersion {
let skippedRelease = GitHubRelease(name: skippedVersion, prerelease: false, draft: false, body: nil)
let skippedRelease = GitHubRelease(tag_name: skippedVersion, prerelease: false, draft: false, body: nil)
if lastRelease <= skippedRelease {
debugPrint("Skip the new version")
DispatchQueue.main.async {
Expand All @@ -68,7 +68,7 @@ import Foundation
}

DispatchQueue.main.async {
completion(self.currentRelease.name, lastRelease.name, lastRelease.body)
completion(self.currentRelease.tag_name, lastRelease.tag_name, lastRelease.body)
}
return
} else {
Expand Down

0 comments on commit ebe82aa

Please sign in to comment.