mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-07 03:50:08 +02:00
👌 Improve async PHP Doctor
This commit is contained in:
@ -13,7 +13,7 @@ struct WarningListView: View {
|
||||
|
||||
init(empty: Bool = false) {
|
||||
if empty {
|
||||
WarningManager.shared.warnings = []
|
||||
WarningManager.shared.clearWarnings()
|
||||
}
|
||||
|
||||
warningManager = WarningManager.shared
|
||||
@ -54,7 +54,7 @@ struct WarningListView: View {
|
||||
|
||||
List {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
if warningManager.warnings.isEmpty {
|
||||
if !warningManager.hasWarnings() {
|
||||
NoWarningsView()
|
||||
} else {
|
||||
ForEach(warningManager.warnings) { warning in
|
||||
|
@ -13,6 +13,14 @@ class WarningManager: ObservableObject {
|
||||
|
||||
static var shared: WarningManager = WarningManager()
|
||||
|
||||
/// These warnings are the ones that are ready to be displayed.
|
||||
@Published public var warnings: [Warning] = []
|
||||
|
||||
/// This variable is thread-safe and may be modified at any time.
|
||||
/// When all temporary warnings are set, you may broadcast these changes
|
||||
/// and they will be sent to the @Published variable via the main thread.
|
||||
private var temporaryWarnings: [Warning] = []
|
||||
|
||||
init() {
|
||||
if isRunningSwiftUIPreview {
|
||||
self.warnings = self.evaluations
|
||||
@ -60,8 +68,6 @@ class WarningManager: ObservableObject {
|
||||
)
|
||||
]
|
||||
|
||||
@Published public var warnings: [Warning] = []
|
||||
|
||||
public func hasWarnings() -> Bool {
|
||||
return !warnings.isEmpty
|
||||
}
|
||||
@ -70,33 +76,37 @@ class WarningManager: ObservableObject {
|
||||
Task { await WarningManager.shared.checkEnvironment() }
|
||||
}
|
||||
|
||||
@MainActor func clearWarnings() {
|
||||
self.warnings = []
|
||||
}
|
||||
|
||||
@MainActor func broadcastWarnings() {
|
||||
self.warnings = temporaryWarnings
|
||||
}
|
||||
|
||||
/**
|
||||
Checks the user's environment and checks if any special warnings apply.
|
||||
*/
|
||||
func checkEnvironment() async {
|
||||
if ProcessInfo.processInfo.environment["EXTREME_DOCTOR_MODE"] != nil {
|
||||
// For debugging purposes, we may wish to see all possible evaluations listed
|
||||
Task { @MainActor in
|
||||
self.warnings = self.evaluations
|
||||
}
|
||||
} else {
|
||||
// Otherwise, loop over the actual evaluations and list the warnings
|
||||
await loopOverEvaluations()
|
||||
self.temporaryWarnings = self.evaluations
|
||||
await self.broadcastWarnings()
|
||||
return
|
||||
}
|
||||
|
||||
await evaluate()
|
||||
await MainMenu.shared.rebuild()
|
||||
}
|
||||
|
||||
private func loopOverEvaluations() async {
|
||||
Task { @MainActor in
|
||||
self.warnings = []
|
||||
}
|
||||
private func evaluate() async {
|
||||
self.temporaryWarnings = []
|
||||
|
||||
for check in self.evaluations where await check.applies() {
|
||||
Log.info("[DOCTOR] \(check.name) (!)")
|
||||
Task { @MainActor in
|
||||
self.warnings.append(check)
|
||||
}
|
||||
self.temporaryWarnings.append(check)
|
||||
continue
|
||||
}
|
||||
|
||||
await self.broadcastWarnings()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user