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

👌 Various alerts updated

This commit is contained in:
2022-02-18 22:01:05 +01:00
parent dd43c94e6e
commit e3ae878cae
8 changed files with 70 additions and 91 deletions

View File

@ -7,34 +7,8 @@
import Cocoa import Cocoa
#warning("This deprecated class should be removed at the earliest convenience once no code relies on it.")
@available(*, deprecated, message: "Use the BetterAlert API instead")
class Alert { class Alert {
public static func present(
messageText: String,
informativeText: String,
buttonTitle: String = "OK",
secondButtonTitle: String = "",
style: NSAlert.Style = .informational
) -> Bool {
if !Thread.isMainThread {
fatalError("You should always present alerts on the main thread!")
}
let alert = NSAlert.init()
alert.alertStyle = style
alert.messageText = messageText
alert.informativeText = informativeText
alert.addButton(withTitle: buttonTitle)
if (!secondButtonTitle.isEmpty) {
alert.addButton(withTitle: secondButtonTitle)
}
return alert.runModal() == .alertFirstButtonReturn
}
public static func confirm( public static func confirm(
onWindow window: NSWindow, onWindow window: NSWindow,
messageText: String, messageText: String,
@ -63,31 +37,4 @@ class Alert {
} }
} }
/**
Notify the user about something by showing an alert.
*/
public static func notify(message: String, info: String, button: String = "OK", style: NSAlert.Style = .informational) {
_ = present(
messageText: message,
informativeText: info,
buttonTitle: button,
secondButtonTitle: "",
style: style
)
}
/**
Notify the user about a particular error (which must be `Alertable`)
by showing an alert.
*/
public static func notify(about error: Error & AlertableError) {
let key = error.getErrorMessageKey()
_ = present(
messageText: "\(key).title".localized,
informativeText: "\(key).description".localized,
buttonTitle: "OK",
secondButtonTitle: "",
style: .critical
)
}
} }

View File

@ -87,7 +87,13 @@ class Valet {
let version = version let version = version
Log.warn("Valet version \(version!) is too old! (recommended: \(Constants.MinimumRecommendedValetVersion))") Log.warn("Valet version \(version!) is too old! (recommended: \(Constants.MinimumRecommendedValetVersion))")
DispatchQueue.main.async { DispatchQueue.main.async {
Alert.notify(message: "alert.min_valet_version.title".localized, info: "alert.min_valet_version.info".localized(version!, Constants.MinimumRecommendedValetVersion)) BetterAlert()
.withInformation(
title: "alert.min_valet_version.title".localized,
subtitle:"alert.min_valet_version.info".localized(version!, Constants.MinimumRecommendedValetVersion)
)
.withPrimary(text: "OK")
.show()
} }
} else { } else {
Log.info("Valet version \(version!) is recent enough, OK (recommended: \(Constants.MinimumRecommendedValetVersion))") Log.info("Valet version \(version!) is recent enough, OK (recommended: \(Constants.MinimumRecommendedValetVersion))")

View File

@ -16,10 +16,14 @@ extension MainMenu {
*/ */
func updateGlobalDependencies(notify: Bool, completion: @escaping (Bool) -> Void) { func updateGlobalDependencies(notify: Bool, completion: @escaping (Bool) -> Void) {
if !Filesystem.fileExists("/usr/local/bin/composer") { if !Filesystem.fileExists("/usr/local/bin/composer") {
Alert.notify( BetterAlert()
message: "alert.composer_missing.title".localized, .withInformation(
info: "alert.composer_missing.info".localized title: "alert.composer_missing.title".localized,
subtitle: "alert.composer_missing.info".localized
) )
.withPrimary(text: "OK")
.show()
return return
} }

View File

@ -33,11 +33,13 @@ extension MainMenu {
if HomebrewDiagnostics.hasAliasConflict() { if HomebrewDiagnostics.hasAliasConflict() {
DispatchQueue.main.async { DispatchQueue.main.async {
Alert.notify( BetterAlert()
message: "alert.php_alias_conflict.title".localized, .withInformation(
info: "alert.php_alias_conflict.info".localized, title: "alert.php_alias_conflict.title".localized,
style: .critical subtitle: "alert.php_alias_conflict.info".localized
) )
.withPrimary(text: "OK")
.show()
} }
} }

View File

@ -170,29 +170,30 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate, PhpSwitcherDelegate
// MARK: - Actions // MARK: - Actions
@objc func fixHomebrewPermissions() { @objc func fixHomebrewPermissions() {
if !Alert.present( if BetterAlert()
messageText: "alert.fix_homebrew_permissions.title".localized, .withInformation(
informativeText: "alert.fix_homebrew_permissions.info".localized, title: "alert.fix_homebrew_permissions.title".localized,
buttonTitle: "alert.fix_homebrew_permissions.ok".localized, subtitle: "alert.fix_homebrew_permissions.info".localized
secondButtonTitle: "alert.fix_homebrew_permissions.cancel".localized, )
style: .warning .withPrimary(text: "alert.fix_homebrew_permissions.ok".localized)
) { .withSecondary(text: "alert.fix_homebrew_permissions.cancel".localized)
.didSelectPrimary() {
return return
} }
/*
asyncExecution { asyncExecution {
try Actions.fixHomebrewPermissions() try Actions.fixHomebrewPermissions()
} success: { } success: {
Alert.notify( BetterAlert()
message: "alert.fix_homebrew_permissions_done.title".localized, .withInformation(
info: "alert.fix_homebrew_permissions_done.info".localized, title: "alert.fix_homebrew_permissions_done.title".localized,
style: .warning subtitle: "alert.fix_homebrew_permissions_done.info".localized
) )
.withPrimary(text: "OK")
.show()
} failure: { error in } failure: { error in
await Alert.notify(about: error as! HomebrewPermissionError) BetterAlert.show(for: error as! HomebrewPermissionError)
} }
*/
} }
@objc func restartPhpFpm() { @objc func restartPhpFpm() {

View File

@ -105,4 +105,15 @@ class BetterAlert {
public func show() { public func show() {
_ = self.runModal() _ = self.runModal()
} }
/**
Shows the modal for a particular error.
*/
public static func show(for error: Error & AlertableError) {
let key = error.getErrorMessageKey()
return BetterAlert().withInformation(
title: "\(key).title".localized,
subtitle: "\(key).description".localized
).withPrimary(text: "OK").show()
}
} }

View File

@ -21,11 +21,13 @@ extension ActivePhpInstallation {
public func notifyAboutBrokenPhpFpm() { public func notifyAboutBrokenPhpFpm() {
if !self.checkPhpFpmStatus() { if !self.checkPhpFpmStatus() {
DispatchQueue.main.async { DispatchQueue.main.async {
Alert.notify( BetterAlert()
message: "alert.php_fpm_broken.title".localized, .withInformation(
info: "alert.php_fpm_broken.info".localized, title: "alert.php_fpm_broken.title".localized,
style: .critical subtitle: "alert.php_fpm_broken.info".localized
) )
.withPrimary(text: "OK")
.show()
} }
} }
} }

View File

@ -23,10 +23,13 @@ extension SiteListVC {
} completion: { [self] in } completion: { [self] in
selectedSite.determineSecured(Valet.shared.config.tld) selectedSite.determineSecured(Valet.shared.config.tld)
if selectedSite.secured == originalSecureStatus { if selectedSite.secured == originalSecureStatus {
Alert.notify( BetterAlert()
message: "site_list.alerts_status_not_changed.title".localized, .withInformation(
info: "site_list.alerts_status_not_changed.desc".localized(command) title: "site_list.alerts_status_not_changed.title".localized,
subtitle: "site_list.alerts_status_not_changed.desc".localized(command)
) )
.withPrimary(text: "OK")
.show()
} else { } else {
let newState = selectedSite.secured ? "secured" : "unsecured" let newState = selectedSite.secured ? "secured" : "unsecured"
LocalNotification.send( LocalNotification.send(
@ -51,10 +54,13 @@ extension SiteListVC {
if url != nil { if url != nil {
NSWorkspace.shared.open(url!) NSWorkspace.shared.open(url!)
} else { } else {
_ = Alert.present( BetterAlert()
messageText: "site_list.alert.invalid_folder_name".localized, .withInformation(
informativeText: "site_list.alert.invalid_folder_name_desc".localized title: "site_list.alert.invalid_folder_name".localized,
subtitle: "site_list.alert.invalid_folder_name_desc".localized
) )
.withPrimary(text: "OK")
.show()
} }
} }