Last active
December 20, 2018 05:36
-
-
Save sarunw/8ab7a0799ab1aea894ee5304c1df058e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
protocol ProductListViewControllerDelegate: class { | |
func productListViewController(_ vc: ProductListViewController, didSelectProduct product: Product) | |
} | |
class ProductListViewController: UIViewController { | |
weak var delegate: ProductListViewControllerDelegate? | |
} | |
protocol ProductDetailViewControllerDelegate: class { | |
func productDetailViewController(_ vc: ProductDetailViewController, didAddProduct product: Product) | |
} | |
class ProductDetailViewController: UIViewController { | |
weak var delegate: ProductDetailViewControllerDelegate? | |
} | |
protocol OrderSummaryViewControllerDelegate: class { | |
func orderSummaryViewControllerDidCheckout(_ vc: OrderSummaryViewController) | |
} | |
class OrderSummaryViewController: UIViewController { | |
var items: [Cart.Item]! | |
weak var delegate: OrderSummaryViewControllerDelegate? | |
} | |
// 2 | |
struct Product { | |
let id: String | |
let name: String | |
let price: Decimal | |
} | |
class Cart { | |
struct Item { | |
let product: Product | |
var quantity: Int | |
} | |
var items = [Item]() | |
func addProduct(_ product: Product) {} | |
func removeProduct(_ product: Product) {} | |
} | |
// 3 | |
class PurchaseCoordinator: ProductListViewControllerDelegate, ProductDetailViewControllerDelegate, OrderSummaryViewControllerDelegate { | |
var cart = Cart() | |
var paymentMethod: PaymentMethod? | |
// Removed this in new implementation | |
var deliveryAddress: String? | |
var shippingOption: ShippingOption? | |
var rootViewController: UINavigationController | |
init() { | |
let vc = ProductListViewController() | |
rootViewController = UINavigationController(rootViewController: vc) | |
vc.delegate = self | |
} | |
// MARK: - Product list | |
// MARK: - Go to product detail | |
func productListViewController(_ vc: ProductListViewController, didSelectProduct product: Product) { | |
let vc = ProductDetailViewController() | |
vc.delegate = self | |
vc.navigationItem.rightBarButtonItem = checkoutButton | |
rootViewController.pushViewController(vc, animated: true) | |
} | |
private var checkoutButton: UIBarButtonItem { | |
return UIBarButtonItem(title: "Cart (\(cart.items.count))", style: .plain, target: self, action: #selector(didTapCheckout(_:))) | |
} | |
// MARK: - Go to order summary | |
@objc func didTapCheckout(_ sender: UIBarButtonItem) { | |
let vc = OrderSummaryViewController() | |
vc.items = cart.items | |
vc.delegate = self | |
vc.navigationItem.leftBarButtonItem = closeButton | |
rootViewController.present(UINavigationController(rootViewController: vc), animated: true, completion: nil) | |
} | |
// MARK: - Product detail | |
// MARK: - Add product | |
func productDetailViewController(_ vc: ProductDetailViewController, didAddProduct product: Product) { | |
cart.addProduct(product) | |
} | |
// MARK: - Order Summary | |
func orderSummaryViewControllerDidCheckout(_ vc: OrderSummaryViewController) { | |
// Go to delivery or shipping options | |
} | |
// MARK: - Helpers | |
private var closeButton: UIBarButtonItem { | |
return UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didTapClose(_:))) | |
} | |
@objc func didTapClose(_ sender: UIBarButtonItem) { | |
rootViewController.dismiss(animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment