From 8240aa992761c656b35f4d02497cbde9d251228a Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 19 Feb 2025 01:04:16 +0800 Subject: [PATCH] add index view --- dimensionhub.xcodeproj/project.pbxproj | 2 + dimensionhub/ContentView.swift | 61 ------- dimensionhub/IndexView.swift | 242 +++++++++++++++++++++++++ dimensionhub/dimensionhubApp.swift | 2 +- 4 files changed, 245 insertions(+), 62 deletions(-) delete mode 100644 dimensionhub/ContentView.swift create mode 100644 dimensionhub/IndexView.swift diff --git a/dimensionhub.xcodeproj/project.pbxproj b/dimensionhub.xcodeproj/project.pbxproj index 638a2c6..32c704c 100644 --- a/dimensionhub.xcodeproj/project.pbxproj +++ b/dimensionhub.xcodeproj/project.pbxproj @@ -404,6 +404,7 @@ INFOPLIST_KEY_UILaunchScreen_Generation = YES; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + IPHONEOS_DEPLOYMENT_TARGET = 17.6; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -433,6 +434,7 @@ INFOPLIST_KEY_UILaunchScreen_Generation = YES; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + IPHONEOS_DEPLOYMENT_TARGET = 17.6; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", diff --git a/dimensionhub/ContentView.swift b/dimensionhub/ContentView.swift deleted file mode 100644 index 03a903d..0000000 --- a/dimensionhub/ContentView.swift +++ /dev/null @@ -1,61 +0,0 @@ -// -// ContentView.swift -// dimensionhub -// -// Created by 安礼成 on 2025/2/18. -// - -import SwiftUI -import SwiftData - -struct ContentView: View { - @Environment(\.modelContext) private var modelContext - @Query private var items: [Item] - - var body: some View { - NavigationSplitView { - List { - ForEach(items) { item in - NavigationLink { - Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))") - } label: { - Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)) - } - } - .onDelete(perform: deleteItems) - } - .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - EditButton() - } - ToolbarItem { - Button(action: addItem) { - Label("Add Item", systemImage: "plus") - } - } - } - } detail: { - Text("Select an item") - } - } - - private func addItem() { - withAnimation { - let newItem = Item(timestamp: Date()) - modelContext.insert(newItem) - } - } - - private func deleteItems(offsets: IndexSet) { - withAnimation { - for index in offsets { - modelContext.delete(items[index]) - } - } - } -} - -#Preview { - ContentView() - .modelContainer(for: Item.self, inMemory: true) -} diff --git a/dimensionhub/IndexView.swift b/dimensionhub/IndexView.swift new file mode 100644 index 0000000..987d289 --- /dev/null +++ b/dimensionhub/IndexView.swift @@ -0,0 +1,242 @@ +// +// ContentView.swift +// dimensionhub +// +// Created by 安礼成 on 2025/2/18. +// + +import SwiftUI +import SwiftData + +let url: String = "https://n.sinaimg.cn/sinacn10113/332/w1024h1708/20190806/73d1-iatixpm8624905.jpg" + +struct IndexView: View { + @Environment(\.modelContext) private var modelContext + @Query private var items: [Item] + + struct DramaItem: Codable { + struct Episode: Codable { + let id: Int32 + let name: String + let thumb: String + let numName: String + } + + let title: String + let episodes: [Episode] + } + + struct UpdateDramaItem: Codable { + let name: String + let thumb: String + let desc: String + let datetime: String + } + + @State var dramas: [DramaItem] = [ + .init(title: "夏目友人帐 陆", episodes: [ + .init(id: 1, name: "西村与北本", thumb: url, numName: "第6话"), + .init(id: 2, name: "西村与北本", thumb: url, numName: "第5话"), + .init(id: 3, name: "西村与北本", thumb: url, numName: "第4话"), + .init(id: 4, name: "西村与北本", thumb: url, numName: "第4话"), + .init(id: 5, name: "西村与北本", thumb: url, numName: "第4话"), + ]), + + .init(title: "吹响吧! 上低音号", episodes: [ + .init(id: 1, name: "欢迎进入高中", thumb: url, numName: "第6话"), + .init(id: 2, name: "拜托了上低音...", thumb: url, numName: "第5话"), + .init(id: 3, name: "吹奏乐部活动...", thumb: url, numName: "第4话"), + .init(id: 4, name: "吹奏乐部活动...", thumb: url, numName: "第4话"), + .init(id: 5, name: "吹奏乐部活动...", thumb: url, numName: "第4话"), + ]) + ] + + let updateDramas: [UpdateDramaItem] = [ + .init(name: "拜托了上", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上1", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上2", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上3", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上4", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上5", thumb: url, desc: "更新到第5话", datetime: "2017年5月番"), + .init(name: "拜托了上6", thumb: url, desc: "更新到第5话", datetime: "2017年5月番"), + ] + + enum UpdateDramaShowItem { + case lable(String) + case item(UpdateDramaItem) + } + + @State var showUpdateDramas: [UpdateDramaShowItem] = Self.yes(updateDramas: [ + .init(name: "拜托了上", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上1", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上2", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上3", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上4", thumb: url, desc: "更新到第5话", datetime: "2017年4月番"), + .init(name: "拜托了上5", thumb: url, desc: "更新到第5话", datetime: "2017年5月番"), + .init(name: "拜托了上6", thumb: url, desc: "更新到第5话", datetime: "2017年5月番"), + ]) + + var body: some View { + VStack(alignment: .center) { + HStack(alignment: .center) { + Spacer() + Text("亚次元") + .padding(.top, 5) + .padding(.bottom, 5) + Spacer() + } + .background(Color.red) + + HStack(alignment: .center) { + Spacer() + Text("番剧补完计划") + .font(.system(size: 24)) + } + + ForEach(dramas.indices, id: \.self) { id in + DramaCellView(dramaItem: dramas[id]) + } + + // 基于日期的更新列表 + ScrollView(.vertical, showsIndicators: false) { + LazyVStack { + ForEach(showUpdateDramas.indices, id: \.self) { index in + UpdateDramaCellView(showItem: showUpdateDramas[index]) + } + } + .background(GeometryReader { geometry in + Color.clear.onChange(of: geometry.frame(in: .global).minY) {_, offset in + let contentHeight = geometry.size.height + let scrollViewHeight = UIScreen.main.bounds.height + + // 判断是否滑动到底部 + if offset + contentHeight > scrollViewHeight - 50 { + print("call me here11") + } + } + + }) + } + } + .task { + let groups = Dictionary(grouping: self.updateDramas) {$0.datetime} + var updateItems: [UpdateDramaShowItem] = [] + groups.forEach { (key, items) in + updateItems.append(.lable(key)) + items.forEach { item in + updateItems.append(.item(item)) + } + } + + self.showUpdateDramas = updateItems + NSLog("call me heere 11") + } + + } + + private func addItem() { + withAnimation { + let newItem = Item(timestamp: Date()) + modelContext.insert(newItem) + } + } + + private func deleteItems(offsets: IndexSet) { + withAnimation { + for index in offsets { + modelContext.delete(items[index]) + } + } + } + + private static func yes(updateDramas: [UpdateDramaItem]) -> [UpdateDramaShowItem] { + let groups = Dictionary(grouping: updateDramas) {$0.datetime} + var updateItems: [UpdateDramaShowItem] = [] + groups.forEach { (key, items) in + updateItems.append(.lable(key)) + items.forEach { item in + updateItems.append(.item(item)) + } + } + return updateItems + } +} + +extension IndexView { + + // 显示剧集的列表信息 + struct DramaCellView: View { + let dramaItem: DramaItem + + var body: some View { + VStack(alignment: .leading) { + Text(dramaItem.title) + .font(.system(size: 20)) + + ScrollView(.horizontal, showsIndicators: false) { + HStack(alignment: .center, spacing: 10) { + ForEach(dramaItem.episodes, id: \.id) { item in + VStack(alignment: .center) { + AsyncImage(url: URL(string: item.thumb)) { image in + image.resizable() + } placeholder: { + ProgressView() + } + .frame(width: 80, height: 80) + .overlay { + Text(item.numName) + } + + Text(item.name) + .lineLimit(1) + } + .frame(width: 100, height: 120) + } + } + .border(Color.red) + } + } + } + } + +} + +extension IndexView { + + struct UpdateDramaCellView: View { + let showItem: UpdateDramaShowItem + + var body: some View { + + switch showItem { + case .lable(let name): + Text(name) + case .item(let item): + VStack(alignment: .center) { + AsyncImage(url: URL(string: item.thumb)) { image in + image.resizable() + } placeholder: { + ProgressView() + } + .frame(width: 80, height: 80) + .overlay { + VStack(alignment: .leading) { + Text(item.name) + .lineLimit(1) + + Text(item.desc) + .lineLimit(1) + } + } + } + .frame(width: 100, height: 120) + } + } + } + +} + +#Preview { + IndexView() + .modelContainer(for: Item.self, inMemory: true) +} diff --git a/dimensionhub/dimensionhubApp.swift b/dimensionhub/dimensionhubApp.swift index cf7f02e..0d1f91a 100644 --- a/dimensionhub/dimensionhubApp.swift +++ b/dimensionhub/dimensionhubApp.swift @@ -25,7 +25,7 @@ struct dimensionhubApp: App { var body: some Scene { WindowGroup { - ContentView() + IndexView() } .modelContainer(sharedModelContainer) }