This commit is contained in:
Max Goedjen 2022-01-18 16:41:20 -05:00
parent e86b9d2465
commit 2100803e0d
No known key found for this signature in database
GPG Key ID: E58C21DD77B9B8E8

View File

@ -44,24 +44,18 @@ public actor Updater: ObservableObject, UpdaterProtocol {
/// Manually trigger an update check. /// Manually trigger an update check.
public func checkForUpdates() async { public func checkForUpdates() async {
if #available(macOS 12.0, *) {
guard let (data, _) = try? await URLSession.shared.data(from: Constants.updateURL), guard let (data, _) = try? await URLSession.shared.data(from: Constants.updateURL),
let releases = try? JSONDecoder().decode([Release].self, from: data) else { return } let releases = try? JSONDecoder().decode([Release].self, from: data) else { return }
self.evaluate(releases: releases) await evaluate(releases: releases)
} else {
// Fallback on earlier versions
}
} }
/// Ignores a specified release. `update` will be nil if the user has ignored the latest available release. /// Ignores a specified release. `update` will be nil if the user has ignored the latest available release.
/// - Parameter release: The release to ignore. /// - Parameter release: The release to ignore.
public func ignore(release: Release) { public func ignore(release: Release) async {
guard !release.critical else { return } guard !release.critical else { return }
defaults.set(true, forKey: release.name) defaults.set(true, forKey: release.name)
Task {
await setUpdate(update: update) await setUpdate(update: update)
} }
}
} }
@ -69,7 +63,7 @@ extension Updater {
/// Evaluates the available downloadable releases, and selects the newest non-prerelease release that the user is able to run. /// Evaluates the available downloadable releases, and selects the newest non-prerelease release that the user is able to run.
/// - Parameter releases: An array of ``Release`` objects. /// - Parameter releases: An array of ``Release`` objects.
func evaluate(releases: [Release]) { func evaluate(releases: [Release]) async {
guard let release = releases guard let release = releases
.sorted() .sorted()
.reversed() .reversed()
@ -79,11 +73,9 @@ extension Updater {
guard !release.prerelease else { return } guard !release.prerelease else { return }
let latestVersion = SemVer(release.name) let latestVersion = SemVer(release.name)
if latestVersion > currentVersion { if latestVersion > currentVersion {
Task {
await setUpdate(update: update) await setUpdate(update: update)
} }
} }
}
@MainActor private func setUpdate(update: Release?) { @MainActor private func setUpdate(update: Release?) {
self.update = update self.update = update
@ -111,3 +103,21 @@ extension Updater {
} }
} }
@available(macOS, deprecated: 12)
extension URLSession {
// Backport for macOS 11
func data(from url: URL) async throws -> (Data, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, let response = response else {
continuation.resume(throwing: error ?? NSError(domain: NSURLErrorDomain, code: NSURLErrorUnknown, userInfo: nil))
return
}
continuation.resume(returning: (data, response))
}
}
}
}