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

👌 Improvements to BetterAlert, apply() configs

- MainActor fixes for BetterAlert
- Added `apply` for TestableConfiguration
This commit is contained in:
2022-10-12 22:19:36 +02:00
parent ec4c4df5fd
commit 12a4efc775
9 changed files with 42 additions and 35 deletions

View File

@ -76,7 +76,7 @@
<EnvironmentVariable
key = "SLOW_SHELL_MODE"
value = ""
isEnabled = "YES">
isEnabled = "NO">
</EnvironmentVariable>
<EnvironmentVariable
key = "PAINT_PHPMON_SWIFTUI_VIEWS"

View File

@ -16,8 +16,7 @@ class ActiveFileSystem {
static var shared: FileSystemProtocol = RealFileSystem()
public static func useTestable(_ files: [String: FakeFile]) {
// TODO
// Self.shared = TestableShell(expectations: expectations)
Self.shared = TestableFileSystem(files: files)
}
public static func useSystem() {

View File

@ -12,6 +12,11 @@ struct TestableConfiguration {
let architecture: String
let filesystem: [String: FakeFile]
let shellOutput: [String: BatchFakeShellOutput]
func apply() {
ActiveShell.useTestable(shellOutput)
ActiveFileSystem.useTestable(filesystem)
}
}
// swiftlint:disable colon trailing_comma

View File

@ -9,6 +9,12 @@
import Foundation
class TestableFileSystem: FileSystemProtocol {
init(files: [String: FakeFile]) {
self.files = files
}
var files: [String: FakeFile]
func isExecutableFile(_ path: String) -> Bool {
// TODO
return false

View File

@ -57,13 +57,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
*/
override init() {
logger.verbosity = .info
#if DEBUG
logger.verbosity = .performance
// TODO: Enable to fake broken setup during testing
// ActiveShell.useTestable(Testables.working.shellOutput)
TestableConfigurations.working.apply()
#endif
if CommandLine.arguments.contains("--v") {
logger.verbosity = .performance
Log.info("Extra verbose mode has been activated.")

View File

@ -29,7 +29,7 @@ class Startup {
// If we get here, something's gone wrong and the check has failed...
Log.info("[FAIL] \(check.name)")
showAlert(for: check)
await showAlert(for: check)
return false
}
@ -45,29 +45,27 @@ class Startup {
- ones that require an app restart, which prompt the user to exit the app
- ones that allow the app to continue, which allow the user to retry
*/
private func showAlert(for check: EnvironmentCheck) {
DispatchQueue.main.async {
if check.requiresAppRestart {
BetterAlert()
.withInformation(
title: check.titleText,
subtitle: check.subtitleText,
description: check.descriptionText
)
.withPrimary(text: check.buttonText, action: { _ in
exit(1)
}).show()
}
@MainActor private func showAlert(for check: EnvironmentCheck) {
if check.requiresAppRestart {
BetterAlert()
.withInformation(
title: check.titleText,
subtitle: check.subtitleText,
description: check.descriptionText
)
.withPrimary(text: "OK")
.show()
.withPrimary(text: check.buttonText, action: { _ in
exit(1)
}).show()
}
BetterAlert()
.withInformation(
title: check.titleText,
subtitle: check.subtitleText,
description: check.descriptionText
)
.withPrimary(text: "OK")
.show()
}
/**

View File

@ -12,7 +12,7 @@ extension MainMenu {
// MARK: - Actions
@objc func fixHomebrewPermissions() {
@MainActor @objc func fixHomebrewPermissions() {
if !BetterAlert()
.withInformation(
title: "alert.fix_homebrew_permissions.title".localized,

View File

@ -31,8 +31,8 @@ class BetterAlert {
public func withPrimary(
text: String,
action: @escaping (BetterAlertVC) -> Void = { vc in
DispatchQueue.main.async { vc.close(with: .alertFirstButtonReturn) }
action: @MainActor @escaping (BetterAlertVC) -> Void = { vc in
vc.close(with: .alertFirstButtonReturn)
}
) -> Self {
self.noticeVC.buttonPrimary.title = text
@ -42,8 +42,8 @@ class BetterAlert {
public func withSecondary(
text: String,
action: ((BetterAlertVC) -> Void)? = { vc in
DispatchQueue.main.async { vc.close(with: .alertSecondButtonReturn) }
action: (@MainActor (BetterAlertVC) -> Void)? = { vc in
vc.close(with: .alertSecondButtonReturn)
}
) -> Self {
self.noticeVC.buttonSecondary.title = text
@ -53,7 +53,7 @@ class BetterAlert {
public func withTertiary(
text: String = "",
action: ((BetterAlertVC) -> Void)? = nil
action: (@MainActor (BetterAlertVC) -> Void)? = nil
) -> Self {
if text == "" {
self.noticeVC.buttonTertiary.bezelStyle = .helpButton
@ -84,7 +84,7 @@ class BetterAlert {
Shows the modal and returns a ModalResponse.
If you wish to simply show the alert and disregard the outcome, use `show`.
*/
public func runModal() -> NSApplication.ModalResponse {
@MainActor public func runModal() -> NSApplication.ModalResponse {
if !Thread.isMainThread {
fatalError("You should always present alerts on the main thread!")
}
@ -96,7 +96,7 @@ class BetterAlert {
}
/** Shows the modal and returns true if the user pressed the primary button. */
public func didSelectPrimary() -> Bool {
@MainActor public func didSelectPrimary() -> Bool {
return self.runModal() == .alertFirstButtonReturn
}

View File

@ -21,9 +21,9 @@ class BetterAlertVC: NSViewController {
@IBOutlet weak var buttonSecondary: NSButton!
@IBOutlet weak var buttonTertiary: NSButton!
var actionPrimary: (BetterAlertVC) -> Void = { _ in }
var actionSecondary: ((BetterAlertVC) -> Void)?
var actionTertiary: ((BetterAlertVC) -> Void)?
var actionPrimary: (@MainActor (BetterAlertVC) -> Void) = { _ in }
var actionSecondary: (@MainActor (BetterAlertVC) -> Void)?
var actionTertiary: (@MainActor (BetterAlertVC) -> Void)?
@IBOutlet weak var imageView: NSImageView!