Glimmer / app · glimmertoday.ai / native / Glimmer / Updates.swift
| 1 | #if os(macOS) |
| 2 | import Combine |
| 3 | import Sparkle |
| 4 | import SwiftUI |
| 5 | |
| 6 | /// Sparkle updates for the Mac build downloaded from glimmertoday.ai. |
| 7 | /// Debug builds and builds without a feed or signing key leave the updater off. |
| 8 | @MainActor |
| 9 | final class AppUpdates: ObservableObject { |
| 10 | @Published private(set) var canCheck = false |
| 11 | private let controller: SPUStandardUpdaterController? |
| 12 | |
| 13 | init() { |
| 14 | #if DEBUG |
| 15 | controller = nil |
| 16 | #else |
| 17 | guard Self.configured("SUFeedURL"), Self.configured("SUPublicEDKey") else { |
| 18 | controller = nil |
| 19 | return |
| 20 | } |
| 21 | let controller = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil) |
| 22 | self.controller = controller |
| 23 | controller.updater.publisher(for: \.canCheckForUpdates).assign(to: &$canCheck) |
| 24 | #endif |
| 25 | } |
| 26 | |
| 27 | var isAvailable: Bool { controller != nil } |
| 28 | |
| 29 | func check() { controller?.checkForUpdates(nil) } |
| 30 | |
| 31 | private static func configured(_ key: String) -> Bool { |
| 32 | !((Bundle.main.object(forInfoDictionaryKey: key) as? String) ?? "").isEmpty |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | struct CheckForUpdatesButton: View { |
| 37 | @ObservedObject var updates: AppUpdates |
| 38 | |
| 39 | var body: some View { |
| 40 | Button("Check for Updates…") { updates.check() } |
| 41 | .disabled(!updates.canCheck) |
| 42 | } |
| 43 | } |
| 44 | #endif |