|
| 1 | +import Sparkle |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +finalclassUpdaterService:NSObject,ObservableObject{ |
| 5 | +private lazyvarinner:SPUStandardUpdaterController=.init( |
| 6 | + startingUpdater:true, |
| 7 | + updaterDelegate:self, |
| 8 | + userDriverDelegate:self, |
| 9 | +) |
| 10 | +privatevarupdater:SPUUpdater! |
| 11 | +@PublishedvarcanCheckForUpdates=true |
| 12 | + |
| 13 | +@PublishedvarautoCheckForUpdates:Bool=false{ |
| 14 | + didSet{ |
| 15 | +if autoCheckForUpdates!= oldValue{ |
| 16 | + updater.automaticallyChecksForUpdates= autoCheckForUpdates |
| 17 | +} |
| 18 | +} |
| 19 | +} |
| 20 | + |
| 21 | +@PublishedvarupdateChannel:UpdateChannel{ |
| 22 | + didSet{ |
| 23 | +UserDefaults.standard.set(updateChannel.rawValue, forKey:Self.updateChannelKey) |
| 24 | +} |
| 25 | +} |
| 26 | + |
| 27 | +staticletupdateChannelKey="updateChannel" |
| 28 | + |
| 29 | +overrideinit(){ |
| 30 | + updateChannel=UserDefaults.standard.string(forKey:Self.updateChannelKey) |
| 31 | +.flatMap{UpdateChannel(rawValue: $0)}??.stable |
| 32 | + super.init() |
| 33 | + updater= inner.updater |
| 34 | + updater.publisher(for: \.canCheckForUpdates).assign(to:&$canCheckForUpdates) |
| 35 | +} |
| 36 | + |
| 37 | +func checkForUpdates(){ |
| 38 | +guard canCheckForUpdateselse{return} |
| 39 | + updater.checkForUpdates() |
| 40 | +} |
| 41 | +} |
| 42 | + |
| 43 | +enumUpdateChannel:String,CaseIterable,Identifiable{ |
| 44 | +case stable |
| 45 | +case preview |
| 46 | + |
| 47 | +varname:String{ |
| 48 | +switchself{ |
| 49 | +case.stable: |
| 50 | +"Stable" |
| 51 | +case.preview: |
| 52 | +"Preview" |
| 53 | +} |
| 54 | +} |
| 55 | + |
| 56 | +varid:String{ rawValue} |
| 57 | +} |
| 58 | + |
| 59 | +extensionUpdaterService:SPUUpdaterDelegate{ |
| 60 | +func allowedChannels(for _:SPUUpdater)->Set<String>{ |
| 61 | + // There's currently no point in subscribing to both channels, as |
| 62 | + // preview >= stable |
| 63 | +[updateChannel.rawValue] |
| 64 | +} |
| 65 | +} |
| 66 | + |
| 67 | +extensionUpdaterService:SUVersionDisplay{ |
| 68 | +func formatUpdateVersion( |
| 69 | + fromUpdate update:SUAppcastItem, |
| 70 | + andBundleDisplayVersion inOutBundleDisplayVersion:AutoreleasingUnsafeMutablePointer<NSString>, |
| 71 | + withBundleVersion bundleVersion:String |
| 72 | +)->String{ |
| 73 | + // Replace CFBundleShortVersionString with CFBundleVersion, as the |
| 74 | + // latter shows build numbers. |
| 75 | + inOutBundleDisplayVersion.pointee= bundleVersionasNSString |
| 76 | + // This is already CFBundleVersion, as that's the only version in the |
| 77 | + // appcast. |
| 78 | +return update.displayVersionString |
| 79 | +} |
| 80 | +} |
| 81 | + |
| 82 | +extensionUpdaterService:SPUStandardUserDriverDelegate{ |
| 83 | +func standardUserDriverRequestsVersionDisplayer()->(anySUVersionDisplay)?{ |
| 84 | +self |
| 85 | +} |
| 86 | +} |