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 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// 指定日期,并更新日期下对应的数据
|
// 指定日期,并更新日期下对应的数据
|
||||||
@ -195,6 +215,10 @@ struct IndexView: View {
|
|||||||
@State var isMoreLoading: Bool = false
|
@State var isMoreLoading: Bool = false
|
||||||
// 前向刷新
|
// 前向刷新
|
||||||
@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 = ""
|
||||||
@ -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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user