From 2b39a9e9d9b92ddf75e0aa56b66ce318e795a5c7 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 2 Apr 2025 16:29:27 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=90=9C=E7=B4=A2=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dimensionhub/Models/SearchHistory.swift | 34 +++++++++++ dimensionhub/Views/SearchView.swift | 75 ++++++++++++++++++++++++- dimensionhub/dimensionhubApp.swift | 7 ++- 3 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 dimensionhub/Models/SearchHistory.swift diff --git a/dimensionhub/Models/SearchHistory.swift b/dimensionhub/Models/SearchHistory.swift new file mode 100644 index 0000000..9d4e72e --- /dev/null +++ b/dimensionhub/Models/SearchHistory.swift @@ -0,0 +1,34 @@ +// +// File.swift +// dimensionhub +// +// Created by 安礼成 on 2025/4/2. +// + +import Foundation +import SwiftData + +@Model +final class SearchHistory { + @Attribute(.unique) var keyword: String + var timestamp: Date + + init(keyword: String, timestamp: Date) { + self.keyword = keyword + self.timestamp = timestamp + } + +// func validateUniqueUsername(context: ModelContext) throws { +// let descriptor = FetchDescriptor( +// predicate: #Predicate { $0.keyword == self.keyword } +// ) +// let count = try context.fetchCount(descriptor) +// if count > 0 { +// throw UniqueError.keywordTaken +// } +// } +// +// enum UniqueError: Error { +// case keywordTaken +// } +} diff --git a/dimensionhub/Views/SearchView.swift b/dimensionhub/Views/SearchView.swift index b8696df..914240e 100644 --- a/dimensionhub/Views/SearchView.swift +++ b/dimensionhub/Views/SearchView.swift @@ -6,9 +6,12 @@ // import SwiftUI -import Observation +import SwiftData struct SearchView: View { + @Environment(\.modelContext) var modelContext + @State var showHistoryNum: Int = 2 + @AppStorage("userId") private var userId: String = Utils.defaultUserId() @Environment(\.dismiss) var dismiss @State var searchText: String = "" @@ -51,12 +54,15 @@ struct SearchView: View { self.dramaGroups = dramaGroups } } + +// let historyModel = SearchHistory(keyword: trimmedSearchText, timestamp: Date()) +// modelContext.insert(historyModel) } } } .frame(height: 50) .padding([.top, .bottom], 8) - + ScrollView(.vertical, showsIndicators: false) { // 基于日期的更新列表 LazyVStack(alignment: .center, spacing: 10) { @@ -89,10 +95,10 @@ struct SearchView: View { return [] } } - } extension SearchView { + // 显示分组信息 struct DramaGroupView: View { let group: DramaGroup @@ -145,6 +151,7 @@ extension SearchView { } } + // 搜索框 struct SearchBar: View { @State private var isSearching = false @@ -210,6 +217,68 @@ extension SearchView { } } } + + + struct SearchHistoryView1: View { + @Query(sort: \SearchHistory.timestamp, order: .reverse) private var historyItems: [SearchHistory] + + var body: some View { + VStack { + if historyItems.count > 5 { + ForEach(Array(0..<5), id: \.self) { idx in + SearchHistoryItemView(history: historyItems[idx]) { + print("click me") + } + } + + Button { + //self.showHistoryNum = 5 + } label: { + Text("查看更多历史") + .font(.system(size: 16)) + .foregroundColor(.gray) + } + .buttonStyle(.plain) + } else { + ForEach(historyItems) { history in + SearchHistoryItemView(history: history) { + print("click me") + } + } + } + } + } + } + + // 搜索历史 + struct SearchHistoryItemView: View { + let history: SearchHistory + var onClick: () -> Void + + var body: some View { + HStack(alignment: .center, spacing: 20) { + Image("lost_network") + .resizable() + .clipShape(Circle()) + .clipped() + .frame(width: 25, height: 25) + + Text(history.keyword) + .font(.system(size: 16)) + .lineLimit(1) + .frame(width: 280, alignment: .leading) + + Button(action: { + onClick() + }) { + Image(systemName: "xmark") + .font(.system(size: 14)) + .foregroundColor(.gray) + } + } + } + } + } #Preview { diff --git a/dimensionhub/dimensionhubApp.swift b/dimensionhub/dimensionhubApp.swift index 1d1b4b8..b058fde 100644 --- a/dimensionhub/dimensionhubApp.swift +++ b/dimensionhub/dimensionhubApp.swift @@ -12,11 +12,12 @@ import SwiftData struct dimensionhubApp: App { var sharedModelContainer: ModelContainer = { let schema = Schema([ - Item.self + Item.self, + SearchHistory.self ]) let modelConfiguration = ModelConfiguration( schema: schema, - isStoredInMemoryOnly: false, + isStoredInMemoryOnly: true, allowsSave: true ) @@ -44,6 +45,6 @@ struct dimensionhubApp: App { .tint(.black) } .modelContainer(sharedModelContainer) - } + }