diff --git a/dimensionhub/Core/API.swift b/dimensionhub/Core/API.swift index 081e71d..fac69c6 100644 --- a/dimensionhub/Core/API.swift +++ b/dimensionhub/Core/API.swift @@ -100,8 +100,8 @@ struct API { } print("request url: \(request.url!.absoluteString)") - let x = String(data: data, encoding: .utf8)! - print("url: \(request.url!.path()), data is: \(x)") +// let x = String(data: data, encoding: .utf8)! +// print("url: \(request.url!.path()), data is: \(x)") do { let result = try JSONDecoder().decode(APISuccessResponse.self, from: data) return .result(result.result) diff --git a/dimensionhub/Views/SearchView.swift b/dimensionhub/Views/SearchView.swift index 6d713f7..caa2d2d 100644 --- a/dimensionhub/Views/SearchView.swift +++ b/dimensionhub/Views/SearchView.swift @@ -11,36 +11,48 @@ import Observation struct SearchView: View { @AppStorage("userId") private var userId: String = Utils.defaultUserId() @Environment(\.dismiss) var dismiss - @State var model = SearchModel() @State var searchText: String = "" + // 请求的数据类型 + struct DramaGroup: Codable { + struct Item: Codable { + let id: Int + let name: String + let time: Int + let thumb: String + let status: String + } + + let group_id: String + let group_name: String + let items: [Item] + } + + @State var dramaGroups: [DramaGroup] = [] + var body: some View { VStack { - HStack(alignment: .center) { - Color.clear - .overlay { - HStack(alignment: .center, spacing: 0) { - Button(action: { dismiss() }) { - Image(systemName: "chevron.left") - .font(.system(size: 20, weight: .medium)) - .foregroundColor(.black) - } - - Spacer() - - SearchBar(searchText: $searchText, placeholder: "搜索...") { - Task { - let trimmedSearchText = searchText.trimmingCharacters(in: .whitespacesAndNewlines) - - if !trimmedSearchText.isEmpty { - UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) - - await model.search(userId: userId, name: trimmedSearchText) - } - } + HStack(alignment: .center, spacing: 0) { + 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 { + let dramaGroups = await self.search(userId: userId, name: trimmedSearchText) + DispatchQueue.main.async { + self.dramaGroups = dramaGroups } } } + } } .frame(height: 50) .padding([.top, .bottom], 8) @@ -48,7 +60,7 @@ struct SearchView: View { ScrollView(.vertical, showsIndicators: false) { // 基于日期的更新列表 LazyVStack(alignment: .center, spacing: 10) { - ForEach(model.dramaGroups, id: \.group_id) { group in + ForEach(dramaGroups, id: \.group_id) { group in DramaGroupView(group: group) } } @@ -60,16 +72,31 @@ struct SearchView: View { .navigationBarBackButtonHidden(true) .padding(8) .ignoresSafeArea(edges: .bottom) - .task { - await model.search(userId: userId, name: "第二季") + } + + // 搜素功能 + func search(userId: String, name: String) async -> [DramaGroup] { + guard let encodeName = name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { + return [] + } + + let response = await API.searchDrama(userId: userId, name: encodeName, as: [DramaGroup].self) + + switch response { + case .result(let dramaGroups): + return dramaGroups + case .error(let int32, let string): + print("error_code: \(int32), message: \(string)") + return [] } } + } extension SearchView { // 显示分组信息 struct DramaGroupView: View { - let group: SearchModel.DramaGroup + let group: DramaGroup var body: some View { VStack(alignment: .center, spacing: 10) { @@ -119,11 +146,6 @@ extension SearchView { } } -} - - -extension SearchView { - struct SearchBar: View { @State private var isSearching = false @@ -131,21 +153,26 @@ extension SearchView { var placeholder: String var onTap: () -> Void + @FocusState private var isFocused + var body: some View { // 自定义搜索栏 HStack(alignment: .center, spacing: 8) { + // 输入框 TextField(placeholder, text: $searchText) + .textFieldStyle(PlainTextFieldStyle()) + .keyboardType(.emailAddress) + .focused($isFocused) + .submitLabel(.search) + .onSubmit { + onTap() + } + .contentShape(Rectangle()) .padding(8) - //.padding(.horizontal, 30) .background(Color(.systemGray6)) .cornerRadius(8) .overlay(alignment: .trailing) { HStack { - // Image(systemName: "magnifyingglass") - // .foregroundColor(.gray) - // .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) - // .padding(.leading, 8) - if isSearching { Button(action: { searchText = "" @@ -156,68 +183,31 @@ extension SearchView { } } } - .border(Color.red) - } .frame(maxWidth: .infinity) - .onChange(of: searchText) { - if searchText.trimmingCharacters(in: .whitespaces).isEmpty { - isSearching = false - } else { - isSearching = true - } - } + // 搜索按钮 Button { onTap() - } label: { Text("搜索") .font(.system(size: 18)) .foregroundColor(.gray) } .padding(.trailing, 10) + .disabled(searchText.isEmpty) } - } - } -} - -@Observable -final class SearchModel { - - struct DramaGroup: Codable { - struct Item: Codable { - let id: Int - let name: String - let time: Int - let thumb: String - let status: String - } - - let group_id: String - let group_name: String - let items: [Item] - } - - var dramaGroups: [DramaGroup] = [] - - func search(userId: String, name: String) async { - guard let encodeName = name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { - self.dramaGroups = [] - return - } - - let response = await API.searchDrama(userId: userId, name: encodeName, as: [DramaGroup].self) - - switch response { - case .result(let dramaGroups): - await MainActor.run { - self.dramaGroups = dramaGroups + .onChange(of: searchText) { + if searchText.trimmingCharacters(in: .whitespaces).isEmpty { + isSearching = false + } else { + isSearching = true + } } - case .error(let int32, let string): - print("error_code: \(int32), message: \(string)") - await MainActor.run { - self.dramaGroups = [] + .onAppear { + DispatchQueue.main.async { + isFocused = true + } } } }