Branchwell

Jobley / app · jobley.ai / Apps / Mac / Updates.swift

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