index promot
This commit is contained in:
parent
ae31aea9f2
commit
e12b713b8f
@ -63,6 +63,11 @@ final class IndexModel {
|
||||
}
|
||||
}
|
||||
|
||||
enum LoadMoreResult {
|
||||
case success
|
||||
case error(String)
|
||||
}
|
||||
|
||||
var dramas: [DramaItem]
|
||||
var showUpdateDramas: [UpdateDramaShowItem]
|
||||
var selectedDate: String
|
||||
@ -91,7 +96,7 @@ final class IndexModel {
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func loadMoreUpdateDramas(userId: String, mode: API.LoadMode) async {
|
||||
func loadMoreUpdateDramas(userId: String, mode: API.LoadMode) async -> LoadMoreResult {
|
||||
// 按照id来判断不一定正确,需要借助其他值
|
||||
let dramaIds = self.getDramaIds(self.updateDramaGroups)
|
||||
print("current ids: \(dramaIds)")
|
||||
@ -102,19 +107,34 @@ final class IndexModel {
|
||||
if let firstId = dramaIds.first {
|
||||
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: firstId, as: [UpdateDramaGroup].self)
|
||||
if case let .result(groups) = response {
|
||||
if groups.count > 0 {
|
||||
self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
|
||||
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups)
|
||||
return .success
|
||||
} else {
|
||||
return .error("没有更多数据")
|
||||
}
|
||||
} else {
|
||||
return .error("加载失败")
|
||||
}
|
||||
}
|
||||
case .next:
|
||||
if let lastId = dramaIds.last {
|
||||
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: lastId, as: [UpdateDramaGroup].self)
|
||||
if case let .result(groups) = response {
|
||||
if groups.count > 0 {
|
||||
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 showPrompt: Bool = false
|
||||
@State var promptMessage: String = ""
|
||||
|
||||
// 是否显示日期弹出层
|
||||
@State private var selectGroupId: String = ""
|
||||
@State private var showDateNavPopover: Bool = false
|
||||
@ -287,7 +311,16 @@ struct IndexView: View {
|
||||
if screenBounds.height - frame.minY > 50 && contextFrame.minY > 0 && !isMoreLoading {
|
||||
Task {
|
||||
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
|
||||
}
|
||||
}
|
||||
@ -314,16 +347,27 @@ struct IndexView: View {
|
||||
|
||||
// 上拉刷新功能
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
.frame(width: 370)
|
||||
.ignoresSafeArea(edges: .bottom)
|
||||
.alert(isPresented: $showPrompt) {
|
||||
Alert(title: Text("提示"), message: Text(self.promptMessage), dismissButton: .default(Text("Ok")))
|
||||
}
|
||||
.task {
|
||||
await self.indexModel.loadData(userId: self.userId)
|
||||
//print(userModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user