Skip to content

Instantly share code, notes, and snippets.

View froggomad's full-sized avatar

Kenny Dubroff (froggomad) froggomad

View GitHub Profile
enum Produce: String {
case fruit
case vegetable
var description: String {
rawValue.capitalized
}
}
enum Fruit: String {
case apple
case orange
case dragonFruit
var description: String {
var name: String = ""
for char in String(describing: rawValue) {
let strChar = String(char)
struct ProduceViewModel {
let description: String
}
enum Fruit: String {
case apple
case orange
case dragonFruit
var description: String {
var name: String = ""
for char in String(describing: rawValue) {
let strChar = String(char)
class ProduceCollectionViewCell: UICollectionViewCell {
static let identifier = "ProduceCollectionViewCell"
var viewModel: ProduceViewModel? {
didSet {
titleLabel.text = viewModel?.description
}
}
lazy var titleLabel: UILabel = {
import UIKit
class TitledCollectionView: UIView {
var produce: Produce? {
didSet {
titleLabel.text = produce?.description
}
}
extension TitledCollectionView: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
produce?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ProduceCollectionViewCell.identifier, for: indexPath) as! ProduceCollectionViewCell
extension TitledCollectionView {
class Layout: UICollectionViewFlowLayout {
static let heightConstant: CGFloat = 150
override func prepare() {
super.prepare()
guard let collectionView = collectionView else { return }
// TODO: why -13 to make the size even? section insets?
itemSize = CGSize(width: collectionView.frame.width / 3 - 13, height: Self.heightConstant - 40)
class TitledCollectionViewController: UIViewController {
lazy var fruitView = TitledCollectionView(produce: .fruit)
lazy var vegetableView = TitledCollectionView(produce: .vegetable)
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
protocol TitledCollectionViewDelegate: AnyObject {
func itemWasSelected(_ fruit: Fruit)
func itemWasSelected(_ vegetable: Vegetable)
}