add 搜索历史

This commit is contained in:
anlicheng 2025-04-02 16:29:27 +08:00
parent b5f6cc9afe
commit 2b39a9e9d9
3 changed files with 110 additions and 6 deletions

View File

@ -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<SearchHistory>(
// predicate: #Predicate { $0.keyword == self.keyword }
// )
// let count = try context.fetchCount(descriptor)
// if count > 0 {
// throw UniqueError.keywordTaken
// }
// }
//
// enum UniqueError: Error {
// case keywordTaken
// }
}

View File

@ -6,9 +6,12 @@
// //
import SwiftUI import SwiftUI
import Observation import SwiftData
struct SearchView: View { struct SearchView: View {
@Environment(\.modelContext) var modelContext
@State var showHistoryNum: Int = 2
@AppStorage("userId") private var userId: String = Utils.defaultUserId() @AppStorage("userId") private var userId: String = Utils.defaultUserId()
@Environment(\.dismiss) var dismiss @Environment(\.dismiss) var dismiss
@State var searchText: String = "" @State var searchText: String = ""
@ -51,12 +54,15 @@ struct SearchView: View {
self.dramaGroups = dramaGroups self.dramaGroups = dramaGroups
} }
} }
// let historyModel = SearchHistory(keyword: trimmedSearchText, timestamp: Date())
// modelContext.insert(historyModel)
} }
} }
} }
.frame(height: 50) .frame(height: 50)
.padding([.top, .bottom], 8) .padding([.top, .bottom], 8)
ScrollView(.vertical, showsIndicators: false) { ScrollView(.vertical, showsIndicators: false) {
// //
LazyVStack(alignment: .center, spacing: 10) { LazyVStack(alignment: .center, spacing: 10) {
@ -89,10 +95,10 @@ struct SearchView: View {
return [] return []
} }
} }
} }
extension SearchView { extension SearchView {
// //
struct DramaGroupView: View { struct DramaGroupView: View {
let group: DramaGroup let group: DramaGroup
@ -145,6 +151,7 @@ extension SearchView {
} }
} }
//
struct SearchBar: View { struct SearchBar: View {
@State private var isSearching = false @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 { #Preview {

View File

@ -12,11 +12,12 @@ import SwiftData
struct dimensionhubApp: App { struct dimensionhubApp: App {
var sharedModelContainer: ModelContainer = { var sharedModelContainer: ModelContainer = {
let schema = Schema([ let schema = Schema([
Item.self Item.self,
SearchHistory.self
]) ])
let modelConfiguration = ModelConfiguration( let modelConfiguration = ModelConfiguration(
schema: schema, schema: schema,
isStoredInMemoryOnly: false, isStoredInMemoryOnly: true,
allowsSave: true allowsSave: true
) )
@ -44,6 +45,6 @@ struct dimensionhubApp: App {
.tint(.black) .tint(.black)
} }
.modelContainer(sharedModelContainer) .modelContainer(sharedModelContainer)
} }
} }