From 1d12ff562583f13fb21f15d7a652f08a1e10f18e Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Sun, 8 Jun 2025 00:04:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=90=9C=E7=B4=A2=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E9=94=AE=E7=9B=98=E5=94=A4=E8=B5=B7=E7=9A=84=E5=8D=A1?= =?UTF-8?q?=E9=A1=BF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xcschemes/xcschememanagement.plist | 18 +++ dimensionhub/Views/Search/SearchBar.swift | 80 ---------- .../Views/Search/SearchDramaGroupView.swift | 47 ------ dimensionhub/Views/Search/SearchView.swift | 141 ++++++++++++++---- 4 files changed, 128 insertions(+), 158 deletions(-) delete mode 100644 dimensionhub/Views/Search/SearchBar.swift delete mode 100644 dimensionhub/Views/Search/SearchDramaGroupView.swift diff --git a/dimensionhub.xcodeproj/xcuserdata/anlicheng.xcuserdatad/xcschemes/xcschememanagement.plist b/dimensionhub.xcodeproj/xcuserdata/anlicheng.xcuserdatad/xcschemes/xcschememanagement.plist index d3561a5..7c1ff6b 100644 --- a/dimensionhub.xcodeproj/xcuserdata/anlicheng.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/dimensionhub.xcodeproj/xcuserdata/anlicheng.xcuserdatad/xcschemes/xcschememanagement.plist @@ -10,5 +10,23 @@ 0 + SuppressBuildableAutocreation + + C85F58BF2D64D10F00D761E9 + + primary + + + C85F58D12D64D11000D761E9 + + primary + + + C85F58DB2D64D11000D761E9 + + primary + + + diff --git a/dimensionhub/Views/Search/SearchBar.swift b/dimensionhub/Views/Search/SearchBar.swift deleted file mode 100644 index 429cb7a..0000000 --- a/dimensionhub/Views/Search/SearchBar.swift +++ /dev/null @@ -1,80 +0,0 @@ -// -// SearchBar.swift -// dimensionhub -// -// Created by 安礼成 on 2025/4/8. -// - -import SwiftUI - -// 搜索框 -struct SearchBar: View { - @State private var isSearching = false - - @Binding var searchText: String - var placeholder: String - var onSearch: () -> Void - - @FocusState private var isFocused: Bool - - var body: some View { - // 自定义搜索栏 - HStack(alignment: .center, spacing: 8) { - // 输入框 - TextField(placeholder, text: $searchText) - .textFieldStyle(PlainTextFieldStyle()) - .keyboardType(.default) - .focused($isFocused) - .submitLabel(.search) - .onSubmit { - onSearch() - } - .contentShape(Rectangle()) - .padding(8) - .background(Color(.systemGray6)) - .cornerRadius(8) - .overlay(alignment: .trailing) { - HStack { - if isSearching { - Button(action: { - searchText = "" - }) { - Image(systemName: "multiply.circle.fill") - .foregroundColor(.gray) - .padding(.trailing, 8) - } - } - } - } - .frame(maxWidth: .infinity) - .onTapGesture { - isFocused = true - } - - // 搜索按钮 - Button { - if !searchText.isEmpty { - onSearch() - } - } label: { - Text("搜索") - .font(.system(size: 18)) - .foregroundColor(.gray) - } - .padding(.trailing, 10) - .disabled(searchText.isEmpty) - } - .onChange(of: searchText) { - isSearching = !searchText.trimmingCharacters(in: .whitespaces).isEmpty - } - .onAppear { - DispatchQueue.main.async { - isFocused = true - } - } - } -} - -//#Preview { -// SearchBar() -//} diff --git a/dimensionhub/Views/Search/SearchDramaGroupView.swift b/dimensionhub/Views/Search/SearchDramaGroupView.swift deleted file mode 100644 index 1ac0ad2..0000000 --- a/dimensionhub/Views/Search/SearchDramaGroupView.swift +++ /dev/null @@ -1,47 +0,0 @@ -// -// SearchDramaGroupView.swift -// dimensionhub -// -// Created by 安礼成 on 2025/4/8. -// - -import SwiftUI - -// 显示分组信息 -struct SearchDramaGroupView: View { - let group: SearchModel.DramaGroup - - var body: some View { - LazyVStack(alignment: .center, spacing: 10) { - ForEach(group.items, id: \.id) { item in - NavigationLink(destination: DetailView(id: item.id)) { - FlexImage(urlString: item.thumb, width: 370, height: 180, placeholder: "ph_img_big") - .frame(width: 370, height: 180) - .overlay(alignment: .topLeading) { - VStack(alignment: .leading, spacing: 8) { - Text(item.name) - .font(.system(size: 16)) - .foregroundColor(.white) - .lineLimit(1) - - Text(item.status) - .font(.system(size: 12)) - .foregroundColor(.white) - .lineLimit(1) - } - .padding(5) - .background( - Color.black.opacity(0.6) - ) - .cornerRadius(5) - .padding(8) - } - } - } - } - } -} - -//#Preview { -// SearchDramaGroupView() -//} diff --git a/dimensionhub/Views/Search/SearchView.swift b/dimensionhub/Views/Search/SearchView.swift index cfb611c..f164411 100644 --- a/dimensionhub/Views/Search/SearchView.swift +++ b/dimensionhub/Views/Search/SearchView.swift @@ -10,67 +10,146 @@ import SwiftData struct SearchView: View { @Environment(\.modelContext) var modelContext - @State var showHistoryNum: Int = 2 - @AppStorage("userId") private var userId: String = Utils.defaultUserId() + @Environment(\.dismiss) var dismiss @State var searchText: String = "" - @State var searchModel = SearchModel() + @FocusState private var isFocused: Bool + @State private var isSearching = false + var body: some View { - VStack { - HStack(alignment: .center, spacing: 0) { + VStack(spacing: 12) { + HStack(spacing: 12) { Button(action: { dismiss() }) { Image(systemName: "chevron.left") .font(.system(size: 20, weight: .medium)) .foregroundColor(.black) } - Spacer() - - SearchBar(searchText: $searchText, placeholder: "搜索...") { - let trimmedSearchText = searchText.trimmingCharacters(in: .whitespacesAndNewlines) - if !trimmedSearchText.isEmpty { - UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) - Task.detached { - await searchModel.search(userId: userId, name: trimmedSearchText) + HStack(spacing: 8) { + TextField("搜索...", text: $searchText) + .focused($isFocused) + .textFieldStyle(PlainTextFieldStyle()) + .padding(8) + .background(Color(.systemGray6)) + .cornerRadius(8) + .submitLabel(.search) + .textInputAutocapitalization(.never) + .disableAutocorrection(true) + .keyboardType(.default) + .onSubmit { + doSearch() } - -// let historyModel = SearchHistory(keyword: trimmedSearchText, timestamp: Date()) -// modelContext.insert(historyModel) + .overlay(alignment: .trailing) { + Button(action: { + searchText = "" + }) { + Image(systemName: "multiply.circle.fill") + .foregroundColor(.gray) + .padding(.trailing, 8) + } + .opacity(isSearching ? 1 : 0) + .disabled(!isSearching) + } + + Button { + if !searchText.isEmpty { + doSearch() + isFocused = false // 关闭键盘 + } + } label: { + Text("搜索") + .font(.system(size: 18)) + .foregroundColor(.gray) } + .padding(.trailing, 10) + .disabled(searchText.isEmpty) + } + .onChange(of: searchText) { + isSearching = !searchText.trimmingCharacters(in: .whitespaces).isEmpty } } .frame(height: 50) - .padding([.top, .bottom], 8) - - if searchModel.dramaGroups.count > 0 { + .padding(.top, 12) + + if searchModel.dramaGroups.isEmpty { + Spacer() + Text("什么都没有找到") + .font(.system(size: 18)) + .foregroundColor(Color(hex: "#333333")) + Spacer() + } else { ScrollView(.vertical, showsIndicators: false) { - // 基于日期的更新列表 LazyVStack(alignment: .center, spacing: 10) { ForEach(searchModel.dramaGroups, id: \.group_id) { group in SearchDramaGroupView(group: group) } } + .padding(.top, 8) } - } else { - Spacer() - - Text("什么都没有找到") - .font(.system(size: 18)) - .foregroundColor(Color(hex: "#333333")) - .frame(alignment: .center) } - - Spacer() } .navigationTitle("") .navigationBarBackButtonHidden(true) - .padding(8) - .ignoresSafeArea(edges: .bottom) + .padding(.horizontal, 12) + .ignoresSafeArea(.keyboard, edges: .bottom) // 避免键盘遮挡 + .onAppear { + // 避免过早触发系统输入错误 + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { + isFocused = true + } + } } + + private func doSearch() { + let trimmed = searchText.trimmingCharacters(in: .whitespacesAndNewlines) + if !trimmed.isEmpty { + Task.detached { + await searchModel.search(userId: userId, name: trimmed) + } + // 可选:添加历史记录 + // let history = SearchHistory(keyword: trimmed, timestamp: Date()) + // modelContext.insert(history) + } + } + +} +// 显示分组信息 +struct SearchDramaGroupView: View { + let group: SearchModel.DramaGroup + + var body: some View { + LazyVStack(alignment: .center, spacing: 10) { + ForEach(group.items, id: \.id) { item in + NavigationLink(destination: DetailView(id: item.id)) { + FlexImage(urlString: item.thumb, width: 370, height: 180, placeholder: "ph_img_big") + .frame(width: 370, height: 180) + .overlay(alignment: .topLeading) { + VStack(alignment: .leading, spacing: 8) { + Text(item.name) + .font(.system(size: 16)) + .foregroundColor(.white) + .lineLimit(1) + + Text(item.status) + .font(.system(size: 12)) + .foregroundColor(.white) + .lineLimit(1) + } + .padding(5) + .background( + Color.black.opacity(0.6) + ) + .cornerRadius(5) + .padding(8) + } + } + } + } + } } extension SearchView {