aboutsummaryrefslogtreecommitdiff
path: root/foray/Scenes/ForayTableViewController.swift
diff options
context:
space:
mode:
authorNicholas Tay <nick@windblume.net>2022-03-24 16:32:23 +1100
committerNicholas Tay <nick@windblume.net>2022-03-24 16:32:23 +1100
commit1b292bc251b3dbef532dacad9705bd197ac4227b (patch)
tree1635ed0cc70922f6337d010b42e2119258877114 /foray/Scenes/ForayTableViewController.swift
parent716724df0fee78a8976d5255096e000af29daad1 (diff)
downloadforayios-1b292bc251b3dbef532dacad9705bd197ac4227b.tar.gz
forayios-1b292bc251b3dbef532dacad9705bd197ac4227b.tar.bz2
forayios-1b292bc251b3dbef532dacad9705bd197ac4227b.zip
Reorganise into folder groups
In preparation for presenters to come in largely, lots of files starting to go everywhere...
Diffstat (limited to 'foray/Scenes/ForayTableViewController.swift')
-rw-r--r--foray/Scenes/ForayTableViewController.swift107
1 files changed, 107 insertions, 0 deletions
diff --git a/foray/Scenes/ForayTableViewController.swift b/foray/Scenes/ForayTableViewController.swift
new file mode 100644
index 0000000..37ac2e5
--- /dev/null
+++ b/foray/Scenes/ForayTableViewController.swift
@@ -0,0 +1,107 @@
+//
+// ForayTableViewController.swift
+// foray
+//
+// Created by Nicholas Tay on 14/3/2022.
+//
+
+import UIKit
+
+class ForayTableViewController: UITableViewController, ForayCoordinated {
+
+ 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(ForayNewTableViewCell.self, forCellReuseIdentifier: "ForayNewTableViewCell")
+
+ // 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() {
+ ForayNetworkManager.shared.get(
+ url: "https://users.windblume.net/~nick/upload/dummy.json",
+ onComplete: { (apiItems: [PenguinItem]) in
+ var items = apiItems
+
+ // Show items in chronological order within sections
+ items.sort { (lhs, rhs) in lhs.releaseDate < rhs.releaseDate }
+
+ // Group by year sections
+ let groups = Dictionary(grouping: apiItems) { (item) in
+ return Calendar.current.component(.year, from: item.releaseDate)
+ }
+ 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 {
+ let item = self.sections[indexPath.section].items[indexPath.row]
+
+ let type: String
+ let icon: UIImage
+ switch item.type {
+ case .item:
+ type = "Item"
+ icon = UIImage(named: item.id)!
+ case .quest:
+ type = "Quest"
+ icon = UIImage(named: "spy")!
+ }
+
+ let cell: ForayNewTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ForayNewTableViewCell", for: indexPath) as! ForayNewTableViewCell
+ cell.setData(name: item.name, desc: type + "ID: " + item.id, img: 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)
+ }
+}