处理刷新逻辑

This commit is contained in:
anlicheng 2025-02-24 22:26:55 +08:00
parent c90d0e51d1
commit 66f9f81a4a
2 changed files with 93 additions and 18 deletions

View File

@ -67,6 +67,10 @@ final class IndexModel {
var showUpdateDramas: [UpdateDramaShowItem] var showUpdateDramas: [UpdateDramaShowItem]
var selectedDate: String var selectedDate: String
//
@ObservationIgnored
var updateDramaGroups: [UpdateDramaGroup] = []
init() { init() {
self.dramas = [] self.dramas = []
self.showUpdateDramas = [] self.showUpdateDramas = []
@ -82,20 +86,34 @@ final class IndexModel {
print(message) print(message)
case .result(let result): case .result(let result):
self.dramas = result.dramas self.dramas = result.dramas
self.showUpdateDramas = groupUpdateDramas(updateDramaGroups: result.update_dramas) self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: result.update_dramas)
self.updateDramaGroups = result.update_dramas
} }
} }
@MainActor @MainActor
func loadMoreUpdateDramas(userId: Int, mode: API.LoadMode, id: Int) async { func loadMoreUpdateDramas(userId: Int, mode: API.LoadMode) async {
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: id, as: [UpdateDramaGroup].self) let id: Int = 8030
if case let .result(groups) = response { // TODO id
let showItems = groupUpdateDramas(updateDramaGroups: groups) switch mode {
self.showUpdateDramas.append(contentsOf: showItems) case .prev:
// id
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: id, as: [UpdateDramaGroup].self)
if case let .result(groups) = response {
self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups)
}
case .next:
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: id, as: [UpdateDramaGroup].self)
if case let .result(groups) = response {
self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups)
}
} }
} }
private func groupUpdateDramas(updateDramaGroups: [UpdateDramaGroup]) -> [UpdateDramaShowItem] { //
private func transformGroupUpdateDramas(updateDramaGroups: [UpdateDramaGroup]) -> [UpdateDramaShowItem] {
var updateItems: [UpdateDramaShowItem] = [] var updateItems: [UpdateDramaShowItem] = []
updateDramaGroups.forEach { group in updateDramaGroups.forEach { group in
updateItems.append(.init(element: .group(group))) updateItems.append(.init(element: .group(group)))
@ -106,16 +124,53 @@ final class IndexModel {
return updateItems return updateItems
} }
// groups
private func preappendMergeDramaGroups(groups: [UpdateDramaGroup], mergeGroups: [UpdateDramaGroup]) -> [UpdateDramaGroup] {
var targetGroups = groups
for group in mergeGroups {
if let idx = targetGroups.firstIndex(where: { $0.group_id == group.group_id}) {
var newItems = group.items
newItems.append(contentsOf: targetGroups[idx].items)
targetGroups[idx] = UpdateDramaGroup(group_id: group.group_id, group_name: group.group_name, items: newItems)
} else {
targetGroups.insert(group, at: 0)
}
}
return targetGroups
}
private func appendMergeDramaGroups(groups: [UpdateDramaGroup], mergeGroups: [UpdateDramaGroup]) -> [UpdateDramaGroup] {
var targetGroups = groups
for group in mergeGroups {
if let idx = targetGroups.firstIndex(where: { $0.group_id == group.group_id}) {
var newItems = targetGroups[idx].items
newItems.append(contentsOf: group.items)
targetGroups[idx] = UpdateDramaGroup(group_id: group.group_id, group_name: group.group_name, items: newItems)
} else {
targetGroups.append(group)
}
}
return targetGroups
}
} }
struct IndexView: View { struct IndexView: View {
@Environment(\.modelContext) private var modelContext @Environment(\.modelContext) private var modelContext
//@Query private var items: [Item] //@Query private var items: [Item]
@Query private var userModel: [UserModel] //@Query private var userModel: [UserModel] = []
@State var indexModel = IndexModel() @State var indexModel = IndexModel()
@State var isLoading: Bool = false @State var isMoreLoading: Bool = false
//
@State var isPrevLoading: Bool = false
// //
@State private var selectGroupId: String = "" @State private var selectGroupId: String = ""
@State private var showDateNavPopover: Bool = false @State private var showDateNavPopover: Bool = false
@ -205,17 +260,17 @@ struct IndexView: View {
let frame = geometry.frame(in: .global) let frame = geometry.frame(in: .global)
let screenBounds = UIScreen.main.bounds let screenBounds = UIScreen.main.bounds
if screenBounds.height - frame.minY > 50 && !isLoading { if screenBounds.height - frame.minY > 50 && !isMoreLoading {
Task { Task {
self.isLoading = true self.isMoreLoading = true
await self.indexModel.loadMoreUpdateDramas(userId: 1, mode: .next, id: 1234) await self.indexModel.loadMoreUpdateDramas(userId: 1, mode: .next)
self.isLoading = false self.isMoreLoading = false
} }
} }
} }
}) })
if self.isLoading { if self.isMoreLoading {
ProgressView() ProgressView()
} }
} }
@ -224,12 +279,23 @@ struct IndexView: View {
print("new selected date: " + selectedDate) print("new selected date: " + selectedDate)
} }
} }
.refreshable {
guard !self.isPrevLoading else {
return
}
//
self.isPrevLoading = true
await self.indexModel.loadMoreUpdateDramas(userId: 1, mode: .prev)
self.isPrevLoading = false
}
} }
.frame(width: 370) .frame(width: 370)
.ignoresSafeArea(edges: .bottom) .ignoresSafeArea(edges: .bottom)
.task { .task {
//await self.indexModel.loadData() await self.indexModel.loadData()
print(userModel) //print(userModel)
} }
} }
@ -252,6 +318,15 @@ struct IndexView: View {
extension IndexView { extension IndexView {
// PreferenceKey
struct ScrollOffsetKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}
// //
struct DramaCellView: View { struct DramaCellView: View {
let dramaItem: IndexModel.DramaItem let dramaItem: IndexModel.DramaItem

View File

@ -12,7 +12,6 @@ import SwiftData
struct dimensionhubApp: App { struct dimensionhubApp: App {
var sharedModelContainer: ModelContainer = { var sharedModelContainer: ModelContainer = {
let schema = Schema([ let schema = Schema([
Item.self,
UserModel.self UserModel.self
]) ])
let modelConfiguration = ModelConfiguration( let modelConfiguration = ModelConfiguration(
@ -29,6 +28,7 @@ struct dimensionhubApp: App {
descriptor.fetchLimit = 1 descriptor.fetchLimit = 1
let users = try container.mainContext.fetch(descriptor) let users = try container.mainContext.fetch(descriptor)
if users.isEmpty { if users.isEmpty {
print("user is empty create user")
container.mainContext.insert(UserModel.defaultUser()) container.mainContext.insert(UserModel.defaultUser())
} }