瀏覽代碼

BaseCollectionViewCell: Add helper to compute number of columns

Refactor cellSizeForWidth to use the helper.
Edgar Fouillet 5 年之前
父節點
當前提交
bb30e5f480

+ 4 - 0
Sources/MediaCategoryCells/BaseCollectionViewCell.swift

@@ -24,6 +24,10 @@ class BaseCollectionViewCell: UICollectionViewCell {
 
     var media: VLCMLObject?
 
+    class func numberOfColumns(for width: CGFloat) -> CGFloat {
+        return CGFloat.zero
+    }
+
     class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
         return CGSize.zero
     }

+ 8 - 5
Sources/MediaCategoryCells/MediaCollectionViewCell.swift

@@ -116,15 +116,18 @@ class MediaCollectionViewCell: BaseCollectionViewCell {
         descriptionLabel.text = genre.numberOfTracksString()
     }
 
-    override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
-        let numberOfCells: CGFloat
+    override class func numberOfColumns(for width: CGFloat) -> CGFloat {
         if width <= DeviceWidth.iPhonePortrait.rawValue {
-            numberOfCells = 1
+            return 1
         } else if width <= DeviceWidth.iPadLandscape.rawValue {
-            numberOfCells = 2
+            return 2
         } else {
-            numberOfCells = 3
+            return 3
         }
+    }
+
+    override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
+        let numberOfCells: CGFloat = numberOfColumns(for: width)
 
         // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
         //

+ 9 - 6
Sources/MediaCategoryCells/MovieCollectionViewCell.swift

@@ -85,17 +85,20 @@ class MovieCollectionViewCell: BaseCollectionViewCell {
         thumbnailView.image = playlist.thumbnail()
     }
 
-    override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
-        let numberOfCells: CGFloat
+    override class func numberOfColumns(for width: CGFloat) -> CGFloat {
         if width <= DeviceWidth.iPhonePortrait.rawValue {
-            numberOfCells = 2
+            return 2
         } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
-            numberOfCells = 3
+            return 3
         } else if width <= DeviceWidth.iPadLandscape.rawValue {
-            numberOfCells = 4
+            return 4
         } else {
-            numberOfCells = 5
+            return 5
         }
+    }
+
+    override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
+        let numberOfCells: CGFloat = numberOfColumns(for: width)
         let aspectRatio: CGFloat = 10.0 / 16.0
 
         // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.

+ 8 - 5
Sources/MediaEditCell.swift

@@ -124,15 +124,18 @@ class MediaEditCell: BaseCollectionViewCell {
         }
     }
 
-    override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
-        let numberOfCells: CGFloat
+    override class func numberOfColumns(for width: CGFloat) -> CGFloat {
         if width <= DeviceWidth.iPhonePortrait.rawValue {
-            numberOfCells = 1
+            return 1
         } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
-            numberOfCells = 2
+            return 2
         } else {
-            numberOfCells = 3
+            return 3
         }
+    }
+
+    override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
+        let numberOfCells: CGFloat = numberOfColumns(for: width)
 
         // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
         //