Skip to content

Instantly share code, notes, and snippets.

@sidraval
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidraval/614cd09b5c6b3385d634 to your computer and use it in GitHub Desktop.
Save sidraval/614cd09b5c6b3385d634 to your computer and use it in GitHub Desktop.
ProductController
// ProductDelegate
import UIKit
protocol ProductDelegate: NSObjectProtocol {
func didReceiveProduct(product: Product)
func didReceiveProductImage(image: UIImage)
}
// ProductController
struct ProductController {
let product: Product
let productFetcher: ProductFetcher
weak var productDelegate: ProductDelegate?
init(product: Product, productDelegate: ProductDelegate, productFetcher: ProductFetcher = ProductClient()) {
self.product = product
self.productFetcher = productFetcher
self.productDelegate = productDelegate
}
func fetchProductData() {
productFetcher.fetchDataFor(product: product) { product in
self.productDelegate?.didReceiveProduct(product)
}
}
func fetchProductImage(imageUrl: String?) {
if let url = imageUrl {
productFetcher.fetchImageFor(url) { image in
self.productDelegate?.didReceiveProductImage(image)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment