Sources/Orgstar/Updates.swift
42 lines · 1501 bytes
1import Combine
2import Sparkle
3import SwiftUI
4
5/// Sparkle, for the signed builds: the Info.plist names the appcast and the key the DMGs are
6/// signed with. A dev build has neither, so the updater doesn't start and the menu item stays
7/// disabled. Whether to check automatically is Sparkle's own setting, asked once on the
8/// second launch.
9@MainActor
10@Observable
11final class Updates {
12 private(set) var canCheck = false
13 @ObservationIgnored private var controller: SPUStandardUpdaterController?
14 @ObservationIgnored private var observation: AnyCancellable?
15
16 func start() {
17 guard controller == nil, Bundle.main.object(forInfoDictionaryKey: "SUFeedURL") != nil else { return }
18 let controller = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil)
19 self.controller = controller
20 observation = controller.updater.publisher(for: \.canCheckForUpdates).sink { [weak self] can in
21 MainActor.assumeIsolated { self?.canCheck = can }
22 }
23 }
24
25 func check() {
26 controller?.updater.checkForUpdates()
27 }
28
29 var automatically: Bool {
30 get { controller?.updater.automaticallyChecksForUpdates ?? false }
31 set { controller?.updater.automaticallyChecksForUpdates = newValue }
32 }
33}
34
35struct CheckForUpdatesItem: View {
36 let updates: Updates
37
38 var body: some View {
39 Button("Check for Updates…") { updates.check() }
40 .disabled(!updates.canCheck)
41 }
42}