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