import Foundation import Sparkle @MainActor final class SparkleUpdater: NSObject, SPUUserDriver, SPUUpdaterDelegate { private let updates: ReleaseUpdates private lazy var updater = SPUUpdater(hostBundle: .main, applicationBundle: .main, userDriver: self, delegate: self) private var started = false private var requestedBuild: String? private var expectedBytes: UInt64 = 0 private var receivedBytes: UInt64 = 0 init(updates: ReleaseUpdates) { self.updates = updates super.init() updates.changeAutomatic = { [weak self] enabled in self?.updater.automaticallyChecksForUpdates = enabled } updates.beginCheck = { [weak self] in self?.check() } updates.beginDownload = { [weak self] in self?.check(download: $0) } } func start() { guard prepare() else { return } if updates.automatic { updater.checkForUpdatesInBackground() } } private func prepare() -> Bool { if started { return true } do { if updater.automaticallyChecksForUpdates != updates.automatic { updater.automaticallyChecksForUpdates = updates.automatic } try updater.start() started = true return true } catch { updates.transition(to: .failed, error: error.localizedDescription) return false } } private func check(download: ReleaseUpdates.Version? = nil) { guard prepare(), !updater.sessionInProgress else { return } requestedBuild = download?.build updates.transition(to: .checking) updater.checkForUpdates() } func show(_ request: SPUUpdatePermissionRequest, reply: @escaping (SUUpdatePermissionResponse) -> Void) { reply(SUUpdatePermissionResponse(automaticUpdateChecks: updates.automatic, sendSystemProfile: false)) } func showUserInitiatedUpdateCheck(cancellation: @escaping () -> Void) {} func showUpdateFound(with appcastItem: SUAppcastItem, state: SPUUserUpdateState, reply: @escaping (SPUUserUpdateChoice) -> Void) { guard state.userInitiated || updates.automatic else { reply(.dismiss); return } let version = ReleaseUpdates.Version(display: appcastItem.displayVersionString, build: appcastItem.versionString, infoURL: appcastItem.isInformationOnlyUpdate ? appcastItem.infoURL : nil) updates.found(version) if state.stage == .installing { updates.ready { reply(.install) } } else if state.userInitiated, requestedBuild == version.build, !appcastItem.isInformationOnlyUpdate { updates.transition(to: .downloading) reply(.install) } else { // Keep the badge, release the session so Sparkle can check again in six hours. reply(.dismiss) } } func showUpdateReleaseNotes(with downloadData: SPUDownloadData) {} func showUpdateReleaseNotesFailedToDownloadWithError(_ error: Error) {} func showUpdateNotFoundWithError(_ error: Error, acknowledgement: @escaping () -> Void) { updates.transition(to: .current) acknowledgement() } func showUpdaterError(_ error: Error, acknowledgement: @escaping () -> Void) { updates.transition(to: .failed, error: error.localizedDescription) acknowledgement() } func showDownloadInitiated(cancellation: @escaping () -> Void) { expectedBytes = 0 receivedBytes = 0 updates.transition(to: .downloading) } func showDownloadDidReceiveExpectedContentLength(_ expectedContentLength: UInt64) { expectedBytes = expectedContentLength } func showDownloadDidReceiveData(ofLength length: UInt64) { receivedBytes += length let fraction = expectedBytes > 0 ? min(1, Double(receivedBytes) / Double(expectedBytes)) : nil updates.transition(to: .downloading, fraction: fraction) } func showDownloadDidStartExtractingUpdate() { updates.transition(to: .extracting) } func showExtractionReceivedProgress(_ progress: Double) { updates.transition(to: .extracting, fraction: progress) } func showReady(toInstallAndRelaunch reply: @escaping (SPUUserUpdateChoice) -> Void) { updates.ready { reply(.install) } } func showInstallingUpdate(withApplicationTerminated terminated: Bool, retryTerminatingApplication: @escaping () -> Void) { if !terminated { updates.ready(restart: retryTerminatingApplication) } } func showUpdateInstalledAndRelaunched(_ relaunched: Bool, acknowledgement: @escaping () -> Void) { updates.transition(to: .current) acknowledgement() } func dismissUpdateInstallation() {} func updater(_ updater: SPUUpdater, didFinishUpdateCycleFor updateCheck: SPUUpdateCheck, error: Error?) { requestedBuild = nil guard let error = error as NSError? else { return } if error.domain == SUSparkleErrorDomain, error.code == SUError.noUpdateError.rawValue { updates.transition(to: .current) } else { updates.transition(to: .failed, error: error.localizedDescription) } } }