选择图片分享功能

This commit is contained in:
2025-08-17 22:08:25 +08:00
parent e6afca5ba6
commit d80289fe73
19 changed files with 1402 additions and 110 deletions

View File

@@ -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