|
| 1 | +import Sparkle |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +final class UpdaterService: NSObject, ObservableObject { |
| 5 | + private lazy var inner: SPUStandardUpdaterController = .init( |
| 6 | + startingUpdater: true, |
| 7 | + updaterDelegate: self, |
| 8 | + userDriverDelegate: self, |
| 9 | + ) |
| 10 | + private var updater: SPUUpdater! |
| 11 | + @Published var canCheckForUpdates = true |
| 12 | + |
| 13 | + @Published var autoCheckForUpdates: Bool = false { |
| 14 | + didSet { |
| 15 | + if autoCheckForUpdates != oldValue { |
| 16 | + updater.automaticallyChecksForUpdates = autoCheckForUpdates |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + @Published var updateChannel: UpdateChannel { |
| 22 | + didSet { |
| 23 | + UserDefaults.standard.set(updateChannel.rawValue, forKey: Self.updateChannelKey) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static let updateChannelKey = "updateChannel" |
| 28 | + |
| 29 | + override init() { |
| 30 | + updateChannel = UserDefaults.standard.string(forKey: Self.updateChannelKey) |
| 31 | + .flatMap { UpdateChannel(rawValue: $0) } ?? .stable |
| 32 | + super.init() |
| 33 | + updater = inner.updater |
| 34 | + updater.publisher(for: \.canCheckForUpdates).assign(to: &$canCheckForUpdates) |
| 35 | + } |
| 36 | + |
| 37 | + func checkForUpdates() { |
| 38 | + guard canCheckForUpdates else { return } |
| 39 | + updater.checkForUpdates() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +enum UpdateChannel: String, CaseIterable, Identifiable { |
| 44 | + case stable |
| 45 | + case preview |
| 46 | + |
| 47 | + var name: String { |
| 48 | + switch self { |
| 49 | + case .stable: |
| 50 | + "Stable" |
| 51 | + case .preview: |
| 52 | + "Preview" |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + var id: String { rawValue } |
| 57 | +} |
| 58 | + |
| 59 | +extension UpdaterService: SPUUpdaterDelegate { |
| 60 | + func allowedChannels(for _: SPUUpdater) -> Set<String> { |
| 61 | + // There's currently no point in subscribing to both channels, as |
| 62 | + // preview >= stable |
| 63 | + [updateChannel.rawValue] |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +extension UpdaterService: SUVersionDisplay { |
| 68 | + func formatUpdateVersion( |
| 69 | + fromUpdate update: SUAppcastItem, |
| 70 | + andBundleDisplayVersion inOutBundleDisplayVersion: AutoreleasingUnsafeMutablePointer<NSString>, |
| 71 | + withBundleVersion bundleVersion: String |
| 72 | + ) -> String { |
| 73 | + // Replace CFBundleShortVersionString with CFBundleVersion, as the |
| 74 | + // latter shows build numbers. |
| 75 | + inOutBundleDisplayVersion.pointee = bundleVersion as NSString |
| 76 | + // This is already CFBundleVersion, as that's the only version in the |
| 77 | + // appcast. |
| 78 | + return update.displayVersionString |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +extension UpdaterService: SPUStandardUserDriverDelegate { |
| 83 | + func standardUserDriverRequestsVersionDisplayer() -> (any SUVersionDisplay)? { |
| 84 | + self |
| 85 | + } |
| 86 | +} |
0 commit comments