1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-08 04:20:07 +02:00

👀 First attempt at using async code in Swift

This commit is contained in:
2022-02-12 13:17:17 +01:00
parent 537536d443
commit e6fbe7c4a4
3 changed files with 29 additions and 28 deletions

View File

@ -91,7 +91,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
// Make sure notifications will work
setupNotifications()
// Make sure the menu performs its initial checks
menu.startup()
Task { await menu.startup() }
}
}

View File

@ -17,13 +17,13 @@ class Startup {
- Parameter success: Callback that is fired if the application can proceed with launch
- Parameter failure: Callback that is fired if the application must retry launch
*/
func checkEnvironment(success: @escaping () -> Void, failure: @escaping () -> Void)
func checkEnvironment() async -> Bool
{
// Do the important system setup checks
Log.info("[ARCH] The user is running PHP Monitor with the architecture: \(App.architecture)")
for check in self.checks {
if check.succeeds() {
if await check.succeeds() {
Log.info("[OK] \(check.name)")
continue
}
@ -31,18 +31,18 @@ class Startup {
// If we get here, something's gone wrong and the check has failed
Log.info("[FAIL] \(check.name)")
showAlert(for: check)
failure()
return
return false
}
// If we get here, nothing has gone wrong. That's what we want!
initializeSwitcher()
Log.info("==================================")
Log.info("PHP Monitor has determined the application has successfully passed all checks.")
success()
return true
}
private func showAlert(for check: EnvironmentCheck) {
DispatchQueue.main.async {
if check.requiresAppRestart {
Alert.notify(
message: check.titleText,
@ -59,6 +59,7 @@ class Startup {
style: .critical
)
}
}
/**
Because the Switcher requires various environment guarantees, the switcher is only
@ -152,7 +153,7 @@ class Startup {
Checks that require an app restart will always lead to an alert and app termination shortly after.
*/
struct EnvironmentCheck {
let command: () -> Bool
let command: () async -> Bool
let name: String
let titleText: String
let descriptionText: String
@ -160,7 +161,7 @@ class Startup {
let requiresAppRestart: Bool
init(
command: @escaping () -> Bool,
command: @escaping () async -> Bool,
name: String,
titleText: String,
descriptionText: String,
@ -175,8 +176,8 @@ class Startup {
self.requiresAppRestart = requiresAppRestart
}
public func succeeds() -> Bool {
return !self.command()
public func succeeds() async -> Bool {
return await !self.command()
}
}
}

View File

@ -12,16 +12,16 @@ extension MainMenu {
/**
Kick off the startup of the rendering of the main menu.
*/
func startup() {
func startup() async {
// Start with the icon
setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
// Perform environment boot checks
DispatchQueue.main.async {
Startup().checkEnvironment(
success: { self.onEnvironmentPass() },
failure: { self.onEnvironmentFail() }
)
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
}
if await Startup().checkEnvironment() {
self.onEnvironmentPass()
} else {
self.onEnvironmentFail()
}
}
@ -113,7 +113,7 @@ extension MainMenu {
exit(1)
}
startup()
Task { await startup() }
}
}
}