From 12abbe7e6ea257fa1686a2ef24fbf12009adcb8d Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 21 Mar 2022 15:13:23 +1100 Subject: First try at implementing coordinator pattern Resources: - https://www.hackingwithswift.com/articles/71/how-to-use-the-coordinator-pattern-in-ios-apps --- foray/ForayCoordinator.swift | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 foray/ForayCoordinator.swift (limited to 'foray/ForayCoordinator.swift') 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) + } +} -- cgit