Refactor ViewModel

This commit is contained in:
世界
2025-12-01 17:11:34 +08:00
parent 30492c1191
commit 8e764c56eb
34 changed files with 136 additions and 160 deletions
@@ -0,0 +1,31 @@
import SwiftUI
@MainActor
open class BaseViewModel: ObservableObject {
@Published public var alert: Alert?
@Published public var isLoading = false
public init() {}
public func showError(_ error: Error) {
alert = Alert(error)
}
public func execute(_ operation: () async throws -> Void) async {
do {
try await operation()
} catch {
alert = Alert(error)
}
}
public func executeOnBackground(_ operation: @escaping @Sendable () async throws -> Void) async {
do {
try await operation()
} catch {
await MainActor.run {
alert = Alert(error)
}
}
}
}