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

View File

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