Advertisement
Guest User

Untitled

a guest
May 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.67 KB | None | 0 0
  1.  
  2. import UIKit
  3. import ChameleonFramework
  4. import Alamofire
  5. import AlamofireImage
  6.  
  7. extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
  8.    
  9.     //MARK: Collection View Delegate Methods
  10.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  11.         return latestMovies.count
  12.     }
  13.    
  14.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  15.        
  16.         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mCell", for: indexPath) as! MovieCollectionViewCell
  17.        
  18.         let cachedImage = imageCache.image(withIdentifier: latestMovies[indexPath.row].getFullImageURL())
  19.         if  cachedImage != nil {
  20.             cell.coverView.image = cachedImage
  21.             colorTracker = cachedImage!
  22.             self.calculateBackgroundColor() // PEPPE
  23.             //print("using the cache")
  24.         }
  25.         else {
  26.             downloadPoster(from: latestMovies[indexPath.row].getFullImageURL(), cell: cell)
  27.         }
  28.        
  29.        
  30.         cell.movieName.text = latestMovies[indexPath.row].title
  31.         cell.releaseDate.text = "Release date: " + latestMovies[indexPath.row].release_date
  32.         //setViewColors(from: colorTracker)
  33.         cell.changeCellColorAttributes(with: UIColor(averageColorFrom: colorTracker))
  34.         setShadows(to: cell)
  35.         return cell
  36.     }
  37.    
  38.     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  39.         performSegue(withIdentifier: "showDetails", sender: latestMovies[indexPath.row])
  40.     }
  41.    
  42.    
  43.     // Set the Collection View Cells to be 'always centered':
  44.     func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
  45.         let layout = self.moviesCollection.collectionViewLayout as! UICollectionViewFlowLayout
  46.         let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing
  47.         var offset = targetContentOffset.pointee
  48.         let index = (offset.x + scrollView.contentInset.left) / cellWidthIncludingSpacing
  49.         let roundedIndex = round(index)
  50.         offset = CGPoint(x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
  51.         targetContentOffset.pointee = offset
  52.     }
  53.  
  54.     func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  55.    
  56.     }
  57.    
  58.     func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  59.  
  60.     }
  61.    
  62.     //MARK: Networking & API Request
  63.     func downloadPoster(from url: String, cell: MovieCollectionViewCell) {
  64.        
  65.         Alamofire.request(url).responseImage { [weak self] (response) in
  66.             guard let self = self else { return }
  67.             guard let image = response.value else { return }
  68.            
  69.             DispatchQueue.main.async {
  70.                 cell.coverView.image = image
  71.                 self.imageCache.add(image, withIdentifier: url)
  72.                 self.colorTracker = image
  73.                 self.imagesArray.append(image)
  74.                
  75.                 self.calculateBackgroundColor() // PEPPE
  76.             }
  77.         }
  78.     }
  79.    
  80.     // PEPPE
  81.     func calculateBackgroundColor() {
  82.         DispatchQueue.main.async {
  83.             let indexPaths: [IndexPath] = self.moviesCollection.indexPathsForVisibleItems.reversed()
  84.             print(indexPaths)
  85.             print(indexPaths[0].row)
  86.             let cell = self.moviesCollection.cellForItem(at: indexPaths[0]) as! MovieCollectionViewCell
  87.             guard let image = cell.coverView.image else { return }
  88.             self.setViewColors(from: image)
  89.         }
  90.     }
  91.    
  92.     //MARK: Customization & Appearence:
  93.    
  94.     func setViewColors(from image: UIImage) {
  95.        
  96.         self.view.backgroundColor = UIColor(averageColorFrom: image)
  97.         if let backColor = self.view.backgroundColor, let navBar = navigationController?.navigationBar {
  98.             self.moviesCollection.backgroundColor = backColor
  99.             navBar.barTintColor = backColor
  100.             navBar.tintColor = ContrastColorOf(backColor, returnFlat: true)
  101.             navBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : ContrastColorOf(backColor, returnFlat: true)]
  102.         }
  103.     }
  104.    
  105.     func setShadows(to cell: MovieCollectionViewCell) {
  106.         cell.layer.cornerRadius = 3.0
  107.         cell.layer.shadowRadius = 10
  108.         cell.layer.shadowOpacity = 0.4
  109.         cell.layer.shadowOffset = CGSize(width: 5, height: 10)
  110.         cell.clipsToBounds = false
  111.     }
  112.    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement