u1
This commit is contained in:
@@ -110,7 +110,7 @@ struct PhotoView: View {
|
||||
if totalImages > 0, currentImageIndex > 0 {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
withAnimation {
|
||||
proxy.scrollTo(currentImageIndex, anchor: .top)
|
||||
proxy.scrollTo(savedIndex, anchor: .top)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ class SearchHistoryManager {
|
||||
}
|
||||
}
|
||||
|
||||
// 修改后的搜索视图(重点调整历史记录条目的交互)
|
||||
// 修改后的搜索视图(实现数字ID直接导航)
|
||||
struct SearchView: View {
|
||||
@State private var searchQuery: String = ""
|
||||
@State private var searchResults: [AlbumItem] = []
|
||||
@@ -44,13 +44,16 @@ struct SearchView: View {
|
||||
@State private var showAlert: Bool = false
|
||||
@State private var alertMessage: String = ""
|
||||
@State private var searchHistory: [String] = []
|
||||
// 添加导航路径状态,用于编程式导航
|
||||
@State private var navigationPath = NavigationPath()
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
// 使用带路径的NavigationStack以支持编程式导航
|
||||
NavigationStack(path: $navigationPath) {
|
||||
Form {
|
||||
Section(header: Text("Jetson Media")) {
|
||||
HStack {
|
||||
TextField("输入搜索内容", text: $searchQuery)
|
||||
TextField("输入搜索内容或ID", text: $searchQuery)
|
||||
.textFieldStyle(RoundedBorderTextFieldStyle())
|
||||
.onChange(of: searchQuery) {
|
||||
if $0.isEmpty {
|
||||
@@ -69,7 +72,7 @@ struct SearchView: View {
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索历史区域(调整条目交互)
|
||||
// 搜索历史区域(保持不变)
|
||||
if searchQuery.isEmpty && !searchHistory.isEmpty {
|
||||
Section(header: HStack {
|
||||
Text("搜索历史")
|
||||
@@ -82,19 +85,15 @@ struct SearchView: View {
|
||||
.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()
|
||||
@@ -102,7 +101,7 @@ struct SearchView: View {
|
||||
Image(systemName: "xmark.circle.fill")
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
.buttonStyle(PlainButtonStyle()) // 避免按钮点击影响整个行
|
||||
.buttonStyle(PlainButtonStyle())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,6 +128,10 @@ struct SearchView: View {
|
||||
.onAppear {
|
||||
loadHistory()
|
||||
}
|
||||
// 定义导航目的地
|
||||
.navigationDestination(for: Int.self) { albumId in
|
||||
PhotoView(albumId: String(albumId))
|
||||
}
|
||||
}
|
||||
.alert(isPresented: $showAlert) {
|
||||
Alert(title: Text("提示"), message: Text(alertMessage), dismissButton: .default(Text("确定")))
|
||||
@@ -142,6 +145,16 @@ struct SearchView: View {
|
||||
private func performSearch() {
|
||||
guard !searchQuery.isEmpty else { return }
|
||||
|
||||
// 首先检查搜索内容是否为数字
|
||||
if let albumId = Int(searchQuery) {
|
||||
// 如果是数字,直接导航到对应的ID页面
|
||||
SearchHistoryManager.shared.addHistory(searchQuery)
|
||||
loadHistory()
|
||||
navigationPath.append(albumId)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果不是数字,执行正常搜索流程
|
||||
SearchHistoryManager.shared.addHistory(searchQuery)
|
||||
loadHistory()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user