|
| 1 | +--- Ice/Updates/UpdatesManager.swift |
| 2 | ++++ Ice/Updates/UpdatesManager.swift |
| 3 | +@@ -3,7 +3,6 @@ |
| 4 | + // Ice |
| 5 | + // |
| 6 | + |
| 7 | +-import Sparkle |
| 8 | + import SwiftUI |
| 9 | + |
| 10 | + /// Manager for app updates. |
| 11 | +@@ -18,39 +17,11 @@ |
| 12 | + /// The shared app state. |
| 13 | + private(set) weak var appState: AppState? |
| 14 | + |
| 15 | +- /// The underlying updater controller. |
| 16 | +- private(set) lazy var updaterController = SPUStandardUpdaterController( |
| 17 | +- startingUpdater: true, |
| 18 | +- updaterDelegate: self, |
| 19 | +- userDriverDelegate: self |
| 20 | +- ) |
| 21 | +- |
| 22 | +- /// The underlying updater. |
| 23 | +- var updater: SPUUpdater { |
| 24 | +- updaterController.updater |
| 25 | +- } |
| 26 | +- |
| 27 | + /// A Boolean value that indicates whether to automatically check for updates. |
| 28 | +- var automaticallyChecksForUpdates: Bool { |
| 29 | +- get { |
| 30 | +- updater.automaticallyChecksForUpdates |
| 31 | +- } |
| 32 | +- set { |
| 33 | +- objectWillChange.send() |
| 34 | +- updater.automaticallyChecksForUpdates = newValue |
| 35 | +- } |
| 36 | +- } |
| 37 | ++ var automaticallyChecksForUpdates: Bool = false |
| 38 | + |
| 39 | + /// A Boolean value that indicates whether to automatically download updates. |
| 40 | +- var automaticallyDownloadsUpdates: Bool { |
| 41 | +- get { |
| 42 | +- updater.automaticallyDownloadsUpdates |
| 43 | +- } |
| 44 | +- set { |
| 45 | +- objectWillChange.send() |
| 46 | +- updater.automaticallyDownloadsUpdates = newValue |
| 47 | +- } |
| 48 | +- } |
| 49 | ++ var automaticallyDownloadsUpdates: Bool = false |
| 50 | + |
| 51 | + /// Creates an updates manager with the given app state. |
| 52 | + init(appState: AppState) { |
| 53 | +@@ -59,87 +30,11 @@ |
| 54 | + } |
| 55 | + |
| 56 | + /// Sets up the manager. |
| 57 | +- func performSetup() { |
| 58 | +- _ = updaterController |
| 59 | +- configureCancellables() |
| 60 | +- } |
| 61 | ++ func performSetup() { } |
| 62 | + |
| 63 | +- /// Configures the internal observers for the manager. |
| 64 | +- private func configureCancellables() { |
| 65 | +- updater.publisher(for: \.canCheckForUpdates) |
| 66 | +- .assign(to: &$canCheckForUpdates) |
| 67 | +- updater.publisher(for: \.lastUpdateCheckDate) |
| 68 | +- .assign(to: &$lastUpdateCheckDate) |
| 69 | +- } |
| 70 | +- |
| 71 | + /// Checks for app updates. |
| 72 | +- @objc func checkForUpdates() { |
| 73 | +- #if DEBUG |
| 74 | +- // Checking for updates hangs in debug mode. |
| 75 | +- let alert = NSAlert() |
| 76 | +- alert.messageText = "Checking for updates is not supported in debug mode." |
| 77 | +- alert.runModal() |
| 78 | +- #else |
| 79 | +- guard let appState else { |
| 80 | +- return |
| 81 | +- } |
| 82 | +- // Activate the app in case an alert needs to be displayed. |
| 83 | +- appState.activate(withPolicy: .regular) |
| 84 | +- appState.openSettingsWindow() |
| 85 | +- updater.checkForUpdates() |
| 86 | +- #endif |
| 87 | +- } |
| 88 | ++ @objc func checkForUpdates() { } |
| 89 | + } |
| 90 | + |
| 91 | +-// MARK: UpdatesManager: SPUUpdaterDelegate |
| 92 | +-extension UpdatesManager: @preconcurrency SPUUpdaterDelegate { |
| 93 | +- func updater(_ updater: SPUUpdater, willScheduleUpdateCheckAfterDelay delay: TimeInterval) { |
| 94 | +- guard let appState else { |
| 95 | +- return |
| 96 | +- } |
| 97 | +- appState.userNotificationManager.requestAuthorization() |
| 98 | +- } |
| 99 | +-} |
| 100 | +- |
| 101 | +-// MARK: UpdatesManager: SPUStandardUserDriverDelegate |
| 102 | +-extension UpdatesManager: @preconcurrency SPUStandardUserDriverDelegate { |
| 103 | +- var supportsGentleScheduledUpdateReminders: Bool { true } |
| 104 | +- |
| 105 | +- func standardUserDriverShouldHandleShowingScheduledUpdate( |
| 106 | +- _ update: SUAppcastItem, |
| 107 | +- andInImmediateFocus immediateFocus: Bool |
| 108 | +- ) -> Bool { |
| 109 | +- if NSApp.isActive { |
| 110 | +- return immediateFocus |
| 111 | +- } else { |
| 112 | +- return false |
| 113 | +- } |
| 114 | +- } |
| 115 | +- |
| 116 | +- func standardUserDriverWillHandleShowingUpdate( |
| 117 | +- _ handleShowingUpdate: Bool, |
| 118 | +- forUpdate update: SUAppcastItem, |
| 119 | +- state: SPUUserUpdateState |
| 120 | +- ) { |
| 121 | +- guard let appState else { |
| 122 | +- return |
| 123 | +- } |
| 124 | +- if !state.userInitiated { |
| 125 | +- appState.userNotificationManager.addRequest( |
| 126 | +- with: .updateCheck, |
| 127 | +- title: "A new update is available", |
| 128 | +- body: "Version \(update.displayVersionString) is now available" |
| 129 | +- ) |
| 130 | +- } |
| 131 | +- } |
| 132 | +- |
| 133 | +- func standardUserDriverDidReceiveUserAttention(forUpdate update: SUAppcastItem) { |
| 134 | +- guard let appState else { |
| 135 | +- return |
| 136 | +- } |
| 137 | +- appState.userNotificationManager.removeDeliveredNotifications(with: [.updateCheck]) |
| 138 | +- } |
| 139 | +-} |
| 140 | +- |
| 141 | + // MARK: UpdatesManager: BindingExposable |
| 142 | + extension UpdatesManager: BindingExposable { } |
| 143 | +--- Ice/Settings/SettingsPanes/UpdatesSettingsPane.swift |
| 144 | ++++ Ice/Settings/SettingsPanes/UpdatesSettingsPane.swift |
| 145 | +@@ -6,66 +6,8 @@ |
| 146 | + import SwiftUI |
| 147 | + |
| 148 | + struct UpdatesSettingsPane: View { |
| 149 | +- @EnvironmentObject var appState: AppState |
| 150 | +- |
| 151 | +- private var updatesManager: UpdatesManager { |
| 152 | +- appState.updatesManager |
| 153 | +- } |
| 154 | +- |
| 155 | +- private var lastUpdateCheckString: String { |
| 156 | +- if let date = updatesManager.lastUpdateCheckDate { |
| 157 | +- date.formatted(date: .abbreviated, time: .standard) |
| 158 | +- } else { |
| 159 | +- "Never" |
| 160 | +- } |
| 161 | +- } |
| 162 | +- |
| 163 | + var body: some View { |
| 164 | +- IceForm { |
| 165 | +- IceSection { |
| 166 | +- automaticallyCheckForUpdates |
| 167 | +- automaticallyDownloadUpdates |
| 168 | +- } |
| 169 | +- if updatesManager.canCheckForUpdates { |
| 170 | +- IceSection { |
| 171 | +- checkForUpdates |
| 172 | +- } |
| 173 | +- } |
| 174 | +- } |
| 175 | ++ Text("Updates are managed by MacPorts.") |
| 176 | ++ .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 177 | + } |
| 178 | +- |
| 179 | +- @ViewBuilder |
| 180 | +- private var automaticallyCheckForUpdates: some View { |
| 181 | +- Toggle( |
| 182 | +- "Automatically check for updates", |
| 183 | +- isOn: updatesManager.bindings.automaticallyChecksForUpdates |
| 184 | +- ) |
| 185 | +- } |
| 186 | +- |
| 187 | +- @ViewBuilder |
| 188 | +- private var automaticallyDownloadUpdates: some View { |
| 189 | +- Toggle( |
| 190 | +- "Automatically download updates", |
| 191 | +- isOn: updatesManager.bindings.automaticallyDownloadsUpdates |
| 192 | +- ) |
| 193 | +- } |
| 194 | +- |
| 195 | +- @ViewBuilder |
| 196 | +- private var checkForUpdates: some View { |
| 197 | +- HStack { |
| 198 | +- Button("Check for Updates…") { |
| 199 | +- updatesManager.checkForUpdates() |
| 200 | +- } |
| 201 | +- .controlSize(.large) |
| 202 | +- |
| 203 | +- Spacer() |
| 204 | +- |
| 205 | +- HStack(spacing: 2) { |
| 206 | +- Text("Last checked:") |
| 207 | +- Text(lastUpdateCheckString) |
| 208 | +- } |
| 209 | +- .lineLimit(1) |
| 210 | +- .font(.caption) |
| 211 | +- } |
| 212 | +- } |
| 213 | + } |
0 commit comments