Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// ContentView.swift
// Stacketude
//
// Created by Yonat Sharon on 06/01/2020.
// Copyright © 2020 Yonat Sharon. All rights reserved.
//
import SwiftUI
struct AirportModel: Identifiable {
var id = UUID()
var aptICAO: String
}
class DataManager {
typealias exit = (([AirportModel]) -> ())
var airportVector: [AirportModel] = [
AirportModel(aptICAO: "First"),
AirportModel(aptICAO: "Second"),
AirportModel(aptICAO: "Third"),
AirportModel(aptICAO: "Fourth"),
]
func filter (valoreSearched: String, arrayToSearch: [AirportModel], closure: @escaping exit) {
guard !valoreSearched.isEmpty else {
closure(arrayToSearch)
return
}
DispatchQueue.global().async {
let aeroportoFiltrato = arrayToSearch.filter { $0.aptICAO.localizedCaseInsensitiveContains(valoreSearched) }
closure(aeroportoFiltrato)
}
}
}
struct ContentView: View {
var dm: DataManager
@State var searchTerm = ""
@State var filteredAirports: [AirportModel] = []
var body: some View {
VStack {
TextField("Search", text: $searchTerm, onCommit: updateSearchResults)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
List {
ForEach(filteredAirports) { valore in
Text(valore.aptICAO)
}
}
}
.onAppear(perform: updateSearchResults)
}
func updateSearchResults() {
dm.filter(valoreSearched: searchTerm, arrayToSearch: dm.airportVector) { airports in
DispatchQueue.main.async {
self.filteredAirports = airports
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(dm: DataManager())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.