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