fix 数据的加载逻辑

This commit is contained in:
anlicheng 2025-07-30 12:23:20 +08:00
parent ebdeb2f0ac
commit ae1ff01f88
2 changed files with 15 additions and 35 deletions

View File

@ -30,9 +30,7 @@ struct IndexMainView: View {
//
@State private var headerRefreshing: Bool = false
@State private var footerRefreshing: Bool = false
@State private var noMore: Bool = false
@State private var scrollOffset: CGFloat = 0
@State private var showFloatGroupLabel: Bool = false
@ -132,11 +130,11 @@ struct IndexMainView: View {
//
self.headerRefreshing = true
Task { @MainActor in
let loadNum = await self.indexModel.loadPrevUpdateDramasTask(userId: self.userId) { _ in
DispatchQueue.main.async {
defer {
self.headerRefreshing = false
}
}
let loadNum = await self.indexModel.loadPrevUpdateDramasTask(userId: self.userId)
self.hasMoreNewest = loadNum > 3
}
}
@ -178,13 +176,18 @@ struct IndexMainView: View {
return
}
Task { @MainActor in
let num = await indexModel.loadMoreUpdateDramasTask(userId: self.userId)
if num > 3 {
self.hasMoreOldest = true
} else {
self.hasMoreOldest = false
guard !self.showDateNavPopover && !self.footerRefreshing else {
return
}
self.footerRefreshing = true
Task { @MainActor in
defer {
self.footerRefreshing = false
}
let num = await indexModel.loadMoreUpdateDramasTask(userId: self.userId)
self.hasMoreOldest = num > 3
}
}
.onAppear {

View File

@ -91,10 +91,6 @@ final class IndexModel {
@ObservationIgnored
private var isLoaded = false
//
@ObservationIgnored
private var isMoreLoading = false
// debug
@ObservationIgnored
private var debug: Bool = false
@ -229,16 +225,11 @@ final class IndexModel {
}
func loadMoreUpdateDramasTask(userId: String) async -> Int {
guard !self.isMoreLoading else {
return 0
}
// id
let dramaIds = self.getDramaIds(self.updateDramaGroups)
//print("current ids: \(dramaIds)")
var loadNum: Int = 0
if let lastId = dramaIds.last {
self.isMoreLoading = true
let response = await API.loadMoreUpdateDramas(userId: userId, mode: .next, id: lastId, as: [UpdateDramaGroup].self)
if case let .result(groups) = response {
if groups.count > 0 {
@ -255,25 +246,18 @@ final class IndexModel {
loadNum = groups.reduce(0) { acc, group in acc + group.items.count}
}
}
self.isMoreLoading = false
}
return loadNum
}
func loadPrevUpdateDramasTask(userId: String, callback: (Bool) -> Void) async -> Int {
guard !self.isMoreLoading else {
return 0
}
func loadPrevUpdateDramasTask(userId: String) async -> Int {
// id
let dramaIds = self.getDramaIds(self.updateDramaGroups)
var loadNum: Int = 0
// id
if let firstId = dramaIds.first {
self.isMoreLoading = true
let response = await API.loadMoreUpdateDramas(userId: userId, mode: .prev, id: firstId, as: [UpdateDramaGroup].self)
if case let .result(groups) = response {
if groups.count > 0 {
@ -288,15 +272,8 @@ final class IndexModel {
displayDramaGroups(self.updateDramaGroups, label: "after")
loadNum = groups.reduce(0, { acc, group in acc + group.items.count })
//
callback(true)
} else {
callback(false)
}
} else {
callback(false)
}
self.isMoreLoading = false
}
return loadNum