add 搜索历史
This commit is contained in:
parent
b5f6cc9afe
commit
2b39a9e9d9
34
dimensionhub/Models/SearchHistory.swift
Normal file
34
dimensionhub/Models/SearchHistory.swift
Normal 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
|
||||||
|
// }
|
||||||
|
}
|
||||||
@ -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,6 +54,9 @@ struct SearchView: View {
|
|||||||
self.dramaGroups = dramaGroups
|
self.dramaGroups = dramaGroups
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// let historyModel = SearchHistory(keyword: trimmedSearchText, timestamp: Date())
|
||||||
|
// modelContext.insert(historyModel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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 {
|
||||||
|
|||||||
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user