-
Notifications
You must be signed in to change notification settings - Fork 16
/
ExploitationTableViewController.swift
514 lines (424 loc) · 25.5 KB
/
ExploitationTableViewController.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//
// ExploitationViewController.swift
// Child Labor
//
// Created by E J Kalafarski on 4/30/15.
// U.S. Government Work https://www.usa.gov/government-works
//
import UIKit
class ExploitationTableViewController: UITableViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var segmentControl : UISegmentedControl!
var countriesWithGoods = NSMutableArray()
var goodsByCountry: [String: NSMutableArray] = [:]
var exploitationByCountry: [String: NSMutableArray] = [:]
var goodsCount = NSMutableArray()
var goodsCountByChildLaborCountry = NSMutableArray()
var goodsCountByForcedLaborCountry = NSMutableArray()
var goodsCountByForcedChildLaborCountry = NSMutableArray()
var goodsCountByDerivedLaborCountry = NSMutableArray()
var countriesWithChildLabor = NSMutableArray()
var goodsWithChildLaborByCountry: [String: NSMutableArray] = [:]
var exploitationWithChildLaborByCountry: [String: NSMutableArray] = [:]
var countriesWithForcedLabor = NSMutableArray()
var goodsWithForcedLaborByCountry: [String: NSMutableArray] = [:]
var exploitationWithForcedLaborByCountry: [String: NSMutableArray] = [:]
var countriesWithForcedChildLabor = NSMutableArray()
var goodsWithForcedChildLaborByCountry: [String: NSMutableArray] = [:]
var exploitationWithForcedChildLaborByCountry: [String: NSMutableArray] = [:]
var countriesWithDerivedLabor = NSMutableArray()
var goodsWithDerivedLaborByCountry: [String: NSMutableArray] = [:]
var exploitationWithDerivedLaborByCountry: [String: NSMutableArray] = [:]
var goodsWithDerivedCountry: [String: String] = [:]
var state = 0
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
// Record GA view
let tracker = GAI.sharedInstance().defaultTracker
tracker?.set(kGAIScreenName, value: "Exploitation Screen")
tracker?.send(GAIDictionaryBuilder.createAppView().build() as! [AnyHashable: Any])
// Get the country data
var contents: NSString?
do {
contents = try NSString(contentsOfFile: Bundle.main.path(forResource: "countries_2016", ofType: "xml")!, encoding: String.Encoding.utf8.rawValue)
} catch _ {
contents = nil
}
let countriesXML = SWXMLHash.parse(contents as! String)
// Create an array of countries and an associative array of goods for each country
for country in countriesXML["Countries"]["Country"].all {
if country["Goods"]["Good"].all.count > 0 {
let countryName = country["Name"].element?.text
//var counttext = "This has" + String(country["Goods"]["Good"].all.count) + "goods"
let goods = NSMutableArray()
let exploitations = NSMutableArray()
goodsCount.add(String(country["Goods"]["Good"].all.count) as NSString)
let goodsWithChildLabor = NSMutableArray()
let exploitationWithChildLabor = NSMutableArray()
let goodsWithForcedLabor = NSMutableArray()
let exploitationWithForcedLabor = NSMutableArray()
let goodsWithForcedChildLabor = NSMutableArray()
let exploitationWithForcedChildLabor = NSMutableArray()
let goodsWithDerivedLaborExploitation = NSMutableArray()
let exploitationWithDerivedLabor = NSMutableArray()
var thisCountryHasChildLabor = false
var thisCountryHasForcedLabor = false
var thisCountryHasForcedChildLabor = false
var thisCountryHasDerivedLabor = false
// For each of the country's goods
for good in country["Goods"]["Good"].all {
let goodName = good["Good_Name"].element?.text
let childLaborStatusForGood = good["Child_Labor"].element?.text
let forcedLaborStatusForGood = good["Forced_Labor"].element?.text
let forcedChildLaborStatusForGood = good["Forced_Child_Labor"].element?.text
let derivedLaborExploitationForGood = good["Derived_Labor_Exploitation"].element?.text
// For every good
// Add the good to an array
goods.add(goodName!)
// Add the exploitation type to an array
if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "No" {
exploitations.add(0)
} else if childLaborStatusForGood == "No" && forcedLaborStatusForGood == "Yes" {
exploitations.add(1)
} else if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "Yes" && forcedChildLaborStatusForGood == "No" {
exploitations.add(2)
} else if derivedLaborExploitationForGood == "Yes" {
exploitations.add(4)
} else {
exploitations.add(3)
}
// For a good with child labor
if childLaborStatusForGood == "Yes" {
goodsWithChildLabor.add(goodName!)
thisCountryHasChildLabor = true
// Add the exploitation type to an array
if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "No" {
exploitationWithChildLabor.add(0)
} else if childLaborStatusForGood == "No" && forcedLaborStatusForGood == "Yes" {
exploitationWithChildLabor.add(1)
} else if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "Yes" && forcedChildLaborStatusForGood == "No" {
exploitationWithChildLabor.add(2)
} else {
exploitationWithChildLabor.add(3)
}
}
// For a good with forced labor
if forcedLaborStatusForGood == "Yes" {
goodsWithForcedLabor.add(goodName!)
thisCountryHasForcedLabor = true
// Add the exploitation type to an array
if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "No" {
exploitationWithForcedLabor.add(0)
} else if childLaborStatusForGood == "No" && forcedLaborStatusForGood == "Yes" {
exploitationWithForcedLabor.add(1)
} else if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "Yes" && forcedChildLaborStatusForGood == "No" {
exploitationWithForcedLabor.add(2)
} else {
exploitationWithForcedLabor.add(3)
}
}
// For a good with forced child labor
if forcedChildLaborStatusForGood == "Yes" {
goodsWithForcedChildLabor.add(goodName!)
thisCountryHasForcedChildLabor = true
// Add the exploitation type to an array
if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "No" {
exploitationWithForcedChildLabor.add(0)
} else if childLaborStatusForGood == "No" && forcedLaborStatusForGood == "Yes" {
exploitationWithForcedChildLabor.add(1)
} else if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "Yes" && forcedChildLaborStatusForGood == "No" {
exploitationWithForcedChildLabor.add(2)
} else {
exploitationWithForcedChildLabor.add(3)
}
}
// For a good with forced Derived labor
if derivedLaborExploitationForGood == "Yes" {
let derivedCountry = good["Derived_Country"].element?.text ?? "no"
goodsWithDerivedLaborExploitation.add(goodName!)
thisCountryHasDerivedLabor = true
// Add the exploitation type to an array
if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "No" {
exploitationWithDerivedLabor.add(0)
} else if childLaborStatusForGood == "No" && forcedLaborStatusForGood == "Yes" {
exploitationWithDerivedLabor.add(1)
} else if childLaborStatusForGood == "Yes" && forcedLaborStatusForGood == "Yes" && forcedChildLaborStatusForGood == "No" {
exploitationWithDerivedLabor.add(2)
} else if childLaborStatusForGood == "No" && forcedLaborStatusForGood == "No" && forcedChildLaborStatusForGood == "No" {
exploitationWithDerivedLabor.add(4)
} else {
exploitationWithDerivedLabor.add(3)
}
goodsWithDerivedCountry.updateValue(derivedCountry, forKey: goodName ?? "")
}
}
// For every country
countriesWithGoods.add(countryName!)
// goodsCountByCountry.updateValue(goodsCount, forKey: countryName!)
goodsByCountry.updateValue(goods, forKey: countryName!)
exploitationByCountry.updateValue(exploitations, forKey: countryName!)
// For a country with child labor
if thisCountryHasChildLabor {
countriesWithChildLabor.add(countryName!)
goodsCountByChildLaborCountry.add(String(goodsWithChildLabor.count) as NSString)
goodsWithChildLaborByCountry.updateValue(goodsWithChildLabor, forKey: countryName!)
exploitationWithChildLaborByCountry.updateValue(exploitationWithChildLabor, forKey: countryName!)
}
// For a country with forced labor
if thisCountryHasForcedLabor {
countriesWithForcedLabor.add(countryName!)
goodsCountByForcedLaborCountry.add(String(goodsWithForcedLabor.count) as NSString)
goodsWithForcedLaborByCountry.updateValue(goodsWithForcedLabor, forKey: countryName!)
exploitationWithForcedLaborByCountry.updateValue(exploitationWithForcedLabor, forKey: countryName!)
}
// For a country with forced child labor
if thisCountryHasForcedChildLabor {
countriesWithForcedChildLabor.add(countryName!)
goodsCountByForcedChildLaborCountry.add(String(goodsWithForcedChildLabor.count) as NSString)
goodsWithForcedChildLaborByCountry.updateValue(goodsWithForcedChildLabor, forKey: countryName!)
exploitationWithForcedChildLaborByCountry.updateValue(exploitationWithForcedChildLabor, forKey: countryName!)
}
// For a country with Derived labor
if thisCountryHasDerivedLabor {
countriesWithDerivedLabor.add(countryName!)
goodsCountByDerivedLaborCountry.add(String(goodsWithDerivedLaborExploitation.count) as NSString)
goodsWithDerivedLaborByCountry.updateValue(goodsWithDerivedLaborExploitation, forKey: countryName!)
exploitationWithDerivedLaborByCountry.updateValue(exploitationWithDerivedLabor, forKey: countryName!)
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// self.customizeSegmentControl()
// Make sure the ugly table cell selection is cleared when returning to this view
if let tableIndex = self.tableView.indexPathForSelectedRow {
self.tableView.deselectRow(at: tableIndex, animated: false)
}
}
private func customizeSegmentControl() {
if #available(iOS 13.0, *) {
self.segmentControl.setImage(
UIImage.textEmbededImage(
image: UIImage(systemName: "flag.fill")!,
string: "Derived Labor",
color: .black
),
forSegmentAt: 4
)
} else {
// Fallback on earlier versions
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
switch state {
case 0:
return countriesWithGoods.count
case 1:
return countriesWithChildLabor.count
case 2:
return countriesWithForcedLabor.count
case 4:
return countriesWithDerivedLabor.count
default:
return countriesWithForcedChildLabor.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath) as UITableViewCell
// Configure the cell...
let theLabel : UILabel? = cell.viewWithTag(101) as? UILabel
// let theButton : UIButton? = cell.viewWithTag(101) as? UIButton
var theCollectionView : UICollectionView? //= cell.viewWithTag(102) as? UICollectionView
let theCount : UILabel? = cell.viewWithTag(110) as? UILabel
//theCount?.text = "This has" + String(country["Goods"]["Good"].all.count) + "goods"
// Set collection view tag to the row index, to pass the index of the country into the collection view
for subview in cell.contentView.subviews {
if subview.isKind(of: UICollectionView.self) {
theCollectionView = subview as? UICollectionView
}
}
theCollectionView!.tag = indexPath.row
theCollectionView?.reloadData()
switch state {
case 0:
// Set the country name
theLabel?.text = countriesWithGoods[indexPath.row] as? String
theLabel?.accessibilityLabel = ""
theCount?.text = goodsCount[indexPath.row] as? String
theCount?.accessibilityLabel = (countriesWithGoods[indexPath.row] as? String)! + " has " + (goodsCount[indexPath.row] as? String)! + " goods of all exploitation types " + (goodsByCountry[countriesWithGoods[indexPath.row] as! String]?.componentsJoined(by: " "))!
case 1:
// Set the country name
theLabel?.text = countriesWithChildLabor[indexPath.row] as? String
theLabel?.accessibilityLabel = ""
theCount?.text = goodsCountByChildLaborCountry[indexPath.row] as? String
theCount?.accessibilityLabel = (countriesWithChildLabor[indexPath.row] as? String)! + " has " + (goodsCountByChildLaborCountry[indexPath.row] as? String)! + " goods of Child Labor exploitation type " + (goodsWithChildLaborByCountry[countriesWithChildLabor[indexPath.row] as! String]?.componentsJoined(by: " "))!
case 2:
// Set the country name
theLabel?.text = countriesWithForcedLabor[indexPath.row] as? String
theLabel?.accessibilityLabel = ""
theCount?.text = goodsCountByForcedLaborCountry[indexPath.row] as? String
theCount?.accessibilityLabel = (countriesWithForcedLabor[indexPath.row] as? String)! + " has " + (goodsCountByForcedLaborCountry[indexPath.row] as? String)! + " goods of Forced Labor exploitation type " + (goodsWithForcedLaborByCountry[countriesWithForcedLabor[indexPath.row] as! String]?.componentsJoined(by: " "))!
case 4:
// Set the country name
theLabel?.text = countriesWithDerivedLabor[indexPath.row] as? String
theLabel?.accessibilityLabel = ""
theCount?.text = goodsCountByDerivedLaborCountry[indexPath.row] as? String
theCount?.accessibilityLabel = (countriesWithDerivedLabor[indexPath.row] as? String)! + " has " + (goodsCountByDerivedLaborCountry[indexPath.row] as? String)! + " goods of Derived Labor exploitation type " + "\(goodsWithDerivedLaborByCountry[countriesWithForcedLabor[indexPath.row] as! String]?.componentsJoined(by: " ") ?? "")"
default:
// Set the country name
theLabel?.text = countriesWithForcedChildLabor[indexPath.row] as? String
theLabel?.accessibilityLabel = ""
theCount?.text = goodsCountByForcedChildLaborCountry[indexPath.row] as? String
theCount?.accessibilityLabel = (countriesWithForcedChildLabor[indexPath.row] as? String)! + " has " + (goodsCountByForcedChildLaborCountry[indexPath.row] as? String)! + " goods of Forced Child Labor exploitation type " + (goodsWithForcedChildLaborByCountry[countriesWithForcedChildLabor[indexPath.row] as! String]?.componentsJoined(by: " "))!
}
if #available(iOS 13.0, *) {
theLabel?.textColor = .label
theCount?.textColor = .label
} else {
// Fallback on earlier versions
}
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch state {
case 0:
return goodsByCountry[countriesWithGoods[collectionView.tag] as! String]!.count
case 1:
return goodsWithChildLaborByCountry[countriesWithChildLabor[collectionView.tag] as! String]!.count
case 2:
return goodsWithForcedLaborByCountry[countriesWithForcedLabor[collectionView.tag] as! String]!.count
case 4:
return goodsWithDerivedLaborByCountry[countriesWithDerivedLabor[collectionView.tag] as! String]!.count
default:
return goodsWithForcedChildLaborByCountry[countriesWithForcedChildLabor[collectionView.tag] as! String]!.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Good", for: indexPath) as UICollectionViewCell
cell.isAccessibilityElement = true
collectionView.isAccessibilityElement = false
collectionView.shouldGroupAccessibilityChildren = true
collectionView.delegate = self
//
let goodButton : UIButton? = cell.viewWithTag(201) as? UIButton
//
let cl : UIImageView? = cell.viewWithTag(202) as? UIImageView
let fl : UIImageView? = cell.viewWithTag(203) as? UIImageView
let dlFlag : UIImageView? = cell.viewWithTag(207) as? UIImageView
let dl : UIImageView? = cell.viewWithTag(208) as? UIImageView
let goodLabel : UILabel? = cell.viewWithTag(204) as? UILabel
var goodName = ""
var countryName = ""
var exploitationType = 0
switch state {
case 0:
goodName = goodsByCountry[countriesWithGoods[collectionView.tag] as! String]![indexPath.row] as! String
countryName = countriesWithGoods[collectionView.tag] as! String
exploitationType = exploitationByCountry[countryName]![indexPath.row] as! Int
case 1:
goodName = goodsWithChildLaborByCountry[countriesWithChildLabor[collectionView.tag] as! String]![indexPath.row] as! String
countryName = countriesWithChildLabor[collectionView.tag] as! String
exploitationType = exploitationWithChildLaborByCountry[countryName]![indexPath.row] as! Int
case 2:
goodName = goodsWithForcedLaborByCountry[countriesWithForcedLabor[collectionView.tag] as! String]![indexPath.row] as! String
countryName = countriesWithForcedLabor[collectionView.tag] as! String
exploitationType = exploitationWithForcedLaborByCountry[countryName]![indexPath.row] as! Int
case 4:
goodName = goodsWithDerivedLaborByCountry[countriesWithDerivedLabor[collectionView.tag] as! String]![indexPath.row] as! String
countryName = countriesWithDerivedLabor[collectionView.tag] as! String
exploitationType = exploitationWithDerivedLaborByCountry[countryName]![indexPath.row] as! Int
default:
goodName = goodsWithForcedChildLaborByCountry[countriesWithForcedChildLabor[collectionView.tag] as! String]![indexPath.row] as! String
countryName = countriesWithForcedChildLabor[collectionView.tag] as! String
exploitationType = exploitationWithForcedChildLaborByCountry[countryName]![indexPath.row] as! Int
}
goodButton!.setImage(UIImage(named:"icons_" + goodName.replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: " ", with: "_") + "-33")?.withRenderingMode(.alwaysTemplate), for: UIControlState())
goodLabel?.text = goodName
switch exploitationType {
case 0:
cl?.isHidden = false
cl?.image = UIImage(named: "hand")
goodButton?.accessibilityLabel = goodName+" Child Labor"
cell.accessibilityLabel = goodName+" Child Labor"
fl?.isHidden = true
dl?.isHidden = true
dlFlag?.isHidden = true
case 1:
cl?.isHidden = true
cl?.image = UIImage(named: "hand")
goodButton?.accessibilityLabel = goodName+" Forced Labor"
cell.accessibilityLabel = goodName+" Forced Labor"
fl?.isHidden = false
fl?.image = UIImage(named: "man")
dl?.isHidden = true
dlFlag?.isHidden = true
case 2:
cl?.isHidden = false
cl?.image = UIImage(named: "hand")
goodButton?.accessibilityLabel = goodName+" Forced Child Labor"
cell.accessibilityLabel = goodName+" Forced Child Labor"
fl?.isHidden = false
fl?.image = UIImage(named: "man")
dl?.isHidden = true
dlFlag?.isHidden = true
case 3:
cl?.isHidden = false
cl?.image = UIImage(named: "hand-black")
goodButton?.accessibilityLabel = goodName+" Forced Child Labor"
cell.accessibilityLabel = goodName+" Forced Child Labor"
fl?.isHidden = false
fl?.image = UIImage(named: "man")
dl?.isHidden = true
dlFlag?.isHidden = true
case 4:
goodButton?.accessibilityLabel = goodName+" Derived Labor"
cell.accessibilityLabel = goodName+" Derived Labor"
cl?.isHidden = true
fl?.isHidden = true
if let derivedCountry = goodsWithDerivedCountry[goodName] {
if let flagImage = UIImage(named: (derivedCountry.replacingOccurrences(of: " ", with: "_", options: NSString.CompareOptions.literal, range: nil).replacingOccurrences(of: "ô", with: "o", options: NSString.CompareOptions.literal, range: nil))) {
dl?.isHidden = false
dlFlag?.isHidden = false
dlFlag?.image = flagImage
dlFlag?.layer.borderColor = UIColor.gray.cgColor
dlFlag?.layer.borderWidth = 1
}
}
default:
dl?.isHidden = true
dlFlag?.isHidden = true
cell.accessibilityLabel = goodName+" Forced Child Labor"
break
}
cell.accessibilityHint = "double tap to open"
return cell
}
@IBAction func filterChanged(_ sender: AnyObject) {
state = sender.selectedSegmentIndex
self.tableView.reloadData()
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goodSelectedFromExploitationTable" {
let svc = segue.destination as! GoodController
svc.goodName = ((sender as! UIButton).superview?.viewWithTag(204) as! UILabel).text!
} else if segue.identifier == "countrySelectedFromExploitationTable" {
let svc = segue.destination as! CountryController
svc.countryName = ((sender as! UITableViewCell).viewWithTag(101) as! UILabel).text!
}
}
}