@@ -2,42 +2,63 @@ import Sparkle
2
2
import SwiftUI
3
3
4
4
final class UpdaterService : NSObject , ObservableObject {
5
- private lazy var inner : SPUStandardUpdaterController = . init(
6
- startingUpdater: true ,
7
- updaterDelegate: self ,
8
- userDriverDelegate: self
9
- )
10
- private var updater : SPUUpdater !
5
+ // The auto-updater can be entirely disabled by setting the
6
+ // `disableUpdater` UserDefaults key to `true`. This is designed for use in
7
+ // MDM configurations, where the value can be set to `true` permanently.
8
+ @Published var disabled : Bool = UserDefaults . standard. bool ( forKey: Keys . disableUpdater) {
9
+ didSet {
10
+ UserDefaults . standard. set ( disabled, forKey: Keys . disableUpdater)
11
+ }
12
+ }
13
+
11
14
@Published var canCheckForUpdates = true
12
15
13
16
@Published var autoCheckForUpdates : Bool ! {
14
17
didSet {
15
18
if let autoCheckForUpdates, autoCheckForUpdates != oldValue {
16
- updater. automaticallyChecksForUpdates = autoCheckForUpdates
19
+ inner ? . updater. automaticallyChecksForUpdates = autoCheckForUpdates
17
20
}
18
21
}
19
22
}
20
23
21
24
@Published var updateChannel : UpdateChannel {
22
25
didSet {
23
- UserDefaults . standard. set ( updateChannel. rawValue, forKey: Self . updateChannelKey )
26
+ UserDefaults . standard. set ( updateChannel. rawValue, forKey: Keys . updateChannel )
24
27
}
25
28
}
26
29
27
- static let updateChannelKey = " updateChannel "
30
+ private var inner : ( controller : SPUStandardUpdaterController , updater : SPUUpdater ) ?
28
31
29
32
override init ( ) {
30
- updateChannel = UserDefaults . standard. string ( forKey: Self . updateChannelKey )
33
+ updateChannel = UserDefaults . standard. string ( forKey: Keys . updateChannel )
31
34
. flatMap { UpdateChannel ( rawValue: $0) } ?? . stable
32
35
super. init ( )
33
- updater = inner. updater
36
+
37
+ guard !disabled else {
38
+ return
39
+ }
40
+
41
+ let inner = SPUStandardUpdaterController (
42
+ startingUpdater: true ,
43
+ updaterDelegate: self ,
44
+ userDriverDelegate: self
45
+ )
46
+
47
+ let updater = inner. updater
48
+ self . inner = ( inner, updater)
49
+
34
50
autoCheckForUpdates = updater. automaticallyChecksForUpdates
35
51
updater. publisher ( for: \. canCheckForUpdates) . assign ( to: & $canCheckForUpdates)
36
52
}
37
53
38
54
func checkForUpdates( ) {
39
- guard canCheckForUpdates else { return }
40
- updater. checkForUpdates ( )
55
+ guard let inner, canCheckForUpdates else { return }
56
+ inner. updater. checkForUpdates ( )
57
+ }
58
+
59
+ enum Keys {
60
+ static let disableUpdater = " disableUpdater "
61
+ static let updateChannel = " updateChannel "
41
62
}
42
63
}
43
64
0 commit comments