From e3ae878cae14823461b6868c8acce07b7eb2bafd Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Fri, 18 Feb 2022 22:01:05 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20Various=20alerts=20updated?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Common/Helpers/Alert.swift | 53 ------------------- phpmon/Domain/Integrations/Valet/Valet.swift | 8 ++- phpmon/Domain/Menu/MainMenu+Composer.swift | 12 +++-- phpmon/Domain/Menu/MainMenu+Startup.swift | 12 +++-- phpmon/Domain/Menu/MainMenu.swift | 31 +++++------ phpmon/Domain/Notice/BetterAlert.swift | 11 ++++ .../PHP/ActivePhpInstallation+Checks.swift | 12 +++-- .../Domain/SiteList/SiteListVC+Actions.swift | 22 +++++--- 8 files changed, 70 insertions(+), 91 deletions(-) diff --git a/phpmon/Common/Helpers/Alert.swift b/phpmon/Common/Helpers/Alert.swift index ccc6f7d..400c257 100644 --- a/phpmon/Common/Helpers/Alert.swift +++ b/phpmon/Common/Helpers/Alert.swift @@ -7,34 +7,8 @@ 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 { - 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( onWindow window: NSWindow, 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 - ) - } } diff --git a/phpmon/Domain/Integrations/Valet/Valet.swift b/phpmon/Domain/Integrations/Valet/Valet.swift index e18c6f1..c67daed 100644 --- a/phpmon/Domain/Integrations/Valet/Valet.swift +++ b/phpmon/Domain/Integrations/Valet/Valet.swift @@ -87,7 +87,13 @@ class Valet { let version = version Log.warn("Valet version \(version!) is too old! (recommended: \(Constants.MinimumRecommendedValetVersion))") 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 { Log.info("Valet version \(version!) is recent enough, OK (recommended: \(Constants.MinimumRecommendedValetVersion))") diff --git a/phpmon/Domain/Menu/MainMenu+Composer.swift b/phpmon/Domain/Menu/MainMenu+Composer.swift index 92cf265..65c4cb7 100644 --- a/phpmon/Domain/Menu/MainMenu+Composer.swift +++ b/phpmon/Domain/Menu/MainMenu+Composer.swift @@ -16,10 +16,14 @@ extension MainMenu { */ func updateGlobalDependencies(notify: Bool, completion: @escaping (Bool) -> Void) { if !Filesystem.fileExists("/usr/local/bin/composer") { - Alert.notify( - message: "alert.composer_missing.title".localized, - info: "alert.composer_missing.info".localized - ) + BetterAlert() + .withInformation( + title: "alert.composer_missing.title".localized, + subtitle: "alert.composer_missing.info".localized + ) + .withPrimary(text: "OK") + .show() + return } diff --git a/phpmon/Domain/Menu/MainMenu+Startup.swift b/phpmon/Domain/Menu/MainMenu+Startup.swift index 0e048bd..4ac6342 100644 --- a/phpmon/Domain/Menu/MainMenu+Startup.swift +++ b/phpmon/Domain/Menu/MainMenu+Startup.swift @@ -33,11 +33,13 @@ extension MainMenu { if HomebrewDiagnostics.hasAliasConflict() { DispatchQueue.main.async { - Alert.notify( - message: "alert.php_alias_conflict.title".localized, - info: "alert.php_alias_conflict.info".localized, - style: .critical - ) + BetterAlert() + .withInformation( + title: "alert.php_alias_conflict.title".localized, + subtitle: "alert.php_alias_conflict.info".localized + ) + .withPrimary(text: "OK") + .show() } } diff --git a/phpmon/Domain/Menu/MainMenu.swift b/phpmon/Domain/Menu/MainMenu.swift index 9d3891c..2035229 100644 --- a/phpmon/Domain/Menu/MainMenu.swift +++ b/phpmon/Domain/Menu/MainMenu.swift @@ -170,29 +170,30 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate, PhpSwitcherDelegate // MARK: - Actions @objc func fixHomebrewPermissions() { - if !Alert.present( - messageText: "alert.fix_homebrew_permissions.title".localized, - informativeText: "alert.fix_homebrew_permissions.info".localized, - buttonTitle: "alert.fix_homebrew_permissions.ok".localized, - secondButtonTitle: "alert.fix_homebrew_permissions.cancel".localized, - style: .warning - ) { + if BetterAlert() + .withInformation( + title: "alert.fix_homebrew_permissions.title".localized, + subtitle: "alert.fix_homebrew_permissions.info".localized + ) + .withPrimary(text: "alert.fix_homebrew_permissions.ok".localized) + .withSecondary(text: "alert.fix_homebrew_permissions.cancel".localized) + .didSelectPrimary() { return } - /* asyncExecution { try Actions.fixHomebrewPermissions() } success: { - Alert.notify( - message: "alert.fix_homebrew_permissions_done.title".localized, - info: "alert.fix_homebrew_permissions_done.info".localized, - style: .warning - ) + BetterAlert() + .withInformation( + title: "alert.fix_homebrew_permissions_done.title".localized, + subtitle: "alert.fix_homebrew_permissions_done.info".localized + ) + .withPrimary(text: "OK") + .show() } failure: { error in - await Alert.notify(about: error as! HomebrewPermissionError) + BetterAlert.show(for: error as! HomebrewPermissionError) } - */ } @objc func restartPhpFpm() { diff --git a/phpmon/Domain/Notice/BetterAlert.swift b/phpmon/Domain/Notice/BetterAlert.swift index ffcfd7c..5f0df74 100644 --- a/phpmon/Domain/Notice/BetterAlert.swift +++ b/phpmon/Domain/Notice/BetterAlert.swift @@ -105,4 +105,15 @@ class BetterAlert { public func show() { _ = 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() + } } diff --git a/phpmon/Domain/PHP/ActivePhpInstallation+Checks.swift b/phpmon/Domain/PHP/ActivePhpInstallation+Checks.swift index c1b3124..50dc957 100644 --- a/phpmon/Domain/PHP/ActivePhpInstallation+Checks.swift +++ b/phpmon/Domain/PHP/ActivePhpInstallation+Checks.swift @@ -21,11 +21,13 @@ extension ActivePhpInstallation { public func notifyAboutBrokenPhpFpm() { if !self.checkPhpFpmStatus() { DispatchQueue.main.async { - Alert.notify( - message: "alert.php_fpm_broken.title".localized, - info: "alert.php_fpm_broken.info".localized, - style: .critical - ) + BetterAlert() + .withInformation( + title: "alert.php_fpm_broken.title".localized, + subtitle: "alert.php_fpm_broken.info".localized + ) + .withPrimary(text: "OK") + .show() } } } diff --git a/phpmon/Domain/SiteList/SiteListVC+Actions.swift b/phpmon/Domain/SiteList/SiteListVC+Actions.swift index b386571..d36a86b 100644 --- a/phpmon/Domain/SiteList/SiteListVC+Actions.swift +++ b/phpmon/Domain/SiteList/SiteListVC+Actions.swift @@ -23,10 +23,13 @@ extension SiteListVC { } completion: { [self] in selectedSite.determineSecured(Valet.shared.config.tld) if selectedSite.secured == originalSecureStatus { - Alert.notify( - message: "site_list.alerts_status_not_changed.title".localized, - info: "site_list.alerts_status_not_changed.desc".localized(command) - ) + BetterAlert() + .withInformation( + title: "site_list.alerts_status_not_changed.title".localized, + subtitle: "site_list.alerts_status_not_changed.desc".localized(command) + ) + .withPrimary(text: "OK") + .show() } else { let newState = selectedSite.secured ? "secured" : "unsecured" LocalNotification.send( @@ -51,10 +54,13 @@ extension SiteListVC { if url != nil { NSWorkspace.shared.open(url!) } else { - _ = Alert.present( - messageText: "site_list.alert.invalid_folder_name".localized, - informativeText: "site_list.alert.invalid_folder_name_desc".localized - ) + BetterAlert() + .withInformation( + title: "site_list.alert.invalid_folder_name".localized, + subtitle: "site_list.alert.invalid_folder_name_desc".localized + ) + .withPrimary(text: "OK") + .show() } }