idex的group视图拆分

This commit is contained in:
anlicheng 2025-02-25 22:29:08 +08:00
parent 449e1bd81e
commit bda86f1c8a

View File

@ -49,36 +49,19 @@ final class IndexModel {
let dramas: [DramaItem] let dramas: [DramaItem]
} }
struct UpdateDramaShowItem {
enum Element {
case group(UpdateDramaGroup)
case item(UpdateDramaGroup.Item)
}
let id = UUID().uuidString
let element: Element
init(element: Element) {
self.element = element
}
}
enum LoadMoreResult { enum LoadMoreResult {
case success case success
case error(String) case error(String)
} }
var dramas: [DramaItem] var dramas: [DramaItem]
var showUpdateDramas: [UpdateDramaShowItem]
var selectedDate: String var selectedDate: String
// //
@ObservationIgnored
var updateDramaGroups: [UpdateDramaGroup] = [] var updateDramaGroups: [UpdateDramaGroup] = []
init() { init() {
self.dramas = [] self.dramas = []
self.showUpdateDramas = []
self.selectedDate = "" self.selectedDate = ""
} }
@ -90,7 +73,6 @@ final class IndexModel {
print("index load data get error_code: \(code), message: \(message)") print("index load data get error_code: \(code), message: \(message)")
case .result(let result): case .result(let result):
self.dramas = result.dramas self.dramas = result.dramas
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: result.update_dramas)
self.updateDramaGroups = result.update_dramas self.updateDramaGroups = result.update_dramas
} }
} }
@ -112,7 +94,6 @@ final class IndexModel {
print("--------- before ------------") print("--------- before ------------")
displayDramaGroups(self.updateDramaGroups) displayDramaGroups(self.updateDramaGroups)
self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups)
print("--------- after ------------") print("--------- after ------------")
displayDramaGroups(self.updateDramaGroups) displayDramaGroups(self.updateDramaGroups)
print("--------- ------------") print("--------- ------------")
@ -133,7 +114,6 @@ final class IndexModel {
print("--------- before ------------") print("--------- before ------------")
displayDramaGroups(self.updateDramaGroups) displayDramaGroups(self.updateDramaGroups)
self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups)
print("----------after-----------") print("----------after-----------")
displayDramaGroups(self.updateDramaGroups) displayDramaGroups(self.updateDramaGroups)
@ -155,26 +135,12 @@ final class IndexModel {
@MainActor @MainActor
func loadDateUpdateDramas(userId: String, date: String) async { func loadDateUpdateDramas(userId: String, date: String) async {
self.updateDramaGroups.removeAll() self.updateDramaGroups.removeAll()
self.showUpdateDramas.removeAll()
let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self) let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self)
if case let .result(groups) = response { if case let .result(groups) = response {
self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups)
} }
} }
//
private func transformGroupUpdateDramas(updateDramaGroups: [UpdateDramaGroup]) -> [UpdateDramaShowItem] {
var updateItems: [UpdateDramaShowItem] = []
updateDramaGroups.forEach { group in
updateItems.append(.init(element: .group(group)))
group.items.forEach { item in
updateItems.append(.init(element: .item(item)))
}
}
return updateItems
}
// groups // groups
private func preappendMergeDramaGroups(groups: [UpdateDramaGroup], mergeGroups: [UpdateDramaGroup]) -> [UpdateDramaGroup] { private func preappendMergeDramaGroups(groups: [UpdateDramaGroup], mergeGroups: [UpdateDramaGroup]) -> [UpdateDramaGroup] {
var targetGroups = groups var targetGroups = groups
@ -231,9 +197,6 @@ struct IndexView: View {
@Environment(\.modelContext) private var modelContext @Environment(\.modelContext) private var modelContext
@AppStorage("userId") private var userId: String = Utils.defaultUserId() @AppStorage("userId") private var userId: String = Utils.defaultUserId()
//@Query private var items: [Item]
//@Query private var userModel: [UserModel] = []
@State var indexModel = IndexModel() @State var indexModel = IndexModel()
@State var isMoreLoading: Bool = false @State var isMoreLoading: Bool = false
// //
@ -275,48 +238,11 @@ struct IndexView: View {
// //
ScrollView(.vertical, showsIndicators: false) { ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .center, spacing: 10) { VStack(alignment: .center, spacing: 10) {
ForEach(indexModel.showUpdateDramas, id: \.id) { drama in ForEach(indexModel.updateDramaGroups, id: \.group_id) { group in
switch drama.element { DramaGroupView(group: group) {
case .group(let group): selectGroupId = group.group_id
HStack { indexModel.selectedDate = group.group_id
Spacer() showDateNavPopover = true
Text(group.group_name)
.font(.system(size: 18))
.fontWeight(.regular)
.onTapGesture {
selectGroupId = group.group_id
indexModel.selectedDate = group.group_id
showDateNavPopover = true
}
}
case .item(let item):
NavigationLink(destination: DetailView(id: item.id)) {
AsyncImage(url: URL(string: item.thumb)) { image in
image.resizable()
} placeholder: {
ProgressView()
}
.frame(width: 370, height: 180)
.overlay {
HStack {
VStack(alignment: .leading, spacing: 8) {
Text(item.name)
.font(.system(size: 16))
.foregroundColor(Color(hex: "#333333"))
.lineLimit(1)
Text(item.status)
.font(.system(size: 12))
.foregroundColor(Color(hex: "#333333"))
.lineLimit(1)
Spacer()
}
Spacer()
}
.padding([.top, .leading], 10)
}
}
} }
} }
} }
@ -330,7 +256,7 @@ 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
let contextFrame = geometry.frame(in: .named("scrollView")) let contextFrame = geometry.frame(in: .named("scrollView"))
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
@ -357,7 +283,6 @@ struct IndexView: View {
.coordinateSpace(name: "scrollView") .coordinateSpace(name: "scrollView")
.popover(isPresented: $showDateNavPopover) { .popover(isPresented: $showDateNavPopover) {
DateNavView(selectGroupId: self.$selectGroupId, showDateNavPopover: $showDateNavPopover) { selectedDate in DateNavView(selectGroupId: self.$selectGroupId, showDateNavPopover: $showDateNavPopover) { selectedDate in
print("new selected date: " + selectedDate)
Task { Task {
await indexModel.loadDateUpdateDramas(userId: self.userId, date: selectedDate) await indexModel.loadDateUpdateDramas(userId: self.userId, date: selectedDate)
} }
@ -387,7 +312,7 @@ struct IndexView: View {
.frame(width: 370) .frame(width: 370)
.ignoresSafeArea(edges: .bottom) .ignoresSafeArea(edges: .bottom)
.alert(isPresented: $showPrompt) { .alert(isPresented: $showPrompt) {
Alert(title: Text("提示"), message: Text(self.promptMessage), dismissButton: .default(Text("Ok"))) 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)
@ -454,6 +379,56 @@ extension IndexView {
} }
} }
//
struct DramaGroupView: View {
let group: IndexModel.UpdateDramaGroup
var onTap: () -> Void
var body: some View {
VStack(alignment: .center, spacing: 10) {
HStack {
Spacer()
Text(group.group_name)
.font(.system(size: 18))
.fontWeight(.regular)
.onTapGesture {
onTap()
}
}
ForEach(group.items, id: \.id) { item in
NavigationLink(destination: DetailView(id: item.id)) {
AsyncImage(url: URL(string: item.thumb)) { image in
image.resizable()
} placeholder: {
ProgressView()
}
.frame(width: 370, height: 180)
.overlay {
HStack {
VStack(alignment: .leading, spacing: 8) {
Text(item.name)
.font(.system(size: 16))
.foregroundColor(Color(hex: "#333333"))
.lineLimit(1)
Text(item.status)
.font(.system(size: 12))
.foregroundColor(Color(hex: "#333333"))
.lineLimit(1)
Spacer()
}
Spacer()
}
.padding([.top, .leading], 10)
}
}
}
}
}
}
} }
#Preview { #Preview {