-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchView.swift
66 lines (57 loc) · 2.32 KB
/
SearchView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// SearchView.swift
// SupHealth
//
// Created by Yahia Berrada on 6/19/20.
// Copyright © 2020 Supinfo. All rights reserved.
//
import SwiftUI
struct SearchView : View {
@State private var Countries = [DataModel]()
@State public var countryName: String
var body: some View {
NavigationView {
VStack {
Text("Country Search")
HStack {
TextField("Search...", text: $countryName)
.padding(10)
.font(Font.system(size: 15, weight: .medium, design: .serif))
.overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.blue, lineWidth: 1))
// if the country name is in Countries
if self.Countries.contains(where: { $0.Country == self.countryName }) {
// foreach the country
ForEach(0..<self.Countries.count) { item in
// if the country is the same that the text value
if self.Countries[item].Country == self.countryName {
NavigationLink(destination: DetailCountry(country: self.Countries[item])) {
Text("Go")
}.buttonStyle(PlainButtonStyle())
}
}
}
}.padding(20).navigationBarTitle("Search")
}
}.navigationViewStyle(StackNavigationViewStyle())
.onAppear(perform: loadData)
}
//api service
func loadData() {
guard let url = URL(string: api) else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let decodedResponse = try? JSONDecoder().decode(ResponseCountries.self, from: data) {
DispatchQueue.main.async {
self.Countries = decodedResponse.Countries
}
return
}
}
print("\(error?.localizedDescription ?? "Unknown error")")
}.resume()
}
}