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
|
//
// ForayTableViewController.swift
// foray
//
// Created by Nicholas Tay on 14/3/2022.
//
import UIKit
struct YearSection {
var year: Int
var items: [PenguinItemViewModel]
}
class ForayTableViewController: UITableViewController, Coordinated {
typealias CoordinatorType = ForayCoordinator
let presenter: PenguinItemPresenter = PenguinItemPresenter()
var coordinator: ForayCoordinator?
// MARK: - Static data TEMP
var sections = [YearSection]()
// MARK: - On load
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Foray"
tableView.rowHeight = UITableView.automaticDimension
// Register our custom cell
tableView.register(ForayTableViewCell.self, forCellReuseIdentifier: "ForayTableViewCell")
// Not sure if this is the right way to go about this...
coordinator?.showLoading()
reloadApiData()
self.refreshControl = UIRefreshControl()
self.refreshControl?.addTarget(self, action: #selector(doRefresh), for: UIControl.Event.valueChanged)
}
@objc
func doRefresh(sender: AnyObject) {
reloadApiData()
}
func reloadApiData() {
Task(priority: .medium) { [weak self] in
let models = await self?.presenter.fetch()
guard let self = self,
let models = models else { return }
let groups = Dictionary(grouping: models) { $0.year }
self.sections = groups.map { (key, values) in
return YearSection(year: key, items: values)
}
// Sort the sections from oldest year to newest
self.sections.sort { (lhs, rhs) in lhs.year < rhs.year }
self.tableView.reloadData()
self.refreshControl?.endRefreshing()
self.coordinator?.hideLoading()
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// Returns number of sections for table
return self.sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Returns number of rows for table's section
return self.sections[section].items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ForayTableViewCell", for: indexPath) as? ForayTableViewCell else { return ForayTableViewCell() }
let item = self.sections[indexPath.section].items[indexPath.row]
cell.setData(name: item.name, desc: item.typeName + "ID: " + item.id, img: item.icon)
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Released in \(self.sections[section].year)"
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let item = self.sections[indexPath.section].items[indexPath.row]
self.coordinator?.showDetails(item: item)
}
}
|