forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateChecker.swift
More file actions
101 lines (87 loc) · 3.07 KB
/
UpdateChecker.swift
File metadata and controls
101 lines (87 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import Logger
import Preferences
import Sparkle
public final class UpdateChecker {
let updater: SPUUpdater
let hostBundleFound: Bool
let delegate: UpdaterDelegate
public weak var updateCheckerDelegate: UpdateCheckerDelegate? {
get { delegate.updateCheckerDelegate }
set { delegate.updateCheckerDelegate = newValue }
}
public init(
hostBundle: Bundle?,
shouldAutomaticallyCheckForUpdate: Bool
) {
if hostBundle == nil {
hostBundleFound = false
Logger.updateChecker.error("Host bundle not found")
} else {
hostBundleFound = true
}
delegate = .init(
shouldAutomaticallyCheckForUpdate: shouldAutomaticallyCheckForUpdate
)
updater = SPUUpdater(
hostBundle: hostBundle ?? Bundle.main,
applicationBundle: Bundle.main,
userDriver: SPUStandardUserDriver(hostBundle: hostBundle ?? Bundle.main, delegate: nil),
delegate: delegate
)
do {
try updater.start()
} catch {
Logger.updateChecker.error(error.localizedDescription)
}
}
public func checkForUpdates() {
updater.checkForUpdates()
}
public func resetUpdateCycle() {
updater.resetUpdateCycleAfterShortDelay()
}
public var automaticallyChecksForUpdates: Bool {
get { updater.automaticallyChecksForUpdates }
set { updater.automaticallyChecksForUpdates = newValue }
}
}
public protocol UpdateCheckerDelegate: AnyObject {
func prepareForRelaunch(finish: @escaping () -> Void)
}
class UpdaterDelegate: NSObject, SPUUpdaterDelegate {
let shouldAutomaticallyCheckForUpdate: Bool
weak var updateCheckerDelegate: UpdateCheckerDelegate?
init(shouldAutomaticallyCheckForUpdate: Bool) {
self.shouldAutomaticallyCheckForUpdate = shouldAutomaticallyCheckForUpdate
}
func updater(_ updater: SPUUpdater, mayPerform updateCheck: SPUUpdateCheck) throws {
// Not sure how it works
// if !shouldAutomaticallyCheckForUpdate, updateCheck == .updatesInBackground {
// throw CancellationError()
// }
}
func updater(
_ updater: SPUUpdater,
shouldPostponeRelaunchForUpdate item: SUAppcastItem,
untilInvokingBlock installHandler: @escaping () -> Void
) -> Bool {
if let updateCheckerDelegate {
updateCheckerDelegate.prepareForRelaunch(finish: installHandler)
return true
}
return false
}
func updater(_ updater: SPUUpdater, willScheduleUpdateCheckAfterDelay delay: TimeInterval) {
Logger.updateChecker.info("Will schedule update check after delay: \(delay)")
}
func updaterWillNotScheduleUpdateCheck(_ updater: SPUUpdater) {
Logger.updateChecker.info("Will not schedule update check")
}
func allowedChannels(for updater: SPUUpdater) -> Set<String> {
if UserDefaults.shared.value(for: \.installBetaBuilds) {
Set(["beta"])
} else {
[]
}
}
}