idex的group视图拆分
This commit is contained in:
parent
449e1bd81e
commit
bda86f1c8a
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user