Created
August 16, 2019 13:33
-
-
Save manishkkatoch/edea96896df3f17bdec72154528db129 to your computer and use it in GitHub Desktop.
HATEOAS main VC
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
import UIKit | |
import SwiftyJSON | |
class ViewController: UIViewController { | |
private let reuseIdentifier = "MovieCell" | |
private let httpClient = RestfulClient.shared | |
private var movies: [Resource<Movie>] = [] | |
private var selectedMovie: Resource<Movie>? | |
private var root: Resource<Root>? | |
@IBOutlet weak var libraryCollectionView: UICollectionView! { | |
didSet { | |
self.libraryCollectionView.dataSource = self | |
self.libraryCollectionView.delegate = self | |
} | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
self.title = "ACMEFLIX" | |
//entry point | |
let rootLink = Link(withUrl: "http://localhost:3000/") | |
loadResource(rootLink, onSuccess: rootIsLoaded ) | |
} | |
private func loadResource<T>(_ link: Link, onSuccess: @escaping onResourceDownloadSuccess<T>){ | |
httpClient.request(link, onSuccess, onResourceLoadFailure) | |
} | |
private func rootIsLoaded(_ resource: Resource<Root>?) { | |
root = resource | |
root?.library(onAvailable: { (link) in | |
self.loadResource(link, onSuccess: self.libraryIsLoaded) | |
}, onUnavailable: { | |
self.onResourceLoadFailure(.EMPTY_RESOURCE) | |
}) | |
root?.cart(onAvailable: { (link) in | |
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "CART", style: .done, target: self, action: #selector(self.cartTapped(_:))) | |
}, onUnavailable: { | |
self.navigationItem.rightBarButtonItem = nil | |
}) | |
} | |
@objc private func cartTapped(_ sender: Any) { | |
performSegue(withIdentifier: "showCart", sender: self) | |
} | |
private func libraryIsLoaded(_ resource: Resource<Library>?) { | |
self.movies = resource?.getMovies() ?? [] | |
let syncDispatch = DispatchGroup() | |
movies.forEach({ (movie) in | |
if let poster = movie.getPosterLink(), let movieItem = movie.item { | |
syncDispatch.enter() | |
httpClient.requestRaw(poster) {(data, status) in | |
PosterCache.shared.addToCache(forKey: movieItem.id, poster: Poster(fromData: data)) | |
syncDispatch.leave() | |
} | |
} | |
}) | |
syncDispatch.notify(queue: .main) { | |
self.libraryCollectionView.reloadData() | |
} | |
} | |
private func onResourceLoadFailure(_ status: HttpResponseCode) { | |
self.performSegue(withIdentifier: "onFailLoad", sender: self) | |
} | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
if segue.identifier == "onMovieSelect" { | |
if let vc = segue.destination as? MovieViewController, let movie = selectedMovie { | |
vc.movie = movie | |
} | |
} | |
else if segue.identifier == "showCart" { | |
if let vc = segue.destination as? CartViewController, let cartLink = root?.getCartLink() { | |
vc.cartLink = cartLink | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment