forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopUpdatePolicyManager.swift
More file actions
203 lines (181 loc) · 6.61 KB
/
Copy pathDesktopUpdatePolicyManager.swift
File metadata and controls
203 lines (181 loc) · 6.61 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import AppKit
import Foundation
import OmiTheme
import SwiftUI
@MainActor
final class DesktopUpdatePolicyManager: ObservableObject {
static let shared = DesktopUpdatePolicyManager()
@Published private(set) var policy: DesktopUpdatePolicyResponse?
private var lastCheckAt = Date.distantPast
private let minimumCheckInterval: TimeInterval = 5 * 60
private let dismissedPrefix = "desktopUpdatePolicyDismissed."
private let fetchPolicy: (Int?) async throws -> DesktopUpdatePolicyResponse
private let currentBuildProvider: () -> Int?
private let now: () -> Date
private let defaults: UserDefaults
private init() {
fetchPolicy = { currentBuild in
try await APIClient.shared.getDesktopUpdatePolicy(currentBuild: currentBuild)
}
currentBuildProvider = {
guard let raw = Bundle.main.infoDictionary?["CFBundleVersion"] as? String else { return nil }
return Int(raw)
}
now = Date.init
defaults = .standard
}
init(
fetchPolicy: @escaping (Int?) async throws -> DesktopUpdatePolicyResponse,
currentBuildProvider: @escaping () -> Int? = { nil },
now: @escaping () -> Date = Date.init,
defaults: UserDefaults = .standard
) {
self.fetchPolicy = fetchPolicy
self.currentBuildProvider = currentBuildProvider
self.now = now
self.defaults = defaults
}
var visiblePolicy: DesktopUpdatePolicyResponse? {
guard let policy, policy.active, policy.severity != .none else { return nil }
if policy.isRequired { return policy }
return isDismissed(policy) ? nil : policy
}
func refresh(force: Bool = false) {
Task { await refreshNow(force: force) }
}
func refreshNow(force: Bool = false) async {
let currentTime = now()
guard force || currentTime.timeIntervalSince(lastCheckAt) >= minimumCheckInterval else { return }
lastCheckAt = currentTime
do {
let fetched = try await fetchPolicy(currentBuildProvider())
policy = fetched.active ? fetched : nil
} catch {
// Never preserve a stale required prompt when its control plane is down.
// Sparkle and the stable manual download path remain available independently.
policy = nil
log("DesktopUpdatePolicy: unavailable error_type=\(String(reflecting: type(of: error)))")
DesktopDiagnosticsManager.shared.recordFallback(
area: "desktop_update",
from: "desktop_update_policy",
to: "desktop_update_appcast",
reason: "other",
outcome: .recovered,
extra: ["user_visible": false]
)
}
}
func dismiss(_ policy: DesktopUpdatePolicyResponse) {
guard policy.canDismiss, !policy.isRequired else { return }
defaults.set(true, forKey: dismissedKey(for: policy))
if self.policy?.id == policy.id {
self.policy = nil
}
}
func openDownload(_ policy: DesktopUpdatePolicyResponse) {
NSWorkspace.shared.open(DesktopUpdatePolicyResponse.resolvedDownloadURL(from: policy.downloadURL))
}
private func isDismissed(_ policy: DesktopUpdatePolicyResponse) -> Bool {
defaults.bool(forKey: dismissedKey(for: policy))
}
private func dismissedKey(for policy: DesktopUpdatePolicyResponse) -> String {
dismissedPrefix + policy.id
}
}
struct DesktopUpdatePolicyBanner: View {
let policy: DesktopUpdatePolicyResponse
let onDownload: () -> Void
let onDismiss: () -> Void
var body: some View {
HStack(alignment: .center, spacing: OmiSpacing.md) {
Image(systemName: "arrow.down.circle.fill")
.scaledFont(size: OmiType.subheading)
.foregroundColor(.white)
.frame(width: 22)
VStack(alignment: .leading, spacing: OmiSpacing.hairline) {
Text(policy.title ?? "Update Omi")
.scaledFont(size: OmiType.body, weight: .semibold)
.foregroundColor(.white)
.lineLimit(1)
if let message = policy.message {
Text(message)
.scaledFont(size: OmiType.caption)
.foregroundColor(.white.opacity(0.82))
.lineLimit(2)
.truncationMode(.tail)
.fixedSize(horizontal: false, vertical: true)
}
}
.frame(maxWidth: 400, alignment: .leading)
.layoutPriority(1)
Button(policy.ctaText) {
onDownload()
}
.buttonStyle(.plain)
.scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundColor(Color(red: 0.08, green: 0.09, blue: 0.10))
.padding(.horizontal, OmiSpacing.md)
.frame(height: 32)
.background(Color.white.opacity(0.94))
.clipShape(RoundedRectangle(cornerRadius: 7))
.fixedSize(horizontal: true, vertical: false)
.layoutPriority(2)
if policy.canDismiss {
Button {
onDismiss()
} label: {
Image(systemName: "xmark")
.scaledFont(size: OmiType.caption, weight: .semibold)
}
.buttonStyle(.plain)
.foregroundColor(.white.opacity(0.72))
.frame(width: 24, height: 24)
.help("Dismiss")
}
}
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.md)
.background(Color(red: 0.10, green: 0.12, blue: 0.14).opacity(0.98))
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.elementRadius)
.stroke(Color.white.opacity(0.12), lineWidth: 1)
)
.clipShape(RoundedRectangle(cornerRadius: OmiChrome.elementRadius))
.shadow(color: Color.black.opacity(0.28), radius: 18, x: 0, y: 8)
}
}
struct DesktopRequiredUpdatePrompt: View {
let policy: DesktopUpdatePolicyResponse
let onDownload: () -> Void
var body: some View {
VStack(spacing: OmiSpacing.lg) {
Image(systemName: "arrow.down.circle.fill")
.scaledFont(size: 30)
.foregroundColor(.white)
VStack(spacing: OmiSpacing.sm) {
Text(policy.title ?? "Update Required")
.scaledFont(size: OmiType.heading, weight: .semibold)
.foregroundColor(.white)
Text(policy.message ?? "Please install the latest Omi desktop app to continue.")
.scaledFont(size: OmiType.body)
.foregroundColor(.white.opacity(0.72))
.multilineTextAlignment(.center)
.fixedSize(horizontal: false, vertical: true)
}
Button(policy.ctaText) {
onDownload()
}
.buttonStyle(.borderedProminent)
.controlSize(.large)
}
.frame(width: 420)
.padding(OmiSpacing.xxl)
.background(Color(red: 0.08, green: 0.09, blue: 0.10))
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius)
.stroke(Color.white.opacity(0.14), lineWidth: 1)
)
.clipShape(RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius))
.shadow(color: Color.black.opacity(0.36), radius: 24, x: 0, y: 14)
}
}