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)
}
}
}
}
@@ -35,7 +35,7 @@ public struct NavigationButtonsView: View {
#if os(tvOS)
private var tvOSBody: some View {
viewBuilder {
Group {
if showConnectionsButton {
Button {
onConnectionsTap()
@@ -1,7 +1,7 @@
import SwiftUI
public func NavigationStackCompat(@ViewBuilder content: () -> some View) -> some View {
viewBuilder {
Group {
if #available(iOS 17.0, macOS 13.0, tvOS 17.0, *) {
// view not updating in iOS 16, but why?
NavigationStack {
@@ -4,7 +4,7 @@
import SwiftUI
public func RequestReviewButton(label: @escaping () -> some View) -> some View {
viewBuilder {
Group {
if #available(iOS 16.0, macOS 13.0, visionOS 1.0, *) {
RequestReviewButton0(label: label)
} else {
@@ -1,6 +0,0 @@
import Foundation
import SwiftUI
public func viewBuilder(@ViewBuilder _ builder: () -> some View) -> some View {
builder()
}
@@ -2,22 +2,12 @@ import SwiftUI
public extension View {
func onChangeCompat(of value: some Equatable, _ action: @escaping () -> Void) -> some View {
// if #available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) {
// return onChange(of: value, action)
// } else {
onChange(of: value) { _ in
action()
}
// }
}
func onChangeCompat<V>(of value: V, _ action: @escaping (_ newValue: V) -> Void) -> some View where V: Equatable {
// if #available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) {
// return onChange(of: value) { _, newValue in
// action(newValue)
// }
// } else {
onChange(of: value, perform: action)
// }
}
}