From 1752ea7075939fb49dd72e0b8ea2accc2be1b02a Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Fri, 25 Mar 2022 09:59:35 +1100 Subject: Reorganise: NetworkManager -> Fetcher, Models -> dedicated groups --- foray/Fetchers/ForayFetcher.swift | 49 +++++++++++++++++++++++++++ foray/ForayModels.swift | 20 ----------- foray/ForayNetworkManager.swift | 51 ----------------------------- foray/ForayViewModels.swift | 16 --------- foray/Models/PenguinItemModel.swift | 20 +++++++++++ foray/Models/PenguinItemViewModel.swift | 16 +++++++++ foray/Presenters/PenguinItemPresenter.swift | 18 +++++----- 7 files changed, 95 insertions(+), 95 deletions(-) create mode 100644 foray/Fetchers/ForayFetcher.swift delete mode 100644 foray/ForayModels.swift delete mode 100644 foray/ForayNetworkManager.swift delete mode 100644 foray/ForayViewModels.swift create mode 100644 foray/Models/PenguinItemModel.swift create mode 100644 foray/Models/PenguinItemViewModel.swift (limited to 'foray') diff --git a/foray/Fetchers/ForayFetcher.swift b/foray/Fetchers/ForayFetcher.swift new file mode 100644 index 0000000..d8df037 --- /dev/null +++ b/foray/Fetchers/ForayFetcher.swift @@ -0,0 +1,49 @@ +// +// ForayNetworkManager.swift +// foray +// +// Created by Nicholas Tay on 20/3/2022. +// + +import Foundation + +class ForayFetcher { + var basicUsername: String? = nil + var basicPassword: String? = nil + + // Reuse JSON decoder, allows for customisation of things like date decode if required + var jsonDecoder: JSONDecoder = { + let jd = JSONDecoder() + // Defaults to year-month-date format + jd.dateDecodingStrategy = .custom({ (decoder) -> Date in + let container = try decoder.singleValueContainer() + let dateStr = try container.decode(String.self) + + let dateFormat = DateFormatter() + dateFormat.dateFormat = "yyyy-MM-dd" + + return dateFormat.date(from: dateStr)! + }) + return jd + }() + + func fetch(url: String, + receiver: @escaping (T) -> ()) { + // Fetch on a background thread + DispatchQueue.global(qos: .background).async { + var request = URLRequest(url: URL(string: url)!) + request.cachePolicy = .reloadRevalidatingCacheData // Needed otherwise default caching policy seems not to check properly + + // Basic auth if required + if (self.basicUsername != nil && self.basicPassword != nil) { + let authData = (self.basicUsername! + ":" + self.basicPassword!).data(using: .utf8)!.base64EncodedString() + request.addValue("Basic \(authData)", forHTTPHeaderField: "Authorization") + } + + URLSession.shared.dataTask(with: request, completionHandler: { data, response, error -> Void in + let items = try! self.jsonDecoder.decode(T.self, from: data!) + receiver(items) + }).resume() + } + } +} diff --git a/foray/ForayModels.swift b/foray/ForayModels.swift deleted file mode 100644 index c721b95..0000000 --- a/foray/ForayModels.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// 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/ForayNetworkManager.swift b/foray/ForayNetworkManager.swift deleted file mode 100644 index 53e9554..0000000 --- a/foray/ForayNetworkManager.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// ForayNetworkManager.swift -// foray -// -// Created by Nicholas Tay on 20/3/2022. -// - -import Foundation - -class ForayNetworkManager { - static let shared = ForayNetworkManager() - - var basicUsername: String? = nil - var basicPassword: String? = nil - - // Reuse JSON decoder, allows for customisation of things like date decode if required - var jsonDecoder: JSONDecoder = { - let jd = JSONDecoder() - // Defaults to year-month-date format - jd.dateDecodingStrategy = .custom({ (decoder) -> Date in - let container = try decoder.singleValueContainer() - let dateStr = try container.decode(String.self) - - let dateFormat = DateFormatter() - dateFormat.dateFormat = "yyyy-MM-dd" - - return dateFormat.date(from: dateStr)! - }) - return jd - }() - - func fetch(url: String, - receiver: @escaping (T) -> ()) { - // Fetch on a background thread - DispatchQueue.global(qos: .background).async { - var request = URLRequest(url: URL(string: url)!) - request.cachePolicy = .reloadRevalidatingCacheData // Needed otherwise default caching policy seems not to check properly - - // Basic auth if required - if (self.basicUsername != nil && self.basicPassword != nil) { - let authData = (self.basicUsername! + ":" + self.basicPassword!).data(using: .utf8)!.base64EncodedString() - request.addValue("Basic \(authData)", forHTTPHeaderField: "Authorization") - } - - URLSession.shared.dataTask(with: request, completionHandler: { data, response, error -> Void in - let items = try! self.jsonDecoder.decode(T.self, from: data!) - receiver(items) - }).resume() - } - } -} diff --git a/foray/ForayViewModels.swift b/foray/ForayViewModels.swift deleted file mode 100644 index 9b9b058..0000000 --- a/foray/ForayViewModels.swift +++ /dev/null @@ -1,16 +0,0 @@ -// -// 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/Models/PenguinItemModel.swift b/foray/Models/PenguinItemModel.swift new file mode 100644 index 0000000..c721b95 --- /dev/null +++ b/foray/Models/PenguinItemModel.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/Models/PenguinItemViewModel.swift b/foray/Models/PenguinItemViewModel.swift new file mode 100644 index 0000000..9b9b058 --- /dev/null +++ b/foray/Models/PenguinItemViewModel.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 index 2f3730f..698b56e 100644 --- a/foray/Presenters/PenguinItemPresenter.swift +++ b/foray/Presenters/PenguinItemPresenter.swift @@ -8,16 +8,18 @@ import Foundation class PenguinItemPresenter { + + let fetcher = ForayFetcher() + func fetch(receiver: @escaping ([PenguinItemViewModel]) -> ()) { - ForayNetworkManager.shared - .fetch(url: "https://users.windblume.net/~nick/upload/dummy.json") { (apiItems: [PenguinItemModel]) in - // Callback to main thread here - // There probably is a nicer way to do it, but we will DispatchQueue it back - // from the Presenter-level for now (main thread from VC onwards) - DispatchQueue.main.async { - receiver(self.transform(models: apiItems)) - } + fetcher.fetch(url: "https://users.windblume.net/~nick/upload/dummy.json") { (apiItems: [PenguinItemModel]) in + // Callback to main thread here + // There probably is a nicer way to do it, but we will DispatchQueue it back + // from the Presenter-level for now (main thread from VC onwards) + DispatchQueue.main.async { + receiver(self.transform(models: apiItems)) } + } } func transform(models: [PenguinItemModel]) -> [PenguinItemViewModel] { -- cgit