Branchwell

PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / AppUpdater.swift

Raw 4.3 KB on 23ffa4e102ea595ec83f2b18683b75332fc815c3
1#if DIRECT_BUILD && canImport(Sparkle) && !LOCAL_BUILD
2import Combine
3import Sparkle
4import SwiftUI
5
6/// The direct-download edition retains Sparkle for the lifetime of the app.
7/// App Store updates are delivered exclusively by Apple.
8@MainActor final class AppUpdater: NSObject, ObservableObject, SPUUpdaterDelegate, SPUStandardUserDriverDelegate {
9 private var controller: SPUStandardUpdaterController!
10 private var observation: NSKeyValueObservation?
11 private var activation: AnyCancellable?
12 private var probeTimer: Timer?
13 private var lastProbe: Date?
14 private var started = false
15 @Published private(set) var availableVersion: String?
16 @Published private(set) var canCheck = false
17 @Published var automaticChecks = false {
18 didSet {
19 guard started else { return }
20 controller.updater.automaticallyChecksForUpdates = automaticChecks
21 if automaticChecks { lastProbe = nil; probeIfNeeded() }
22 }
23 }
24 override init() {
25 super.init()
26 controller = SPUStandardUpdaterController(startingUpdater: false, updaterDelegate: self, userDriverDelegate: self)
27 automaticChecks = controller.updater.automaticallyChecksForUpdates
28 observation = controller.updater.observe(\.canCheckForUpdates, options: [.initial, .new]) { [weak self] updater, _ in
29 let ready = updater.canCheckForUpdates
30 Task { @MainActor in
31 self?.canCheck = ready
32 if ready { self?.probeIfNeeded() }
33 }
34 }
35 controller.startUpdater()
36 started = true
37 activation = NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification).sink { [weak self] _ in
38 Task { @MainActor in self?.probeIfNeeded() }
39 }
40 probeTimer = Timer.scheduledTimer(withTimeInterval: 900, repeats: true) { [weak self] _ in
41 Task { @MainActor in self?.probeIfNeeded() }
42 }
43 probeIfNeeded()
44 }
45 deinit { probeTimer?.invalidate() }
46
47 private func probeIfNeeded() {
48 guard started, automaticChecks, availableVersion == nil,
49 controller.updater.canCheckForUpdates, !controller.updater.sessionInProgress,
50 lastProbe.map({ Date().timeIntervalSince($0) >= 900 }) ?? true else { return }
51 lastProbe = Date()
52 controller.updater.checkForUpdateInformation()
53 }
54 func checkForUpdates() {
55 guard controller.updater.canCheckForUpdates else { return }
56 controller.checkForUpdates(nil)
57 }
58
59 nonisolated func updater(_ updater: SPUUpdater, didFindValidUpdate item: SUAppcastItem) {
60 let version = item.displayVersionString
61 Task { @MainActor [weak self] in self?.availableVersion = version }
62 }
63 nonisolated func updaterDidNotFindUpdate(_ updater: SPUUpdater, error: any Error) {
64 Task { @MainActor [weak self] in self?.availableVersion = nil }
65 }
66 nonisolated func updater(_ updater: SPUUpdater, userDidMake choice: SPUUserUpdateChoice,
67 forUpdate updateItem: SUAppcastItem, state: SPUUserUpdateState) {
68 if choice == .skip {
69 Task { @MainActor [weak self] in self?.availableVersion = nil }
70 }
71 }
72 nonisolated var supportsGentleScheduledUpdateReminders: Bool { true }
73 nonisolated func standardUserDriverShouldHandleShowingScheduledUpdate(_ update: SUAppcastItem, andInImmediateFocus immediateFocus: Bool) -> Bool { false }
74 nonisolated func standardUserDriverWillHandleShowingUpdate(_ handleShowingUpdate: Bool,
75 forUpdate update: SUAppcastItem, state: SPUUserUpdateState) {
76 let version = update.displayVersionString
77 Task { @MainActor [weak self] in self?.availableVersion = version }
78 }
79}
80
81struct AppUpdateButton: View {
82 @ObservedObject var updater: AppUpdater
83 var body: some View {
84 if let version = updater.availableVersion {
85 Button { updater.checkForUpdates() } label: {
86 Image(systemName: "tray.and.arrow.down.fill").font(.system(size: 17, weight: .semibold))
87 }
88 .buttonStyle(StudioButtonStyle(treatment: .primary, circular: true))
89 .disabled(!updater.canCheck)
90 .help("Update \(version) is available. Click to install.")
91 .accessibilityLabel("Install update \(version)")
92 }
93 }
94}
95#endif