-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
74 lines (58 loc) · 2.34 KB
/
ViewController.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
67
68
69
70
71
72
73
//
// ViewController.swift
// Day59 - Milestone - Projects 13-14-15
//
// Created by Jean-Yves Garcin on 22/06/2023.
//
import UIKit
class ViewController: UITableViewController {
var countries = [Country]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Country Facts"
DispatchQueue.global(qos: .userInitiated).async {
[weak self] in
if let datafile = Bundle.main.url(forResource: "countries", withExtension: "json") {
if let data = try? Data(contentsOf: datafile) {
self?.parse(json: data)
return
}
}
self?.showError()
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countries.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath)
let country = countries[indexPath.row]
cell.textLabel?.text = country.name
//cell.detailTextLabel?.text = country.body
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "sb_Detail") as? DetailTableViewController {
vc.country = countries[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
}
func parse(json: Data) {
let decoder = JSONDecoder()
if let jsonPetitions = try? decoder.decode(Countries.self, from: json) {
countries = jsonPetitions.items
DispatchQueue.main.async {
[weak self] in
self?.tableView.reloadData()
}
}
}
func showError() {
DispatchQueue.main.async {
[weak self] in
let ac = UIAlertController(title: "Loading error", message: "There was a problem loading the feed, please check your connection and try again.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self?.present(ac, animated: true)
}
}
}