diff options
author | Nicholas Tay <nick@windblume.net> | 2022-03-21 15:13:23 +1100 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2022-03-21 15:13:23 +1100 |
commit | 12abbe7e6ea257fa1686a2ef24fbf12009adcb8d (patch) | |
tree | fd26bb46fdd0915754cd36eafd42a0258cf60124 /foray | |
parent | ccd997e3c6c4262d58b21753fd69b31ca0a601e1 (diff) | |
download | forayios-12abbe7e6ea257fa1686a2ef24fbf12009adcb8d.tar.gz forayios-12abbe7e6ea257fa1686a2ef24fbf12009adcb8d.tar.bz2 forayios-12abbe7e6ea257fa1686a2ef24fbf12009adcb8d.zip |
First try at implementing coordinator pattern
Resources:
- https://www.hackingwithswift.com/articles/71/how-to-use-the-coordinator-pattern-in-ios-apps
Diffstat (limited to 'foray')
-rw-r--r-- | foray/AppDelegate.swift | 13 | ||||
-rw-r--r-- | foray/Coordinator.swift | 19 | ||||
-rw-r--r-- | foray/ForayCoordinator.swift | 53 | ||||
-rw-r--r-- | foray/ForayDetailViewController.swift | 26 | ||||
-rw-r--r-- | foray/ForayTableViewController.swift | 10 |
5 files changed, 90 insertions, 31 deletions
diff --git a/foray/AppDelegate.swift b/foray/AppDelegate.swift index fc5d2f9..7795d22 100644 --- a/foray/AppDelegate.swift +++ b/foray/AppDelegate.swift @@ -11,17 +11,18 @@ import UIKit class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + var coordinator: Coordinator? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let nav = UINavigationController() - let mainView = ForayTableViewController() - nav.viewControllers = [mainView] - let window = UIWindow(frame: UIScreen.main.bounds) - window.rootViewController = nav - self.window = window - window.makeKeyAndVisible() + coordinator = ForayCoordinator(navigationController: nav) + coordinator?.start() + + self.window = UIWindow(frame: UIScreen.main.bounds) + self.window?.rootViewController = nav + self.window?.makeKeyAndVisible() return true } diff --git a/foray/Coordinator.swift b/foray/Coordinator.swift new file mode 100644 index 0000000..db97a1d --- /dev/null +++ b/foray/Coordinator.swift @@ -0,0 +1,19 @@ +// +// Coordinator.swift +// foray +// +// Created by Nicholas Tay on 21/3/2022. +// Based on code from Paul Hudson on Hacking with Swift +// (https://www.hackingwithswift.com/articles/71/how-to-use-the-coordinator-pattern-in-ios-apps) +// + +import Foundation +import UIKit + +protocol Coordinator { + var childCoordinators: [Coordinator] { get set } + var navigationController: UINavigationController { get set } + + func start() +} + diff --git a/foray/ForayCoordinator.swift b/foray/ForayCoordinator.swift new file mode 100644 index 0000000..8fe6656 --- /dev/null +++ b/foray/ForayCoordinator.swift @@ -0,0 +1,53 @@ +// +// ForayCoordinator.swift +// foray +// +// Created by Nicholas Tay on 21/3/2022. +// + +import Foundation +import UIKit + +protocol ForayCoordinated: UIViewController { + var coordinator: ForayCoordinator? { get set } +} + +class ForayCoordinator: Coordinator { + var childCoordinators = [Coordinator]() + var navigationController: UINavigationController + + init(navigationController: UINavigationController) { + self.navigationController = navigationController + } + + private func push(vc: ForayCoordinated, animated: Bool = true) { + vc.coordinator = self + navigationController.pushViewController(vc, animated: animated) + } + + func start() { + push(vc: ForayTableViewController(), animated: false) + } + + let detailViewController = ForayDetailViewController() + + func showDetails(item: PenguinItem) { + let image: UIImage + var description: String = "Type: " + switch item.type { + case .item: + description += "Item" + image = UIImage(named: item.id)! + case .quest: + description += "Quest" + image = UIImage(named: "spy")! + } + description += "\nID: " + item.id + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + description += "\nReleased: " + dateFormatter.string(from: item.releaseDate) + + detailViewController.setDetails(name: item.name, description: description, image: image) + push(vc: detailViewController) + } +} diff --git a/foray/ForayDetailViewController.swift b/foray/ForayDetailViewController.swift index 08845df..1382d24 100644 --- a/foray/ForayDetailViewController.swift +++ b/foray/ForayDetailViewController.swift @@ -7,7 +7,9 @@ import UIKit -class ForayDetailViewController: UIViewController { +class ForayDetailViewController: UIViewController, ForayCoordinated { + + var coordinator: ForayCoordinator? let scrollView: UIScrollView = { let sv = UIScrollView() @@ -83,24 +85,10 @@ class ForayDetailViewController: UIViewController { } } - public func setSelectedItem(selectedItem: PenguinItem) { - nameLabel.text = selectedItem.name - itemImageView.image = UIImage(named: selectedItem.id) - - descLabel.text = "Type: " - switch selectedItem.type { - case .item: - descLabel.text! += "Item" - case .quest: - descLabel.text! += "Quest" - itemImageView.image = UIImage(named: "spy") - } - - descLabel.text! += "\nID: " + selectedItem.id - - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "yyyy-MM-dd" - descLabel.text! += "\nReleased: " + dateFormatter.string(from: selectedItem.releaseDate) + public func setDetails(name: String, description: String, image: UIImage) { + nameLabel.text = name + descLabel.text = description + itemImageView.image = image } } diff --git a/foray/ForayTableViewController.swift b/foray/ForayTableViewController.swift index 2732fce..827d4ea 100644 --- a/foray/ForayTableViewController.swift +++ b/foray/ForayTableViewController.swift @@ -13,7 +13,9 @@ private func firstDayOfYear(date: Date) -> Date { return calendar.date(from: components)! } -class ForayTableViewController: UITableViewController { +class ForayTableViewController: UITableViewController, ForayCoordinated { + + var coordinator: ForayCoordinator? // MARK: - Static data TEMP @@ -110,14 +112,10 @@ class ForayTableViewController: UITableViewController { return "Released in " + dateFormatter.string(from: section.year) } - let detailViewController: ForayDetailViewController = ForayDetailViewController() - override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) let item = self.sections[indexPath.section].items[indexPath.row] - detailViewController.setSelectedItem(selectedItem: item) - - self.navigationController?.pushViewController(detailViewController, animated: true) + self.coordinator?.showDetails(item: item) } } |