-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
157 lines (139 loc) · 5.61 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// ViewController.swift
// project7
//
// Created by Arnold Mitricã on 23/10/2020.
//
import UIKit
class ViewController: UITableViewController {
var petitions = [Petition]()
var filter = [String]()
var petitionsfiltered = [Petition]()
var urlString = ""
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if filter.isEmpty {
return petitions.count
}
else{
return petitionsfiltered.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let petition = petitions[indexPath.row]
//let petitionfiltered = petitionsfiltered[indexPath.row]
if filter.isEmpty {
cell.textLabel?.text = petition.title
cell.detailTextLabel?.text = petition.body
//print(indexPath.row)
}
else{
cell.textLabel?.text = petitionsfiltered[indexPath.row].title
cell.detailTextLabel?.text = petitionsfiltered[indexPath.row].body
}
return cell
}
func filtering(){
// let petitionsfilterednew
for word in filter{
self.petitionsfiltered = petitionsfiltered.filter { $0.body.contains(word)}
}
// DispatchQueue.main.async {
// [weak self] in
// self?.petitionsfiltered = petitionsfilterednew
// }
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(alerta))
navigationItem.rightBarButtonItems = [UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(alerta)), UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(addfilter))]
// let urlString = "https://api.whitehouse.gov/v1/petitions.json?limit=100"
if navigationController?.tabBarItem.tag == 0 {
urlString = "https://api.whitehouse.gov/v1/petitions.json?limit=100"
} else {
urlString = "https://api.whitehouse.gov/v1/petitions.json?signatureCountFloor=10000&limit=100"
}
performSelector(inBackground: #selector(fetchJSON), with: nil)
//fetchJSON()
}
@objc func fetchJSON() {
if let url = URL(string: urlString) {
if let data = try? Data(contentsOf: url) {
parse(json: data)
return
}
}
performSelector(onMainThread: #selector(showError), with: nil, waitUntilDone: false)
}
func parse(json: Data) {
let decoder = JSONDecoder()
if let jsonPetitions = try? decoder.decode(Petitions.self, from: json) {
petitions = jsonPetitions.results
tableView.performSelector(onMainThread: #selector(UITableView.reloadData), with: nil, waitUntilDone: false)
} else {
performSelector(onMainThread: #selector(showError), with: nil, waitUntilDone: false)
}
}
@objc func alerta(){
let ac = UIAlertController(title: "Info", message: "We The People API of the Whitehouse.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
@objc func addfilter() {
// title = "Add filter"
let ac = UIAlertController(title: "Enter filter", message:nil, preferredStyle: .alert)
ac.addTextField()
let submitAction = UIAlertAction(title: "Submit", style: .default){
[weak self, weak ac] _ in
guard let answer = ac?.textFields?[0].text else { return }
self?.submit(answer)
}
ac.addAction(submitAction)
present(ac, animated: true)
}
func submit(_ answer: String)
{
filter.append(answer)
print(filter)
if filter.count == 1 {
navigationItem.rightBarButtonItems?.append(UIBarButtonItem(barButtonSystemItem: .redo, target: self, action: #selector(removefilter)))
petitionsfiltered = petitions
}
filtering()
tableView.reloadData()
}
func submitremove(){
filter.removeAll()
navigationItem.rightBarButtonItems?.removeLast()
tableView.reloadData()
}
@objc func removefilter() {
var filters = String()
for word in filter{
filters.append(word)
filters.append(" ")
}
let ac = UIAlertController(title: "Remove filter", message: "You removed filters: '\(filters)'.", preferredStyle: .alert)
let submitAction = UIAlertAction(title: "Submit", style: .default){
[weak self] _ in
self?.submitremove()
}
ac.addAction(submitAction)
present(ac, animated: true)
}
@objc 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)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = DetailViewController()
if filter.isEmpty{
vc.detailItem = petitions[indexPath.row]
}
else{
vc.detailItem = petitionsfiltered[indexPath.row]
}
navigationController?.pushViewController(vc, animated: true)
}
}