选择图片分享功能
This commit is contained in:
@@ -1,44 +1,133 @@
|
||||
import SwiftUI
|
||||
import Foundation
|
||||
|
||||
// 搜索历史记录管理器(保持不变)
|
||||
class SearchHistoryManager {
|
||||
static let shared = SearchHistoryManager()
|
||||
private let userDefaultsKey = "search_history"
|
||||
private let maxHistoryCount = 50
|
||||
|
||||
var history: [String] {
|
||||
UserDefaults.standard.array(forKey: userDefaultsKey) as? [String] ?? []
|
||||
}
|
||||
|
||||
func addHistory(_ query: String) {
|
||||
var newHistory = history
|
||||
if let index = newHistory.firstIndex(of: query) {
|
||||
newHistory.remove(at: index)
|
||||
}
|
||||
newHistory.insert(query, at: 0)
|
||||
if newHistory.count > maxHistoryCount {
|
||||
newHistory = Array(newHistory.prefix(maxHistoryCount))
|
||||
}
|
||||
UserDefaults.standard.set(newHistory, forKey: userDefaultsKey)
|
||||
}
|
||||
|
||||
func removeHistory(_ query: String) {
|
||||
var newHistory = history
|
||||
if let index = newHistory.firstIndex(of: query) {
|
||||
newHistory.remove(at: index)
|
||||
UserDefaults.standard.set(newHistory, forKey: userDefaultsKey)
|
||||
}
|
||||
}
|
||||
|
||||
func clearAllHistory() {
|
||||
UserDefaults.standard.removeObject(forKey: userDefaultsKey)
|
||||
}
|
||||
}
|
||||
|
||||
// 修改后的搜索视图(重点调整历史记录条目的交互)
|
||||
struct SearchView: View {
|
||||
@State private var searchQuery: String = ""
|
||||
@State private var searchResults: [AlbumItem] = []
|
||||
@State private var isSearching: Bool = false
|
||||
@State private var showAlert: Bool = false
|
||||
@State private var alertMessage: String = ""
|
||||
|
||||
@State private var searchHistory: [String] = []
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section(header: Text("搜索")) {
|
||||
HStack {
|
||||
TextField("输入搜索内容", text: $searchQuery)
|
||||
.textFieldStyle(RoundedBorderTextFieldStyle())
|
||||
|
||||
Button(action: performSearch) {
|
||||
if isSearching {
|
||||
ProgressView()
|
||||
} else {
|
||||
Image(systemName: "magnifyingglass")
|
||||
NavigationStack {
|
||||
Form {
|
||||
Section(header: Text("Jetson Media")) {
|
||||
HStack {
|
||||
TextField("输入搜索内容", text: $searchQuery)
|
||||
.textFieldStyle(RoundedBorderTextFieldStyle())
|
||||
.onChange(of: searchQuery) {
|
||||
if $0.isEmpty {
|
||||
loadHistory()
|
||||
}
|
||||
}
|
||||
|
||||
Button(action: performSearch) {
|
||||
if isSearching {
|
||||
ProgressView()
|
||||
} else {
|
||||
Image(systemName: "magnifyingglass")
|
||||
}
|
||||
}
|
||||
.disabled(searchQuery.isEmpty || isSearching)
|
||||
}
|
||||
.disabled(searchQuery.isEmpty || isSearching)
|
||||
}
|
||||
}
|
||||
|
||||
if !searchResults.isEmpty {
|
||||
Section(header: Text("搜索结果")) {
|
||||
ForEach(searchResults, id: \.album_id) { item in
|
||||
NavigationLink(destination: PhotoView(albumId: item.album_id)) {
|
||||
VStack(alignment: .leading) {
|
||||
Text(item.title)
|
||||
.font(.headline)
|
||||
Text("ID: \(item.album_id)")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.gray)
|
||||
|
||||
// 搜索历史区域(调整条目交互)
|
||||
if searchQuery.isEmpty && !searchHistory.isEmpty {
|
||||
Section(header: HStack {
|
||||
Text("搜索历史")
|
||||
Spacer()
|
||||
Button("清除全部") {
|
||||
SearchHistoryManager.shared.clearAllHistory()
|
||||
loadHistory()
|
||||
}
|
||||
.foregroundColor(.red)
|
||||
.font(.subheadline)
|
||||
}) {
|
||||
ForEach(searchHistory, id: \.self) { query in
|
||||
// 用HStack包裹,区分文本点击和删除按钮点击
|
||||
HStack {
|
||||
// 文本区域:仅触发搜索
|
||||
Text(query)
|
||||
.foregroundColor(.primary)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.onTapGesture {
|
||||
// 点击文本直接搜索
|
||||
searchQuery = query
|
||||
performSearch()
|
||||
}
|
||||
|
||||
// 删除按钮:仅触发删除
|
||||
Button(action: {
|
||||
SearchHistoryManager.shared.removeHistory(query)
|
||||
loadHistory()
|
||||
}) {
|
||||
Image(systemName: "xmark.circle.fill")
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
.buttonStyle(PlainButtonStyle()) // 避免按钮点击影响整个行
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索结果区域(保持不变)
|
||||
if !searchResults.isEmpty {
|
||||
Section(header: Text("搜索结果")) {
|
||||
ForEach(searchResults, id: \.album_id) { item in
|
||||
NavigationLink(destination: PhotoView(albumId: item.album_id)) {
|
||||
VStack(alignment: .leading) {
|
||||
Text(item.title)
|
||||
.font(.headline)
|
||||
Text("ID: \(item.album_id)")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("搜索")
|
||||
.onAppear {
|
||||
loadHistory()
|
||||
}
|
||||
}
|
||||
.alert(isPresented: $showAlert) {
|
||||
@@ -46,15 +135,23 @@ struct SearchView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func loadHistory() {
|
||||
searchHistory = SearchHistoryManager.shared.history
|
||||
}
|
||||
|
||||
private func performSearch() {
|
||||
guard !searchQuery.isEmpty else { return }
|
||||
|
||||
SearchHistoryManager.shared.addHistory(searchQuery)
|
||||
loadHistory()
|
||||
|
||||
isSearching = true
|
||||
searchResults = []
|
||||
|
||||
let encodedQuery = searchQuery.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? searchQuery
|
||||
let urlString = "https://jms.godserver.cn/search/?search_query=\(encodedQuery)&page=1"
|
||||
|
||||
let urlString = Config.shared.apiURL(
|
||||
path: "\(Config.Path.search)?search_query=\(encodedQuery)&page=1"
|
||||
)
|
||||
guard let url = URL(string: urlString) else {
|
||||
showAlert(message: "无效的URL")
|
||||
isSearching = false
|
||||
@@ -91,6 +188,7 @@ struct SearchView: View {
|
||||
}
|
||||
task.resume()
|
||||
}
|
||||
|
||||
private func showAlert(message: String) {
|
||||
alertMessage = message
|
||||
showAlert = true
|
||||
|
||||
Reference in New Issue
Block a user