diff options
author | Nicholas Tay <nick@windblume.net> | 2022-07-17 02:25:12 +1000 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2022-07-17 02:25:12 +1000 |
commit | bd7761216a065b0dd859cb19d709996739a240cd (patch) | |
tree | 8b1d33034f8c212613d7a07b864d1688b8c080d2 /foray | |
parent | 1adbed9f8b94521befd237c14d36325a55037a41 (diff) | |
download | forayios-bd7761216a065b0dd859cb19d709996739a240cd.tar.gz forayios-bd7761216a065b0dd859cb19d709996739a240cd.tar.bz2 forayios-bd7761216a065b0dd859cb19d709996739a240cd.zip |
Clean up force unwraps and lets
Wow, I didn't know `if let` was a thing back then, haha. Also made
UIImage a bit safer in case asset is missing by unwrapping in one common
place.
Diffstat (limited to 'foray')
-rw-r--r-- | foray/Extensions/UIImage+Extensions.swift | 21 | ||||
-rw-r--r-- | foray/Fetchers/ForayFetcher.swift | 14 | ||||
-rw-r--r-- | foray/Presenters/PenguinItemPresenter.swift | 9 | ||||
-rw-r--r-- | foray/Scenes/ForayDetailViewController.swift | 4 | ||||
-rw-r--r-- | foray/Scenes/ForayTableViewController.swift | 6 |
5 files changed, 37 insertions, 17 deletions
diff --git a/foray/Extensions/UIImage+Extensions.swift b/foray/Extensions/UIImage+Extensions.swift new file mode 100644 index 0000000..252c3fb --- /dev/null +++ b/foray/Extensions/UIImage+Extensions.swift @@ -0,0 +1,21 @@ +// +// UIImage+Extensions.swift +// foray +// +// Created by Nicholas Tay on 17/7/2022. +// + +import UIKit + +extension UIImage { + static func fromAsset(_ assetImage: AssetImage) -> UIImage { + return UIImage(named: assetImage.rawValue)! + } +} + +/// Known asset images that we can safely unwrap +enum AssetImage: String { + case AppIcon + case it + case spy +} diff --git a/foray/Fetchers/ForayFetcher.swift b/foray/Fetchers/ForayFetcher.swift index e4c6fd9..15db4c1 100644 --- a/foray/Fetchers/ForayFetcher.swift +++ b/foray/Fetchers/ForayFetcher.swift @@ -21,20 +21,22 @@ class ForayFetcher { let dateFormat = DateFormatter() dateFormat.dateFormat = "yyyy-MM-dd" - + + // OK to throw as I believe it just errors out the decode; it isn't what we expected schema wise return dateFormat.date(from: dateStr)! }) return jd }() - func fetch<T: Decodable>(url: String) async throws -> T { - var request = URLRequest(url: URL(string: url)!) + func fetch<T: Decodable>(url: URL) async throws -> T { + var request = URLRequest(url: 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") + if let basicUsername = basicUsername, + let basicPassword = basicPassword, + let authData = (basicUsername + ":" + basicPassword).data(using: .utf8) { + request.addValue("Basic \(authData.base64EncodedString())", forHTTPHeaderField: "Authorization") } let (data, _) = try await URLSession.shared.data(for: request) diff --git a/foray/Presenters/PenguinItemPresenter.swift b/foray/Presenters/PenguinItemPresenter.swift index 1d617bf..45b970c 100644 --- a/foray/Presenters/PenguinItemPresenter.swift +++ b/foray/Presenters/PenguinItemPresenter.swift @@ -16,12 +16,9 @@ class PenguinItemPresenter { let fetcher = ForayFetcher() func fetch() async -> [PenguinItemViewModel] { - do { - let apiItems: [PenguinItemModel] = try await fetcher.fetch(url: Constants.apiEndpoint) - return transform(models: apiItems) - } catch { - return [] - } + guard let endpoint = URL(string: Constants.apiEndpoint), + let apiItems: [PenguinItemModel] = try? await fetcher.fetch(url: endpoint) else { return [] } + return transform(models: apiItems) } func transform(models: [PenguinItemModel]) -> [PenguinItemViewModel] { diff --git a/foray/Scenes/ForayDetailViewController.swift b/foray/Scenes/ForayDetailViewController.swift index 1e2f9ca..52aa6b8 100644 --- a/foray/Scenes/ForayDetailViewController.swift +++ b/foray/Scenes/ForayDetailViewController.swift @@ -29,10 +29,10 @@ class ForayDetailViewController: UIViewController, HasCustomView, Coordinated { switch item.type { case .item: description += "Item" - image = UIImage(named: item.id)! + image = UIImage(named: item.id) ?? UIImage.fromAsset(.it) case .quest: description += "Quest" - image = UIImage(named: "spy")! + image = UIImage.fromAsset(.spy) } description += "\nID: " + item.id description += "\nReleased: " + item.releaseDateFormatted diff --git a/foray/Scenes/ForayTableViewController.swift b/foray/Scenes/ForayTableViewController.swift index 849553c..3d839a1 100644 --- a/foray/Scenes/ForayTableViewController.swift +++ b/foray/Scenes/ForayTableViewController.swift @@ -87,13 +87,13 @@ class ForayTableViewController: UITableViewController, Coordinated { switch item.type { case .item: type = "Item" - icon = UIImage(named: item.id)! + icon = UIImage(named: item.id) ?? UIImage.fromAsset(.it) case .quest: type = "Quest" - icon = UIImage(named: "spy")! + icon = UIImage.fromAsset(.spy) } - let cell: ForayTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ForayTableViewCell", for: indexPath) as! ForayTableViewCell + guard let cell = tableView.dequeueReusableCell(withIdentifier: "ForayTableViewCell", for: indexPath) as? ForayTableViewCell else { return ForayTableViewCell() } cell.setData(name: item.name, desc: type + "ID: " + item.id, img: icon) return cell } |