Refactor to MVVM architecture

This commit is contained in:
世界
2025-11-26 22:25:48 +08:00
parent 61f1036f57
commit b71cbae382
31 changed files with 1551 additions and 1229 deletions
@@ -0,0 +1,66 @@
#if os(iOS) || os(macOS)
import Foundation
import Library
import SwiftUI
@MainActor
public final class EditProfileContentViewModel: ObservableObject {
@Published public var isLoading = true
@Published public var profile: Profile?
@Published public var profileContent = ""
@Published public var isChanged = false
@Published public var alert: Alert?
private let profileID: Int64?
public init(profileID: Int64?) {
self.profileID = profileID
}
public func markAsChanged() {
isChanged = true
}
public func loadContent() async {
do {
try await loadContentBackground()
} catch {
alert = Alert(error)
}
isLoading = false
}
private nonisolated func loadContentBackground() async throws {
guard let profileID else {
throw NSError(domain: "Context destroyed", code: 0)
}
guard let profile = try await ProfileManager.get(profileID) else {
throw NSError(domain: "Profile missing", code: 0)
}
let profileContent = try profile.read()
await MainActor.run {
self.profile = profile
self.profileContent = profileContent
}
}
public func saveContent() async {
guard let profile else {
return
}
do {
try await saveContentBackground(profile)
} catch {
alert = Alert(error)
return
}
isChanged = false
}
private nonisolated func saveContentBackground(_ profile: Profile) async throws {
let profileContent = await profileContent
try profile.write(profileContent)
}
}
#endif