index promot

This commit is contained in:
anlicheng 2025-02-25 14:39:28 +08:00
parent ae31aea9f2
commit e12b713b8f

View File

@ -63,6 +63,11 @@ final class IndexModel {
} }
} }
enum LoadMoreResult {
case success
case error(String)
}
var dramas: [DramaItem] var dramas: [DramaItem]
var showUpdateDramas: [UpdateDramaShowItem] var showUpdateDramas: [UpdateDramaShowItem]
var selectedDate: String var selectedDate: String
@ -91,7 +96,7 @@ final class IndexModel {
} }
@MainActor @MainActor
func loadMoreUpdateDramas(userId: String, mode: API.LoadMode) async { func loadMoreUpdateDramas(userId: String, mode: API.LoadMode) async -> LoadMoreResult {
// id // id
let dramaIds = self.getDramaIds(self.updateDramaGroups) let dramaIds = self.getDramaIds(self.updateDramaGroups)
print("current ids: \(dramaIds)") print("current ids: \(dramaIds)")
@ -102,19 +107,34 @@ final class IndexModel {
if let firstId = dramaIds.first { if let firstId = dramaIds.first {
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: firstId, as: [UpdateDramaGroup].self) let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: firstId, as: [UpdateDramaGroup].self)
if case let .result(groups) = response { if case let .result(groups) = response {
self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) if groups.count > 0 {
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups) self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups)
return .success
} else {
return .error("没有更多数据")
}
} else {
return .error("加载失败")
} }
} }
case .next: case .next:
if let lastId = dramaIds.last { if let lastId = dramaIds.last {
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: lastId, as: [UpdateDramaGroup].self) let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: lastId, as: [UpdateDramaGroup].self)
if case let .result(groups) = response { if case let .result(groups) = response {
self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) if groups.count > 0 {
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups) self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups)
return .success
} else {
return .error("没有更多数据")
}
} else {
return .error("加载失败")
} }
} }
} }
return .success
} }
// //
@ -196,6 +216,10 @@ struct IndexView: View {
// //
@State var isPrevLoading: Bool = false @State var isPrevLoading: Bool = false
//
@State var showPrompt: Bool = false
@State var promptMessage: String = ""
// //
@State private var selectGroupId: String = "" @State private var selectGroupId: String = ""
@State private var showDateNavPopover: Bool = false @State private var showDateNavPopover: Bool = false
@ -287,7 +311,16 @@ struct IndexView: View {
if screenBounds.height - frame.minY > 50 && contextFrame.minY > 0 && !isMoreLoading { if screenBounds.height - frame.minY > 50 && contextFrame.minY > 0 && !isMoreLoading {
Task { Task {
self.isMoreLoading = true self.isMoreLoading = true
await self.indexModel.loadMoreUpdateDramas(userId: self.userId, mode: .next) let result = await self.indexModel.loadMoreUpdateDramas(userId: self.userId, mode: .next)
switch result {
case .success:
()
case .error(let message):
DispatchQueue.main.async {
self.showPrompt = true
self.promptMessage = message
}
}
self.isMoreLoading = false self.isMoreLoading = false
} }
} }
@ -314,16 +347,27 @@ struct IndexView: View {
// //
self.isPrevLoading = true self.isPrevLoading = true
await self.indexModel.loadMoreUpdateDramas(userId: self.userId, mode: .prev) let result = await self.indexModel.loadMoreUpdateDramas(userId: self.userId, mode: .prev)
switch result {
case .success:
()
case .error(let message):
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.showPrompt = true
self.promptMessage = message
}
}
self.isPrevLoading = false self.isPrevLoading = false
} }
} }
.frame(width: 370) .frame(width: 370)
.ignoresSafeArea(edges: .bottom) .ignoresSafeArea(edges: .bottom)
.alert(isPresented: $showPrompt) {
Alert(title: Text("提示"), message: Text(self.promptMessage), dismissButton: .default(Text("Ok")))
}
.task { .task {
await self.indexModel.loadData(userId: self.userId) await self.indexModel.loadData(userId: self.userId)
//print(userModel)
} }
} }
} }