Refactor UpdateViewModel to use new UpdateState
Update @macos/Sources/Features/Update/UpdateViewModel.swift to use the new `UpdateState` exclusively. Rename `state2` to `state` (remove the old state).
The user wants me to update the UpdateViewModel.swift file to:
- Use the new
UpdateStateexclusively (the enum defined at the top) - Rename
state2tostate - Remove the old
stateproperty (which is of typeState)
Looking at the file, I can see:
- There's a new
UpdateStateenum (lines 16-63) - The class has
@Published var state2: UpdateState = .idle(line 66) - The class has the old
@Published var state: State = .idle(line 68) - There are also
progress,details, anderrorproperties that seem to be associated with the old state system - There's an old
Stateenum (lines 73-84) - There are various computed properties that use the old
stateproperty
I need to:
- Remove the old
Stateenum (lines 73-84) - Remove the old
@Published var state: State = .idle(line 68) - Rename
state2tostate - Remove or update the properties that were used with the old state system (
progress,details,error) - Update all the computed properties to work with the new
UpdateState
Let me plan this carefully. The computed properties like stateTooltip, text, iconName, iconColor, backgroundColor, and foregroundColor all switch on the old state. I need to update them to switch on the new UpdateState.
Looking at the new UpdateState, it has associated values for various cases, so I'll need to extract those when needed in the computed properties.
Let me start by creating a TODO list and then make the changes.
@@ -62,41 +62,10 @@
}
}
class UpdateViewModel: ObservableObject {
@Published var state2: UpdateState = .idle
@Published var state: UpdateState = .idle
@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 ""
@@ -72,31 +72,26 @@
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 {
case .updateAvailable(let update):
return "Update available: \(update.appcastItem.displayVersionString)"
case .downloading(let download):
if let expectedLength = download.expectedLength, expectedLength > 0 {
let progress = Double(download.progress) / Double(expectedLength)
return String(format: "Downloading %.0f%%…", progress * 100)
}
return "Downloading…"
case .extracting:
if let progress {
return String(format: "Preparing %.0f%%…", progress * 100)
}
return "Preparing…"
case .extracting(let extracting):
return String(format: "Preparing %.0f%%…", extracting.progress * 100)
case .readyToInstall:
return "Ready to install"
case .installing:
return "Installing…"
case .notFound:
return "No updates found"
case .error:
return error?.title ?? "Update failed"
case .error(let err):
return err.error.localizedDescription
}
}
var text: String {
@@ -101,31 +101,26 @@
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 {
case .updateAvailable(let update):
return "Update Available: \(update.appcastItem.displayVersionString)"
case .downloading(let download):
if let expectedLength = download.expectedLength, expectedLength > 0 {
let progress = Double(download.progress) / Double(expectedLength)
return String(format: "Downloading: %.0f%%", progress * 100)
}
return "Downloading…"
case .extracting:
if let progress {
return String(format: "Preparing: %.0f%%", progress * 100)
}
return "Preparing…"
case .extracting(let extracting):
return String(format: "Preparing: %.0f%%", extracting.progress * 100)
case .readyToInstall:
return "Install Update"
case .installing:
return "Installing…"
case .notFound:
return "No Updates Available"
case .error:
return error?.title ?? "Update Failed"
return "Update Failed"
}
}
var iconName: String {
Public
Thread
3mo ago
ghostty-org/ghostty
smart (Sonnet 4.5)
CLI
4
prompts
7% of 968k
6
files
+178
-226
~81
Reference Thread
T-29c43cfa-5b00-420d-9490-ce16cea29103