Textley / app · textley.ai / Sources / Textley / TextleyUpdates.swift
| 1 | #if TEXTLEY_SPARKLE |
| 2 | import Foundation |
| 3 | import Observation |
| 4 | import Sparkle |
| 5 | |
| 6 | /// Sparkle updates for the Developer ID build. Debug and source builds compile without it. |
| 7 | @MainActor |
| 8 | @Observable |
| 9 | final 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 |