Skip to content

Instantly share code, notes, and snippets.

@yonat
Created January 6, 2020 12:35
Show Gist options
  • Save yonat/e824402f85f909dfec54ddc3a0e46660 to your computer and use it in GitHub Desktop.
Save yonat/e824402f85f909dfec54ddc3a0e46660 to your computer and use it in GitHub Desktop.
//
// 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