В настоящее время мой SearchView
использует простой фильтр в массиве names
, и он нужен мне для поиска в моем файле JSON. У меня есть JSON файл с этой структурой:
struct UcmData: Codable, Identifiable {
let id: Int
let building: [Building]
}
// MARK: - Building
struct Building: Codable, Identifiable {
let id: Int
let title, subtitle, info, image: String
let floor: [Floor]
}
// MARK: - Floor
struct Floor: Codable, Identifiable {
let id, number: Int
let title, subtitle, image: String
let cabinet: [Cabinet]?
}
// MARK: - Cabinet
struct Cabinet: Codable, Identifiable {
let id: Int
let number: String
let person: [Person]
}
// MARK: - Person
struct Person: Codable, Identifiable {
let id: Int
let name: String
}
SearchView:
struct SearchView: View {
let names = ["306 B", "doc. Ing. Michal Čerňanský, PhD."]
let ucmData = Bundle.main.decode(UcmData.self, from: "ucm_data.json")
@State private var searchText = ""
@State private var showCancelButton: Bool = false
var body: some View {
VStack {
HStack {
HStack {
Image(systemName: "magnifyingglass")
TextField("Zadajte text pre vyhľadávanie", text: $searchText, onEditingChanged: { isEditing in
self.showCancelButton = true
}, onCommit: {
print("onCommit")
}).foregroundColor(.primary)
Button(action: {
self.searchText = ""
}) {
Image(systemName: "xmark.circle.fill").opacity(searchText == "" ? 0 : 1)
}
}
.padding(EdgeInsets(top: 8, leading: 6, bottom: 8, trailing: 6))
.foregroundColor(.secondary)
.background(Color(.secondarySystemBackground))
.cornerRadius(10.0)
if showCancelButton {
Button("Cancel") {
UIApplication.shared.endEditing(true)
self.searchText = ""
self.showCancelButton = false
}
.foregroundColor(Color(.systemBlue))
}
}
.padding(.horizontal)
.navigationBarHidden(showCancelButton)
List {
ForEach(self.names.filter{
self.searchText.isEmpty ? $0.localizedStandardContains("") :
$0.localizedStandardContains(self.searchText)
}, id: \.self) { name in
Text(name)
}
}
.navigationBarTitle(Text("Vyhľadávanie"))
.resignKeyboardOnDragGesture()
}
}
}
extension UIApplication {
func endEditing(_ force: Bool) {
self.windows
.filter{$0.isKeyWindow}
.first?
.endEditing(force)
}
}
struct ResignKeyboardOnDragGesture: ViewModifier {
var gesture = DragGesture().onChanged{_ in
UIApplication.shared.endEditing(true)
}
func body(content: Content) -> some View {
content.gesture(gesture)
}
}
extension View {
func resignKeyboardOnDragGesture() -> some View {
return modifier(ResignKeyboardOnDragGesture())
}
}
struct SearchView_Previews: PreviewProvider {
static var previews: some View {
Group {
SearchView()
.environment(\.colorScheme, .light)
SearchView()
.environment(\.colorScheme, .dark)
}
}
}
Как мне сделать соответствие searchText
с Cabinet.number
или Person.name
и списком соответствие элементов их пути в файле JSON, например, "building.title> floor.title> cab inet .number" или для персоны "building.title> floor.title> cab inet .number> person.name" ? Спасибо за предложения.