aboutsummaryrefslogtreecommitdiff
path: root/foray/Presenters
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/Presenters
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 '')
-rw-r--r--foray/Presenters/PenguinItemPresenter.swift32
1 files changed, 32 insertions, 0 deletions
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)
+ }
+ }
+}