summaryrefslogtreecommitdiff
path: root/macos/Sources/Features/Update/UpdateViewModel.swift
blob: fb477324c013b89c7d986f1059527986d27ecc39 (plain)
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
import Foundation
import SwiftUI

struct UpdateUIActions {
    let allowAutoChecks: () -> Void
    let denyAutoChecks: () -> Void
    let cancel: () -> Void
    let install: () -> Void
    let remindLater: () -> Void
    let skipThisVersion: () -> Void
    let showReleaseNotes: () -> Void
    let retry: () -> Void
}

class UpdateViewModel: ObservableObject {
    @Published var state: State = .idle
    @Published var progress: Double? = nil
    @Published var details: Details? = nil
    @Published var error: ErrorInfo? = nil
    
    enum State: Equatable {
        case idle
        case permissionRequest
        case checking
        case updateAvailable
        case downloading
        case extracting
        case readyToInstall
        case installing
        case notFound
        case error
    }
    
    struct ErrorInfo: Equatable {
        let title: String
        let message: String
    }
    
    struct Details: Equatable {
        let version: String
        let build: String?
        let size: String?
        let date: Date?
        let notesSummary: String?
    }
    
    var stateTooltip: String {
        switch state {
        case .idle:
            return ""
        case .permissionRequest:
            return "Update permission required"
        case .checking:
            return "Checking for updates…"
        case .updateAvailable:
            if let details {
                return "Update available: \(details.version)"
            }
            return "Update available"
        case .downloading:
            if let progress {
                return String(format: "Downloading %.0f%%…", progress * 100)
            }
            return "Downloading…"
        case .extracting:
            if let progress {
                return String(format: "Preparing %.0f%%…", progress * 100)
            }
            return "Preparing…"
        case .readyToInstall:
            return "Ready to install"
        case .installing:
            return "Installing…"
        case .notFound:
            return "No updates found"
        case .error:
            return error?.title ?? "Update failed"
        }
    }
    
    var text: String {
        switch state {
        case .idle:
            return ""
        case .permissionRequest:
            return "Update Permission"
        case .checking:
            return "Checking for Updates…"
        case .updateAvailable:
            if let details {
                return "Update Available: \(details.version)"
            }
            return "Update Available"
        case .downloading:
            if let progress {
                return String(format: "Downloading: %.0f%%", progress * 100)
            }
            return "Downloading…"
        case .extracting:
            if let progress {
                return String(format: "Preparing: %.0f%%", progress * 100)
            }
            return "Preparing…"
        case .readyToInstall:
            return "Install Update"
        case .installing:
            return "Installing…"
        case .notFound:
            return "No Updates Available"
        case .error:
            return error?.title ?? "Update Failed"
        }
    }
    
    var iconName: String {
        switch state {
        case .idle:
            return ""
        case .permissionRequest:
            return "questionmark.circle"
        case .checking:
            return "arrow.triangle.2.circlepath"
        case .updateAvailable:
            return "arrow.down.circle.fill"
        case .downloading, .extracting:
            return "" // Progress ring instead
        case .readyToInstall:
            return "checkmark.circle.fill"
        case .installing:
            return "gear"
        case .notFound:
            return "info.circle"
        case .error:
            return "exclamationmark.triangle.fill"
        }
    }
    
    var iconColor: Color {
        switch state {
        case .idle:
            return .secondary
        case .permissionRequest, .checking:
            return .secondary
        case .updateAvailable, .readyToInstall:
            return .accentColor
        case .downloading, .extracting, .installing:
            return .secondary
        case .notFound:
            return .secondary
        case .error:
            return .orange
        }
    }
    
    var backgroundColor: Color {
        switch state {
        case .updateAvailable:
            return .accentColor
        case .readyToInstall:
            return Color(nsColor: NSColor.systemGreen.blended(withFraction: 0.3, of: .black) ?? .systemGreen)
        case .error:
            return .orange.opacity(0.2)
        default:
            return Color(nsColor: .controlBackgroundColor)
        }
    }
    
    var foregroundColor: Color {
        switch state {
        case .updateAvailable, .readyToInstall:
            return .white
        case .error:
            return .orange
        default:
            return .primary
        }
    }
}