1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-08 21:37:05 +01:00

🏗 Use @MainActor for BetterAlert

This commit is contained in:
2022-06-18 17:59:43 +02:00
parent ea7572ffbb
commit c5fd43312f
9 changed files with 44 additions and 42 deletions

View File

@@ -10,7 +10,7 @@ import UserNotifications
class LocalNotification { class LocalNotification {
public static func send(title: String, subtitle: String, preference: PreferenceName) { @MainActor public static func send(title: String, subtitle: String, preference: PreferenceName) {
if !Preferences.isEnabled(preference) { if !Preferences.isEnabled(preference) {
return return
} }

View File

@@ -56,6 +56,7 @@ class InterApp {
if PhpEnv.shared.availablePhpVersions.contains(version) { if PhpEnv.shared.availablePhpVersions.contains(version) {
MainMenu.shared.switchToPhpVersion(version) MainMenu.shared.switchToPhpVersion(version)
} else { } else {
DispatchQueue.main.async {
BetterAlert().withInformation( BetterAlert().withInformation(
title: "alert.php_switch_unavailable.title".localized, title: "alert.php_switch_unavailable.title".localized,
subtitle: "alert.php_switch_unavailable.subtitle".localized(version) subtitle: "alert.php_switch_unavailable.subtitle".localized(version)
@@ -63,6 +64,7 @@ class InterApp {
text: "alert.php_switch_unavailable.ok".localized text: "alert.php_switch_unavailable.ok".localized
).show() ).show()
} }
}
}) })
]} ]}

View File

@@ -25,7 +25,9 @@ class ComposerWindow {
Paths.shared.detectBinaryPaths() Paths.shared.detectBinaryPaths()
if Paths.composer == nil { if Paths.composer == nil {
presentMissingAlert() DispatchQueue.main.async {
self.presentMissingAlert()
}
return return
} }
@@ -116,7 +118,7 @@ class ComposerWindow {
// MARK: Alert // MARK: Alert
private func presentMissingAlert() { @MainActor private func presentMissingAlert() {
BetterAlert() BetterAlert()
.withInformation( .withInformation(
title: "alert.composer_missing.title".localized, title: "alert.composer_missing.title".localized,

View File

@@ -53,7 +53,6 @@ extension MainMenu {
Actions.restartPhpFpm() Actions.restartPhpFpm()
Actions.restartNginx() Actions.restartNginx()
} success: { } success: {
DispatchQueue.main.async {
LocalNotification.send( LocalNotification.send(
title: "notification.services_restarted".localized, title: "notification.services_restarted".localized,
subtitle: "notification.services_restarted_desc".localized, subtitle: "notification.services_restarted_desc".localized,
@@ -61,13 +60,11 @@ extension MainMenu {
) )
} }
} }
}
@objc func stopValetServices() { @objc func stopValetServices() {
asyncExecution { asyncExecution {
Actions.stopValetServices() Actions.stopValetServices()
} success: { } success: {
DispatchQueue.main.async {
LocalNotification.send( LocalNotification.send(
title: "notification.services_stopped".localized, title: "notification.services_stopped".localized,
subtitle: "notification.services_stopped_desc".localized, subtitle: "notification.services_stopped_desc".localized,
@@ -75,7 +72,6 @@ extension MainMenu {
) )
} }
} }
}
@objc func restartNginx() { @objc func restartNginx() {
asyncExecution { asyncExecution {
@@ -155,7 +151,7 @@ extension MainMenu {
} }
} }
@objc func rollbackPreset() { @MainActor @objc func rollbackPreset() {
guard let preset = PresetHelper.rollbackPreset else { guard let preset = PresetHelper.rollbackPreset else {
return return
} }
@@ -180,7 +176,7 @@ extension MainMenu {
} }
} }
@objc func showPresetHelp() { @MainActor @objc func showPresetHelp() {
BetterAlert().withInformation( BetterAlert().withInformation(
title: "preset_help_title".localized, title: "preset_help_title".localized,
subtitle: "preset_help_info".localized, subtitle: "preset_help_info".localized,

View File

@@ -36,8 +36,8 @@ extension MainMenu {
*/ */
func asyncExecution( func asyncExecution(
_ execute: @escaping () throws -> Void, _ execute: @escaping () throws -> Void,
success: @escaping () -> Void = {}, success: @MainActor @escaping () -> Void = {},
failure: @escaping (Error) -> Void = { _ in }, failure: @MainActor @escaping (Error) -> Void = { _ in },
behaviours: [AsyncBehaviour] = [ behaviours: [AsyncBehaviour] = [
.setsBusyUI, .setsBusyUI,
.reloadsPhpInstallation, .reloadsPhpInstallation,

View File

@@ -11,7 +11,7 @@ import AppKit
extension MainMenu { extension MainMenu {
@objc func fixMyValet() { @MainActor @objc func fixMyValet() {
let previousVersion = PhpEnv.phpInstall.version.short let previousVersion = PhpEnv.phpInstall.version.short
if !PhpEnv.shared.availablePhpVersions.contains(PhpEnv.brewPhpVersion) { if !PhpEnv.shared.availablePhpVersions.contains(PhpEnv.brewPhpVersion) {
@@ -42,7 +42,7 @@ extension MainMenu {
} }
} }
private func presentAlertForMissingFormula() { @MainActor private func presentAlertForMissingFormula() {
BetterAlert() BetterAlert()
.withInformation( .withInformation(
title: "alert.php_formula_missing.title".localized, title: "alert.php_formula_missing.title".localized,
@@ -52,7 +52,7 @@ extension MainMenu {
.show() .show()
} }
private func presentAlertForSameVersion() { @MainActor private func presentAlertForSameVersion() {
BetterAlert() BetterAlert()
.withInformation( .withInformation(
title: "alert.fix_my_valet_done.title".localized, title: "alert.fix_my_valet_done.title".localized,
@@ -63,7 +63,7 @@ extension MainMenu {
.show() .show()
} }
private func presentAlertForDifferentVersion(version: String) { @MainActor private func presentAlertForDifferentVersion(version: String) {
BetterAlert() BetterAlert()
.withInformation( .withInformation(
title: "alert.fix_my_valet_done.title".localized, title: "alert.fix_my_valet_done.title".localized,

View File

@@ -50,7 +50,7 @@ extension MainMenu {
} }
} }
private func suggestFixMyValet(failed version: String) { @MainActor private func suggestFixMyValet(failed version: String) {
let outcome = BetterAlert() let outcome = BetterAlert()
.withInformation( .withInformation(
title: "alert.php_switch_failed.title".localized(version), title: "alert.php_switch_failed.title".localized(version),
@@ -75,6 +75,7 @@ extension MainMenu {
} }
private func notifyAboutVersionChange(to version: String) { private func notifyAboutVersionChange(to version: String) {
DispatchQueue.main.async {
LocalNotification.send( LocalNotification.send(
title: String(format: "notification.version_changed_title".localized, version), title: String(format: "notification.version_changed_title".localized, version),
subtitle: String(format: "notification.version_changed_desc".localized, version), subtitle: String(format: "notification.version_changed_desc".localized, version),
@@ -84,3 +85,4 @@ extension MainMenu {
PhpEnv.phpInstall.notifyAboutBrokenPhpFpm() PhpEnv.phpInstall.notifyAboutBrokenPhpFpm()
} }
} }
}

View File

@@ -102,14 +102,14 @@ class BetterAlert {
/** /**
Shows the modal and does not return anything. Shows the modal and does not return anything.
*/ */
public func show() { @MainActor public func show() {
_ = self.runModal() _ = self.runModal()
} }
/** /**
Shows the modal for a particular error. Shows the modal for a particular error.
*/ */
public static func show(for error: Error & AlertableError) { @MainActor public static func show(for error: Error & AlertableError) {
let key = error.getErrorMessageKey() let key = error.getErrorMessageKey()
return BetterAlert().withInformation( return BetterAlert().withInformation(
title: "\(key).title".localized, title: "\(key).title".localized,

View File

@@ -107,13 +107,13 @@ struct Preset: Codable, Equatable {
// Show the correct notification // Show the correct notification
if wasRollback { if wasRollback {
LocalNotification.send( await LocalNotification.send(
title: "notification.preset_reverted_title".localized, title: "notification.preset_reverted_title".localized,
subtitle: "notification.preset_reverted_desc".localized, subtitle: "notification.preset_reverted_desc".localized,
preference: .notifyAboutPresets preference: .notifyAboutPresets
) )
} else { } else {
LocalNotification.send( await LocalNotification.send(
title: "notification.preset_applied_title".localized, title: "notification.preset_applied_title".localized,
subtitle: "notification.preset_applied_desc".localized(self.name), subtitle: "notification.preset_applied_desc".localized(self.name),
preference: .notifyAboutPresets preference: .notifyAboutPresets