Branchwell

Textley / app · textley.ai / Sources / Textley / TextleyUpdates.swift

Raw 1.7 KB on 63b71f34cfe61bc6553e8463e803a45424bff4d3
1#if TEXTLEY_SPARKLE
2import Foundation
3import Observation
4import Sparkle
5
6/// Sparkle updates for the Developer ID build. Debug and source builds compile without it.
7@MainActor
8@Observable
9final class TextleyUpdates: NSObject {
10 static let shared = TextleyUpdates()
11
12 private(set) var canCheck = false
13 private(set) var automaticUpdates = false
14 @ObservationIgnored private var controller: SPUStandardUpdaterController?
15 @ObservationIgnored private var observations: [NSKeyValueObservation] = []
16
17 func start() {
18 guard controller == nil else { return }
19 let controller = SPUStandardUpdaterController(
20 startingUpdater: false, updaterDelegate: nil, userDriverDelegate: nil
21 )
22 self.controller = controller
23 observations = [
24 controller.updater.observe(\.canCheckForUpdates, options: [.initial, .new]) { [weak self] _, _ in
25 Task { @MainActor in self?.refresh() }
26 },
27 controller.updater.observe(\.automaticallyDownloadsUpdates, options: [.initial, .new]) { [weak self] _, _ in
28 Task { @MainActor in self?.refresh() }
29 }
30 ]
31 controller.startUpdater()
32 refresh()
33 }
34
35 @objc func check(_ sender: Any? = nil) {
36 guard let updater = controller?.updater, updater.canCheckForUpdates else { return }
37 updater.checkForUpdates()
38 }
39
40 func setAutomaticUpdates(_ enabled: Bool) {
41 controller?.updater.automaticallyDownloadsUpdates = enabled
42 refresh()
43 }
44
45 private func refresh() {
46 canCheck = controller?.updater.canCheckForUpdates ?? false
47 automaticUpdates = controller?.updater.automaticallyDownloadsUpdates ?? false
48 }
49}
50#endif