Skip to content

Instantly share code, notes, and snippets.

@lanserxt
Created May 17, 2023 12:15
Show Gist options
  • Save lanserxt/7ad69f413f7215cbfd3ade005e954ff2 to your computer and use it in GitHub Desktop.
Save lanserxt/7ad69f413f7215cbfd3ade005e954ff2 to your computer and use it in GitHub Desktop.
UICollection model extension to support extra model
//We have a UICollectionView with Grid layout that shows a collection card.
//Model for a card looks like this:
struct CollectionViewCellModel: Codable {
let id: Int
let image: String
let imageUrl: String
let name: String
let description: String
let stocksAmount: Int
let dailyGrow: Float
}
//Our RecommendedCollectionViewCell is configured using it directly
final class RecommendedCollectionViewCell: UICollectionViewCell {
func configureWith(model: CollectionViewCellModel) {
}
}
//And the ViewControlelr looks like this
final class CollectionViewController: UIViewController {
//Models array aka SoT
private var models: [CollectionViewCellModel] = []
//Let's skip UICollectionViewFlowLayout definition
private let recCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
var collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.register(RecommendedCollectionViewCell.self)
collectionView.showsVerticalScrollIndicator = false
return collectionView
}()
}
//After some time we need to show not just collection model but a stock in a collection view
struct StockCellModel: Codable {
let id: Int
let name: String
let description: String
let dailyGrow: Float
}
//How would you change homogeneous array into heterogeneous one and adapt what approach for Cell would you use?
//Will it be 1 cell with layout switches? 2 cells?
//How to split models in same array and pick correct type to render?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment