diff options
author | Nicholas Tay <nick@windblume.net> | 2022-03-24 17:07:51 +1100 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2022-03-24 17:07:51 +1100 |
commit | d8624dbf8a9111f931802cbb2759ebd009096552 (patch) | |
tree | c0154b234a8274374aada77fd7a63d5dabf00e7e /foray | |
parent | 1b292bc251b3dbef532dacad9705bd197ac4227b (diff) | |
download | forayios-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')
-rw-r--r-- | foray/Coordinators/ForayCoordinator.swift | 6 | ||||
-rw-r--r-- | foray/ForayItems.swift | 25 | ||||
-rw-r--r-- | foray/ForayModels.swift | 20 | ||||
-rw-r--r-- | foray/ForayViewModels.swift | 16 | ||||
-rw-r--r-- | foray/Presenters/PenguinItemPresenter.swift | 32 | ||||
-rw-r--r-- | foray/Scenes/ForayTableViewController.swift | 40 |
6 files changed, 88 insertions, 51 deletions
diff --git a/foray/Coordinators/ForayCoordinator.swift b/foray/Coordinators/ForayCoordinator.swift index 6497ec0..6c4bb3f 100644 --- a/foray/Coordinators/ForayCoordinator.swift +++ b/foray/Coordinators/ForayCoordinator.swift @@ -33,7 +33,7 @@ class ForayCoordinator: Coordinator { let detailViewController = ForayDetailViewController() - func showDetails(item: PenguinItem) { + func showDetails(item: PenguinItemViewModel) { let image: UIImage var description: String = "Type: " switch item.type { @@ -45,9 +45,7 @@ class ForayCoordinator: Coordinator { image = UIImage(named: "spy")! } description += "\nID: " + item.id - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "yyyy-MM-dd" - description += "\nReleased: " + dateFormatter.string(from: item.releaseDate) + description += "\nReleased: " + item.releaseDateFormatted detailViewController.setDetails(name: item.name, description: description, image: image) push(vc: detailViewController) diff --git a/foray/ForayItems.swift b/foray/ForayItems.swift deleted file mode 100644 index a8786ee..0000000 --- a/foray/ForayItems.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// ForayItems.swift -// foray -// -// Created by Nicholas Tay on 20/3/2022. -// - -import Foundation - -enum ItemType: String, Decodable { - case item - case quest -} - -struct YearSection { - var year: Int - var items: [PenguinItem] -} - -struct PenguinItem: Decodable { - var type: ItemType - var releaseDate: Date - var id: String - var name: String -} diff --git a/foray/ForayModels.swift b/foray/ForayModels.swift new file mode 100644 index 0000000..c721b95 --- /dev/null +++ b/foray/ForayModels.swift @@ -0,0 +1,20 @@ +// +// ForayItems.swift +// foray +// +// Created by Nicholas Tay on 20/3/2022. +// + +import Foundation + +enum PenguinItemType: String, Decodable { + case item + case quest +} + +struct PenguinItemModel: Decodable { + let type: PenguinItemType + let releaseDate: Date + let id: String + let name: String +} diff --git a/foray/ForayViewModels.swift b/foray/ForayViewModels.swift new file mode 100644 index 0000000..9b9b058 --- /dev/null +++ b/foray/ForayViewModels.swift @@ -0,0 +1,16 @@ +// +// ForayViewModels.swift +// foray +// +// Created by Nicholas Tay on 24/3/2022. +// + +import Foundation + +struct PenguinItemViewModel { + let type: PenguinItemType + let releaseDateFormatted: String + let year: Int + let id: String + let name: String +} diff --git a/foray/Presenters/PenguinItemPresenter.swift b/foray/Presenters/PenguinItemPresenter.swift new file mode 100644 index 0000000..0420825 --- /dev/null +++ b/foray/Presenters/PenguinItemPresenter.swift @@ -0,0 +1,32 @@ +// +// PenguinItemPresenter.swift +// foray +// +// Created by Nicholas Tay on 24/3/2022. +// + +import Foundation + +class PenguinItemPresenter { + func getData(onComplete: @escaping ([PenguinItemViewModel]) -> ()) { + ForayNetworkManager.shared.get( + url: "https://users.windblume.net/~nick/upload/dummy.json", + onComplete: { (apiItems: [PenguinItemModel]) in + onComplete(self.transform(models: apiItems)) + }) + } + + func transform(models: [PenguinItemModel]) -> [PenguinItemViewModel] { + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + + return models + .sorted { $0.releaseDate < $1.releaseDate } + .map { + PenguinItemViewModel(type: $0.type, + releaseDateFormatted: dateFormatter.string(from: $0.releaseDate), + year: Calendar.current.component(.year, from: $0.releaseDate), + id: $0.id, name: $0.name) + } + } +} 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 |