Branchwell

Graphley / app · graphley.ai / Apps / Mac / Updates.swift

Raw 1.3 KB on d69ca9c4c8ff088be085e80dbb7478e9463bddf8
1#if os(macOS)
2import Combine
3import Sparkle
4import SwiftUI
5
6/// Sparkle updates for the Mac build downloaded from graphley.ai.
7/// Debug builds and builds without a feed or signing key leave the updater off.
8@MainActor
9final 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
36struct 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