Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import ChameleonFramework
- import Alamofire
- import AlamofireImage
- extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
- //MARK: Collection View Delegate Methods
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return latestMovies.count
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mCell", for: indexPath) as! MovieCollectionViewCell
- let cachedImage = imageCache.image(withIdentifier: latestMovies[indexPath.row].getFullImageURL())
- if cachedImage != nil {
- cell.coverView.image = cachedImage
- colorTracker = cachedImage!
- self.calculateBackgroundColor() // PEPPE
- //print("using the cache")
- }
- else {
- downloadPoster(from: latestMovies[indexPath.row].getFullImageURL(), cell: cell)
- }
- cell.movieName.text = latestMovies[indexPath.row].title
- cell.releaseDate.text = "Release date: " + latestMovies[indexPath.row].release_date
- //setViewColors(from: colorTracker)
- cell.changeCellColorAttributes(with: UIColor(averageColorFrom: colorTracker))
- setShadows(to: cell)
- return cell
- }
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- performSegue(withIdentifier: "showDetails", sender: latestMovies[indexPath.row])
- }
- // Set the Collection View Cells to be 'always centered':
- func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
- let layout = self.moviesCollection.collectionViewLayout as! UICollectionViewFlowLayout
- let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing
- var offset = targetContentOffset.pointee
- let index = (offset.x + scrollView.contentInset.left) / cellWidthIncludingSpacing
- let roundedIndex = round(index)
- offset = CGPoint(x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
- targetContentOffset.pointee = offset
- }
- func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
- }
- func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
- }
- //MARK: Networking & API Request
- func downloadPoster(from url: String, cell: MovieCollectionViewCell) {
- Alamofire.request(url).responseImage { [weak self] (response) in
- guard let self = self else { return }
- guard let image = response.value else { return }
- DispatchQueue.main.async {
- cell.coverView.image = image
- self.imageCache.add(image, withIdentifier: url)
- self.colorTracker = image
- self.imagesArray.append(image)
- self.calculateBackgroundColor() // PEPPE
- }
- }
- }
- // PEPPE
- func calculateBackgroundColor() {
- DispatchQueue.main.async {
- let indexPaths: [IndexPath] = self.moviesCollection.indexPathsForVisibleItems.reversed()
- print(indexPaths)
- print(indexPaths[0].row)
- let cell = self.moviesCollection.cellForItem(at: indexPaths[0]) as! MovieCollectionViewCell
- guard let image = cell.coverView.image else { return }
- self.setViewColors(from: image)
- }
- }
- //MARK: Customization & Appearence:
- func setViewColors(from image: UIImage) {
- self.view.backgroundColor = UIColor(averageColorFrom: image)
- if let backColor = self.view.backgroundColor, let navBar = navigationController?.navigationBar {
- self.moviesCollection.backgroundColor = backColor
- navBar.barTintColor = backColor
- navBar.tintColor = ContrastColorOf(backColor, returnFlat: true)
- navBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : ContrastColorOf(backColor, returnFlat: true)]
- }
- }
- func setShadows(to cell: MovieCollectionViewCell) {
- cell.layer.cornerRadius = 3.0
- cell.layer.shadowRadius = 10
- cell.layer.shadowOpacity = 0.4
- cell.layer.shadowOffset = CGSize(width: 5, height: 10)
- cell.clipsToBounds = false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement