Browse Source

MediaCells: added dynamic sizes that are retrieved from the cells

Carola Nitz 6 years ago
parent
commit
e119078bbe

+ 16 - 1
MediaCategoryCells/AudioCollectionViewCell.swift

@@ -18,6 +18,9 @@ class AudioCollectionViewCell: BaseCollectionViewCell {
     @IBOutlet weak var titleLabel: UILabel!
     @IBOutlet weak var descriptionLabel: UILabel!
     @IBOutlet weak var newLabel: UILabel!
+    override class var cellPadding: CGFloat {
+        return 5.0
+    }
 
     override var media: VLCMLObject? {
         didSet {
@@ -45,13 +48,25 @@ class AudioCollectionViewCell: BaseCollectionViewCell {
 
     func update(audiotrack: VLCMLMedia) {
         titleLabel.text = audiotrack.title
-        descriptionLabel.text = audiotrack.mediaDuration()
+        descriptionLabel.text = audiotrack.albumTrack.artist.name
         if audiotrack.isThumbnailGenerated() {
             thumbnailView.image = UIImage(contentsOfFile: audiotrack.thumbnail.absoluteString)
         }
         newLabel.isHidden = !audiotrack.isNew()
     }
 
+    override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
+        let numberOfCells: CGFloat = round(width / 320)
+
+        // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
+        // we then have the entire padding, we divide the entire padding by the number of Cells to know how much needs to be substracted from ech cell
+        // since this might be an uneven number we ceil
+        var cellWidth = width / numberOfCells
+        cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
+
+        return CGSize(width: cellWidth, height: 80)
+    }
+
     override func prepareForReuse() {
         super.prepareForReuse()
         titleLabel.text = ""

+ 8 - 0
MediaCategoryCells/BaseCollectionViewCell.swift

@@ -23,4 +23,12 @@ class BaseCollectionViewCell: UICollectionViewCell {
     }
 
     var media: VLCMLObject?
+
+    class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
+        return CGSize.zero
+    }
+
+    class var cellPadding: CGFloat {
+        return 0
+    }
 }

+ 17 - 0
MediaCategoryCells/MovieCollectionViewCell.swift

@@ -19,6 +19,9 @@ class MovieCollectionViewCell: BaseCollectionViewCell {
     @IBOutlet weak var newLabel: UILabel!
     @IBOutlet weak var descriptionLabel: UILabel!
     @IBOutlet weak var progressView: UIProgressView!
+    override class var cellPadding: CGFloat {
+        return 5.0
+    }
 
     override var media: VLCMLObject? {
         didSet {
@@ -56,6 +59,20 @@ class MovieCollectionViewCell: BaseCollectionViewCell {
         newLabel.isHidden = !movie.isNew()
     }
 
+    override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
+        let numberOfCells: CGFloat = round(width / 250)
+        let aspectRatio: CGFloat = 10.0 / 16.0
+
+        // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
+        // we then have the entire padding, we divide the entire padding by the number of Cells to know how much needs to be substracted from ech cell
+        // since this might be an uneven number we ceil
+        var cellWidth = width / numberOfCells
+        cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
+
+        // 3*20 for the labels + 24 for 3*8 which is the padding
+        return CGSize(width: cellWidth, height: cellWidth * aspectRatio + 3*20+24)
+    }
+
     override func prepareForReuse() {
         super.prepareForReuse()
         titleLabel.text = ""

+ 9 - 16
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -14,12 +14,13 @@
 import Foundation
 
 class VLCMediaCategoryViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchResultsUpdating, UISearchControllerDelegate, IndicatorInfoProvider {
-    let cellPadding: CGFloat = 5.0
+    
     private var services: Services
     private var searchController: UISearchController?
     private let searchDataSource = VLCLibrarySearchDisplayDataSource()
     var category: MediaLibraryBaseModel
     private lazy var editController = VLCEditController(collectionView: self.collectionView!, category: self.category)
+    private var cachedCellSize = CGSize.zero
 
 //    @available(iOS 11.0, *)
 //    lazy var dragAndDropManager: VLCDragAndDropManager = { () -> VLCDragAndDropManager<T> in
@@ -213,30 +214,22 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
 
     // MARK: - UICollectionViewDelegateFlowLayout
     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
-
-        let numberOfCells: CGFloat = round(collectionView.frame.size.width / 250)
-        let aspectRatio: CGFloat = 10.0 / 16.0
-
-        // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
-        // we then have the entire padding, we divide the entire padding by the number of Cells to know how much needs to be substracted from ech cell
-        // since this might be an uneven number we ceil
-        var cellWidth = collectionView.bounds.size.width / numberOfCells
-        cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
-
-        // 3*20 for the labels + 24 for 3*8 which is the padding
-        return CGSize(width: cellWidth, height: cellWidth * aspectRatio + 3*20+24)
+        if cachedCellSize == .zero {
+            cachedCellSize = category.cellType.cellSizeForWidth(collectionView.frame.size.width)
+        }
+        return cachedCellSize
     }
 
     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
-        return UIEdgeInsets(top: cellPadding, left: cellPadding, bottom: cellPadding, right: cellPadding)
+        return UIEdgeInsets(top: category.cellType.cellPadding, left: category.cellType.cellPadding, bottom: category.cellType.cellPadding, right: category.cellType.cellPadding)
     }
 
     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
-        return cellPadding
+        return category.cellType.cellPadding
     }
 
     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
-        return cellPadding
+        return category.cellType.cellPadding
     }
 }