fix 数据的加载逻辑
This commit is contained in:
parent
ebdeb2f0ac
commit
ae1ff01f88
@ -30,9 +30,7 @@ struct IndexMainView: View {
|
|||||||
|
|
||||||
// 刷新逻辑
|
// 刷新逻辑
|
||||||
@State private var headerRefreshing: Bool = false
|
@State private var headerRefreshing: Bool = false
|
||||||
|
|
||||||
@State private var footerRefreshing: Bool = false
|
@State private var footerRefreshing: Bool = false
|
||||||
@State private var noMore: Bool = false
|
|
||||||
|
|
||||||
@State private var scrollOffset: CGFloat = 0
|
@State private var scrollOffset: CGFloat = 0
|
||||||
@State private var showFloatGroupLabel: Bool = false
|
@State private var showFloatGroupLabel: Bool = false
|
||||||
@ -132,11 +130,11 @@ struct IndexMainView: View {
|
|||||||
// 上拉刷新功能
|
// 上拉刷新功能
|
||||||
self.headerRefreshing = true
|
self.headerRefreshing = true
|
||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
let loadNum = await self.indexModel.loadPrevUpdateDramasTask(userId: self.userId) { _ in
|
defer {
|
||||||
DispatchQueue.main.async {
|
self.headerRefreshing = false
|
||||||
self.headerRefreshing = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let loadNum = await self.indexModel.loadPrevUpdateDramasTask(userId: self.userId)
|
||||||
self.hasMoreNewest = loadNum > 3
|
self.hasMoreNewest = loadNum > 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,13 +176,18 @@ struct IndexMainView: View {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
guard !self.showDateNavPopover && !self.footerRefreshing else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
self.footerRefreshing = true
|
||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
let num = await indexModel.loadMoreUpdateDramasTask(userId: self.userId)
|
defer {
|
||||||
if num > 3 {
|
self.footerRefreshing = false
|
||||||
self.hasMoreOldest = true
|
|
||||||
} else {
|
|
||||||
self.hasMoreOldest = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let num = await indexModel.loadMoreUpdateDramasTask(userId: self.userId)
|
||||||
|
self.hasMoreOldest = num > 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
|
|||||||
@ -91,10 +91,6 @@ final class IndexModel {
|
|||||||
@ObservationIgnored
|
@ObservationIgnored
|
||||||
private var isLoaded = false
|
private var isLoaded = false
|
||||||
|
|
||||||
// 是否正在刷新
|
|
||||||
@ObservationIgnored
|
|
||||||
private var isMoreLoading = false
|
|
||||||
|
|
||||||
// 是否显示debug信息
|
// 是否显示debug信息
|
||||||
@ObservationIgnored
|
@ObservationIgnored
|
||||||
private var debug: Bool = false
|
private var debug: Bool = false
|
||||||
@ -229,16 +225,11 @@ final class IndexModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadMoreUpdateDramasTask(userId: String) async -> Int {
|
func loadMoreUpdateDramasTask(userId: String) async -> Int {
|
||||||
guard !self.isMoreLoading else {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// 按照id来判断不一定正确,需要借助其他值
|
// 按照id来判断不一定正确,需要借助其他值
|
||||||
let dramaIds = self.getDramaIds(self.updateDramaGroups)
|
let dramaIds = self.getDramaIds(self.updateDramaGroups)
|
||||||
//print("current ids: \(dramaIds)")
|
//print("current ids: \(dramaIds)")
|
||||||
var loadNum: Int = 0
|
var loadNum: Int = 0
|
||||||
if let lastId = dramaIds.last {
|
if let lastId = dramaIds.last {
|
||||||
self.isMoreLoading = true
|
|
||||||
let response = await API.loadMoreUpdateDramas(userId: userId, mode: .next, id: lastId, as: [UpdateDramaGroup].self)
|
let response = await API.loadMoreUpdateDramas(userId: userId, mode: .next, id: lastId, as: [UpdateDramaGroup].self)
|
||||||
if case let .result(groups) = response {
|
if case let .result(groups) = response {
|
||||||
if groups.count > 0 {
|
if groups.count > 0 {
|
||||||
@ -255,25 +246,18 @@ final class IndexModel {
|
|||||||
loadNum = groups.reduce(0) { acc, group in acc + group.items.count}
|
loadNum = groups.reduce(0) { acc, group in acc + group.items.count}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.isMoreLoading = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return loadNum
|
return loadNum
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadPrevUpdateDramasTask(userId: String, callback: (Bool) -> Void) async -> Int {
|
func loadPrevUpdateDramasTask(userId: String) async -> Int {
|
||||||
guard !self.isMoreLoading else {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// 按照id来判断不一定正确,需要借助其他值
|
// 按照id来判断不一定正确,需要借助其他值
|
||||||
let dramaIds = self.getDramaIds(self.updateDramaGroups)
|
let dramaIds = self.getDramaIds(self.updateDramaGroups)
|
||||||
|
|
||||||
var loadNum: Int = 0
|
var loadNum: Int = 0
|
||||||
// 查找最小的id
|
// 查找最小的id
|
||||||
if let firstId = dramaIds.first {
|
if let firstId = dramaIds.first {
|
||||||
self.isMoreLoading = true
|
|
||||||
let response = await API.loadMoreUpdateDramas(userId: userId, mode: .prev, id: firstId, as: [UpdateDramaGroup].self)
|
let response = await API.loadMoreUpdateDramas(userId: userId, mode: .prev, id: firstId, as: [UpdateDramaGroup].self)
|
||||||
if case let .result(groups) = response {
|
if case let .result(groups) = response {
|
||||||
if groups.count > 0 {
|
if groups.count > 0 {
|
||||||
@ -288,15 +272,8 @@ final class IndexModel {
|
|||||||
displayDramaGroups(self.updateDramaGroups, label: "after")
|
displayDramaGroups(self.updateDramaGroups, label: "after")
|
||||||
|
|
||||||
loadNum = groups.reduce(0, { acc, group in acc + group.items.count })
|
loadNum = groups.reduce(0, { acc, group in acc + group.items.count })
|
||||||
// 处理回调
|
|
||||||
callback(true)
|
|
||||||
} else {
|
|
||||||
callback(false)
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
callback(false)
|
|
||||||
}
|
}
|
||||||
self.isMoreLoading = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return loadNum
|
return loadNum
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user