diff --git a/dimensionhub/Views/IndexView.swift b/dimensionhub/Views/IndexView.swift index f14e62d..8a26c62 100644 --- a/dimensionhub/Views/IndexView.swift +++ b/dimensionhub/Views/IndexView.swift @@ -49,36 +49,19 @@ final class IndexModel { 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 { case success case error(String) } var dramas: [DramaItem] - var showUpdateDramas: [UpdateDramaShowItem] var selectedDate: String // 保存原始的更新数据 - @ObservationIgnored var updateDramaGroups: [UpdateDramaGroup] = [] init() { self.dramas = [] - self.showUpdateDramas = [] self.selectedDate = "" } @@ -90,7 +73,6 @@ final class IndexModel { print("index load data get error_code: \(code), message: \(message)") case .result(let result): self.dramas = result.dramas - self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: result.update_dramas) self.updateDramaGroups = result.update_dramas } } @@ -112,7 +94,6 @@ final class IndexModel { print("--------- before ------------") displayDramaGroups(self.updateDramaGroups) self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) - self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups) print("--------- after ------------") displayDramaGroups(self.updateDramaGroups) print("--------- ------------") @@ -133,7 +114,6 @@ final class IndexModel { print("--------- before ------------") displayDramaGroups(self.updateDramaGroups) self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) - self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups) print("----------after-----------") displayDramaGroups(self.updateDramaGroups) @@ -155,26 +135,12 @@ final class IndexModel { @MainActor func loadDateUpdateDramas(userId: String, date: String) async { self.updateDramaGroups.removeAll() - self.showUpdateDramas.removeAll() let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self) if case let .result(groups) = response { 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 private func preappendMergeDramaGroups(groups: [UpdateDramaGroup], mergeGroups: [UpdateDramaGroup]) -> [UpdateDramaGroup] { var targetGroups = groups @@ -231,9 +197,6 @@ struct IndexView: View { @Environment(\.modelContext) private var modelContext @AppStorage("userId") private var userId: String = Utils.defaultUserId() - //@Query private var items: [Item] - //@Query private var userModel: [UserModel] = [] - @State var indexModel = IndexModel() @State var isMoreLoading: Bool = false // 前向刷新 @@ -275,48 +238,11 @@ struct IndexView: View { // 基于日期的更新列表 ScrollView(.vertical, showsIndicators: false) { VStack(alignment: .center, spacing: 10) { - ForEach(indexModel.showUpdateDramas, id: \.id) { drama in - switch drama.element { - case .group(let group): - HStack { - Spacer() - 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) - } - } + ForEach(indexModel.updateDramaGroups, id: \.group_id) { group in + DramaGroupView(group: group) { + selectGroupId = group.group_id + indexModel.selectedDate = group.group_id + showDateNavPopover = true } } } @@ -330,7 +256,7 @@ struct IndexView: View { let frame = geometry.frame(in: .global) let screenBounds = UIScreen.main.bounds let contextFrame = geometry.frame(in: .named("scrollView")) - + if screenBounds.height - frame.minY > 50 && contextFrame.minY > 0 && !isMoreLoading { Task { self.isMoreLoading = true @@ -357,7 +283,6 @@ struct IndexView: View { .coordinateSpace(name: "scrollView") .popover(isPresented: $showDateNavPopover) { DateNavView(selectGroupId: self.$selectGroupId, showDateNavPopover: $showDateNavPopover) { selectedDate in - print("new selected date: " + selectedDate) Task { await indexModel.loadDateUpdateDramas(userId: self.userId, date: selectedDate) } @@ -387,7 +312,7 @@ struct IndexView: View { .frame(width: 370) .ignoresSafeArea(edges: .bottom) .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 { 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 {