-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
121 lines (92 loc) · 4.25 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// ViewController.swift
// P07 WhiteHousePetitions
//
// Created by Julian Moorhouse on 04/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
class ViewController: UITableViewController {
var petitions = [Petition]()
var filteredPetitions = [Petition]()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Credits", style: .plain, target: self, action: #selector(credits))
let filter = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(showFilter))
let reset = UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: #selector(resetList))
navigationItem.rightBarButtonItems = [filter, reset]
//Beware - this code will lock up the app while data is downloaded, but it's ok for example purposes here
let urlString: String
if navigationController?.tabBarItem.tag == 0 {
// urlString = "https://api.whitehouse.gov/v1/petitions.json?limit=100"
urlString = "https://www.hackingwithswift.com/samples/petitions-1.json"
} else {
// urlString = "https://api.whitehouse.gov/v1/petitions.json?signatureCountFloor=10000&limit=100"
urlString = "https://www.hackingwithswift.com/samples/petitions-2.json"
}
if let url = URL(string: urlString) {
if let data = try? Data(contentsOf: url) {
parse(json: data)
return
}
}
showError()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredPetitions.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let petition = filteredPetitions[indexPath.row]
cell.textLabel?.text = petition.title
cell.detailTextLabel?.text = petition.body
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = DetailViewController()
vc.detailItem = filteredPetitions[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
func parse(json: Data) {
let decoder = JSONDecoder()
if let jsonPetitions = try? decoder.decode(Petitions.self, from: json) {
petitions = jsonPetitions.results
filteredPetitions = petitions
tableView.reloadData()
}
}
func showError() {
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))
present(ac, animated: true)
}
@objc func credits() {
let ac = UIAlertController(title: "Credits", message: "Data from We The People API of the Whitehouse", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(ac, animated: true)
}
@objc func showFilter() {
let ac = UIAlertController(title: "Enter filter text", message: nil, preferredStyle: .alert)
ac.addTextField()
let submitAction = UIAlertAction(title: "Search", style: .default) {
[weak self, weak ac] action in
guard let filter = ac?.textFields?[0].text else { return }
self?.filterResults(filter)
}
ac.addAction(submitAction)
present(ac, animated: true)
}
@objc func filterResults(_ filter: String) {
filteredPetitions.removeAll(keepingCapacity: true)
for petition in petitions {
if petition.title.contains(filter) || petition.body.contains(filter) {
filteredPetitions.append(petition);
}
}
tableView.reloadData()
}
@objc func resetList() {
filteredPetitions = petitions
tableView.reloadData()
}
}