Skip to content

Commit f34e415

Browse files
committed
feat: support disabling the built-in updater
1 parent de4b0e5 commit f34e415

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

Coder-Desktop/Coder-Desktop/UpdaterService.swift

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,63 @@ import Sparkle
22
import SwiftUI
33

44
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+
1114
@Published var canCheckForUpdates = true
1215

1316
@Published var autoCheckForUpdates: Bool! {
1417
didSet {
1518
if let autoCheckForUpdates, autoCheckForUpdates != oldValue {
16-
updater.automaticallyChecksForUpdates = autoCheckForUpdates
19+
inner?.updater.automaticallyChecksForUpdates = autoCheckForUpdates
1720
}
1821
}
1922
}
2023

2124
@Published var updateChannel: UpdateChannel {
2225
didSet {
23-
UserDefaults.standard.set(updateChannel.rawValue, forKey: Self.updateChannelKey)
26+
UserDefaults.standard.set(updateChannel.rawValue, forKey: Keys.updateChannel)
2427
}
2528
}
2629

27-
static let updateChannelKey = "updateChannel"
30+
private var inner: (controller: SPUStandardUpdaterController, updater: SPUUpdater)?
2831

2932
override init() {
30-
updateChannel = UserDefaults.standard.string(forKey: Self.updateChannelKey)
33+
updateChannel = UserDefaults.standard.string(forKey: Keys.updateChannel)
3134
.flatMap { UpdateChannel(rawValue: $0) } ?? .stable
3235
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+
3450
autoCheckForUpdates = updater.automaticallyChecksForUpdates
3551
updater.publisher(for: \.canCheckForUpdates).assign(to: &$canCheckForUpdates)
3652
}
3753

3854
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"
4162
}
4263
}
4364

Coder-Desktop/Coder-Desktop/Views/Settings/GeneralTab.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ struct GeneralTab: View {
1919
Text("Start Coder Connect on launch")
2020
}
2121
}
22-
Section {
23-
Toggle(isOn: $updater.autoCheckForUpdates) {
24-
Text("Automatically check for updates")
25-
}
26-
Picker("Update channel", selection: $updater.updateChannel) {
27-
ForEach(UpdateChannel.allCases) { channel in
28-
Text(channel.name).tag(channel)
22+
if !updater.disabled {
23+
Section {
24+
Toggle(isOn: $updater.autoCheckForUpdates) {
25+
Text("Automatically check for updates")
26+
}
27+
Picker("Update channel", selection: $updater.updateChannel) {
28+
ForEach(UpdateChannel.allCases) { channel in
29+
Text(channel.name).tag(channel)
30+
}
31+
}
32+
HStack {
33+
Spacer()
34+
Button("Check for updates") { updater.checkForUpdates() }.disabled(!updater.canCheckForUpdates)
2935
}
30-
}
31-
HStack {
32-
Spacer()
33-
Button("Check for updates") { updater.checkForUpdates() }.disabled(!updater.canCheckForUpdates)
3436
}
3537
}
3638
}.formStyle(.grouped)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy