add detail alert

This commit is contained in:
anlicheng 2025-02-25 13:20:10 +08:00
parent f080cd4d13
commit bae2eb607a

View File

@ -71,6 +71,11 @@ final class DetailModel {
} }
enum FollowResult {
case success
case error(String, String)
}
var name: String = "" var name: String = ""
var summary: String = "" var summary: String = ""
var thumb: String = "" var thumb: String = ""
@ -113,12 +118,11 @@ final class DetailModel {
} }
@MainActor @MainActor
func onTapFollowButton(userId: Int, id: Int, status: String) async { func onTapFollowButton(userId: Int, id: Int, status: String) async -> FollowResult {
let response = await API.followDrama(userId: userId, id: id, status: status, as: [String].self) let response = await API.followDrama(userId: userId, id: id, status: status, as: [String].self)
switch response { switch response {
case .error(let code, let message): case .error(let code, let message):
print(code) return .error("错误", message)
print(message)
case .result(let newStatuses): case .result(let newStatuses):
self.statuses = newStatuses.flatMap({ s in self.statuses = newStatuses.flatMap({ s in
if let status = DramaStatus(s) { if let status = DramaStatus(s) {
@ -127,6 +131,7 @@ final class DetailModel {
return [] return []
} }
}) })
return .success
} }
} }
@ -135,6 +140,11 @@ final class DetailModel {
struct DetailView: View { struct DetailView: View {
@State var detailModel = DetailModel() @State var detailModel = DetailModel()
@State var showAllSummary: Bool = false @State var showAllSummary: Bool = false
//
@State var showAlert: Bool = false
@State var errorInfo: (String, String) = ("", "")
let id: Int let id: Int
var body: some View { var body: some View {
@ -175,7 +185,14 @@ struct DetailView: View {
ForEach(detailModel.statuses, id: \.status) { status in ForEach(detailModel.statuses, id: \.status) { status in
FollowButtonView(dramaStatus: status) { followStatus in FollowButtonView(dramaStatus: status) { followStatus in
Task { Task {
await detailModel.onTapFollowButton(userId: 1, id: id, status: followStatus) let result = await detailModel.onTapFollowButton(userId: 1, id: id, status: followStatus)
switch result {
case .success:
()
case .error(let title, let message):
self.errorInfo = (title, message)
self.showAlert = true
}
} }
} }
} }
@ -246,6 +263,9 @@ struct DetailView: View {
Spacer() Spacer()
} }
.frame(width: 370, alignment: .center) .frame(width: 370, alignment: .center)
.alert(isPresented: $showAlert) {
Alert(title: Text(self.errorInfo.0), message: Text(self.errorInfo.1), dismissButton: .default(Text("OK")))
}
.task { .task {
await detailModel.loadData(userId: 1, id: self.id) await detailModel.loadData(userId: 1, id: self.id)
print(detailModel.summary) print(detailModel.summary)