mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2026-04-01 09:10:08 +02:00
♻️ Make NVAlert invocation more consistent
This commit is contained in:
@@ -29,32 +29,44 @@ class AppUpdater {
|
|||||||
var interactive: Bool = false
|
var interactive: Bool = false
|
||||||
|
|
||||||
public func checkForUpdates(userInitiated: Bool) async -> UpdateCheckResult {
|
public func checkForUpdates(userInitiated: Bool) async -> UpdateCheckResult {
|
||||||
|
// If user initiated, we always expect to see an alert
|
||||||
self.interactive = userInitiated
|
self.interactive = userInitiated
|
||||||
|
|
||||||
|
// Log that we're looking for updates
|
||||||
Log.info("The app will search for updates...")
|
Log.info("The app will search for updates...")
|
||||||
|
|
||||||
let caskUrl = Constants.Urls.UpdateCheckEndpoint
|
// Attempt to get the latest CaskFile from the API
|
||||||
|
guard let caskFile = try? await CaskFile.fromUrl(
|
||||||
guard let caskFile = try? await CaskFile.fromUrl(App.shared.container, caskUrl) else {
|
App.shared.container,
|
||||||
await presentCouldNotRetrieveUpdateIfInteractive()
|
Constants.Urls.UpdateCheckEndpoint
|
||||||
|
) else {
|
||||||
|
// ERROR #1: The endpoint is unreachable or the response is invalid.
|
||||||
|
Log.err("Could not get a valid CaskFile from the endpoint.")
|
||||||
|
if interactive {
|
||||||
|
await presentCouldNotRetrieveUpdate()
|
||||||
|
}
|
||||||
return .networkError
|
return .networkError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We will now persist the CaskFile so we can reference it later
|
||||||
self.caskFile = caskFile
|
self.caskFile = caskFile
|
||||||
|
|
||||||
let currentVersion = AppVersion.fromCurrentVersion()
|
// Let's parse the latest online version if we can
|
||||||
|
|
||||||
guard let onlineVersion = AppVersion.from(caskFile.version) else {
|
guard let onlineVersion = AppVersion.from(caskFile.version) else {
|
||||||
|
// ERROR #2: The CaskFile's version string is invalid.
|
||||||
Log.err("The version string from the CaskFile could not be read.")
|
Log.err("The version string from the CaskFile could not be read.")
|
||||||
await presentCouldNotRetrieveUpdateIfInteractive()
|
if interactive {
|
||||||
|
await presentCouldNotRetrieveUpdate()
|
||||||
|
}
|
||||||
return .parseError
|
return .parseError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We will now persist the version number so we can reference it later
|
||||||
latestVersionOnline = onlineVersion
|
latestVersionOnline = onlineVersion
|
||||||
Log.info("The latest version read from '\(caskUrl.lastPathComponent)' is: v\(onlineVersion.computerReadable).")
|
Log.info("The latest version read from the endpoint is: v\(onlineVersion.computerReadable).")
|
||||||
|
|
||||||
Task { // Present this concurrently w/ returning the .success value
|
Task { // Present this concurrently w/ returning the .success value
|
||||||
if latestVersionOnline > currentVersion {
|
if latestVersionOnline > AppVersion.fromCurrentVersion() {
|
||||||
await presentNewerVersionAvailableAlert()
|
await presentNewerVersionAvailableAlert()
|
||||||
} else if interactive {
|
} else if interactive {
|
||||||
await presentNoNewerVersionAvailableAlert()
|
await presentNoNewerVersionAvailableAlert()
|
||||||
@@ -64,12 +76,6 @@ class AppUpdater {
|
|||||||
return .success
|
return .success
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor private func presentCouldNotRetrieveUpdateIfInteractive() {
|
|
||||||
if interactive {
|
|
||||||
return presentCouldNotRetrieveUpdate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Alerts
|
// MARK: - Alerts
|
||||||
|
|
||||||
@MainActor public func presentNewerVersionAvailableAlert() {
|
@MainActor public func presentNewerVersionAvailableAlert() {
|
||||||
@@ -117,7 +123,7 @@ class AppUpdater {
|
|||||||
description: ""
|
description: ""
|
||||||
)
|
)
|
||||||
.withPrimary(text: "generic.ok".localized)
|
.withPrimary(text: "generic.ok".localized)
|
||||||
.show(urgency: interactive ? .bringToFront : .none)
|
.show(urgency: .bringToFront)
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor public func presentCouldNotRetrieveUpdate() {
|
@MainActor public func presentCouldNotRetrieveUpdate() {
|
||||||
@@ -135,7 +141,7 @@ class AppUpdater {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
.withPrimary(text: "generic.ok".localized)
|
.withPrimary(text: "generic.ok".localized)
|
||||||
.show(urgency: interactive ? .bringToFront : .normalRequestAttention)
|
.show(urgency: .bringToFront)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Preparing for Self-Updater
|
// MARK: - Preparing for Self-Updater
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ extension MainMenu {
|
|||||||
description: "alert.fix_homebrew_permissions_done.desc".localized
|
description: "alert.fix_homebrew_permissions_done.desc".localized
|
||||||
)
|
)
|
||||||
.withPrimary(text: "generic.ok".localized)
|
.withPrimary(text: "generic.ok".localized)
|
||||||
.show(urgency: .urgentRequestAttention)
|
.show(urgency: .bringToFront)
|
||||||
} failure: { error in
|
} failure: { error in
|
||||||
NVAlert.show(for: error as! HomebrewPermissionError)
|
NVAlert.show(for: error as! HomebrewPermissionError)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ extension MainMenu {
|
|||||||
description: "alert.fix_my_valet_done.desc".localized
|
description: "alert.fix_my_valet_done.desc".localized
|
||||||
)
|
)
|
||||||
.withPrimary(text: "generic.ok".localized)
|
.withPrimary(text: "generic.ok".localized)
|
||||||
.show(urgency: .normalRequestAttention)
|
.show(urgency: .bringToFront)
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor private func presentAlertForDifferentVersion(version: String) {
|
@MainActor private func presentAlertForDifferentVersion(version: String) {
|
||||||
@@ -87,7 +87,7 @@ extension MainMenu {
|
|||||||
.withTertiary(text: "", action: { _ in
|
.withTertiary(text: "", action: { _ in
|
||||||
NSWorkspace.shared.open(Constants.Urls.FrequentlyAskedQuestions)
|
NSWorkspace.shared.open(Constants.Urls.FrequentlyAskedQuestions)
|
||||||
})
|
})
|
||||||
.show(urgency: .urgentRequestAttention)
|
.show(urgency: .bringToFront)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ extension MainMenu {
|
|||||||
alert.close(with: .OK)
|
alert.close(with: .OK)
|
||||||
self.terminateApp()
|
self.terminateApp()
|
||||||
})
|
})
|
||||||
.show(urgency: .urgentRequestAttention)
|
.show(urgency: .bringToFront)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func reloadDomainListData() async {
|
private func reloadDomainListData() async {
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ class PhpGuard {
|
|||||||
Stats.persistCurrentGlobalPhpVersion(version: currentVersion)
|
Stats.persistCurrentGlobalPhpVersion(version: currentVersion)
|
||||||
alert.close(with: .OK)
|
alert.close(with: .OK)
|
||||||
})
|
})
|
||||||
.show(urgency: .normalRequestAttention)
|
.show(urgency: .urgentRequestAttention)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -153,7 +153,7 @@ struct Preset: Codable, Equatable {
|
|||||||
)
|
)
|
||||||
).withPrimary(
|
).withPrimary(
|
||||||
text: "alert.php_switch_unavailable.ok".localized
|
text: "alert.php_switch_unavailable.ok".localized
|
||||||
).show(urgency: .urgentRequestAttention)
|
).show(urgency: .bringToFront)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user