aboutsummaryrefslogtreecommitdiff
path: root/foray/Scenes/ForayTableViewController.swift
diff options
context:
space:
mode:
authorNicholas Tay <nick@windblume.net>2022-03-24 17:07:51 +1100
committerNicholas Tay <nick@windblume.net>2022-03-24 17:07:51 +1100
commitd8624dbf8a9111f931802cbb2759ebd009096552 (patch)
treec0154b234a8274374aada77fd7a63d5dabf00e7e /foray/Scenes/ForayTableViewController.swift
parent1b292bc251b3dbef532dacad9705bd197ac4227b (diff)
downloadforayios-d8624dbf8a9111f931802cbb2759ebd009096552.tar.gz
forayios-d8624dbf8a9111f931802cbb2759ebd009096552.tar.bz2
forayios-d8624dbf8a9111f931802cbb2759ebd009096552.zip
First attempt at Presenter
This is admittedly pretty hard for me to wrap my head around, and I'm not even using background threading explicitly yet. Will improve.
Diffstat (limited to 'foray/Scenes/ForayTableViewController.swift')
-rw-r--r--foray/Scenes/ForayTableViewController.swift40
1 files changed, 18 insertions, 22 deletions
diff --git a/foray/Scenes/ForayTableViewController.swift b/foray/Scenes/ForayTableViewController.swift
index 37ac2e5..ed44139 100644
--- a/foray/Scenes/ForayTableViewController.swift
+++ b/foray/Scenes/ForayTableViewController.swift
@@ -7,8 +7,14 @@
import UIKit
+struct YearSection {
+ var year: Int
+ var items: [PenguinItemViewModel]
+}
+
class ForayTableViewController: UITableViewController, ForayCoordinated {
+ let presenter: PenguinItemPresenter = PenguinItemPresenter()
var coordinator: ForayCoordinator?
// MARK: - Static data TEMP
@@ -39,28 +45,18 @@ class ForayTableViewController: UITableViewController, ForayCoordinated {
}
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()
- })
+ presenter.getData(onComplete: { (data: [PenguinItemViewModel]) in
+ let groups = Dictionary(grouping: data) { $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