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 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 {

View File

@ -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