From d8624dbf8a9111f931802cbb2759ebd009096552 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Thu, 24 Mar 2022 17:07:51 +1100 Subject: 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. --- foray/Scenes/ForayTableViewController.swift | 40 +++++++++++++---------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'foray/Scenes/ForayTableViewController.swift') 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 -- cgit