Skip to content

Instantly share code, notes, and snippets.

@viniciussoares
Created August 8, 2019 14:44
Show Gist options
  • Save viniciussoares/388f25241b1a2f4ed25643f2f02ad61c to your computer and use it in GitHub Desktop.
Save viniciussoares/388f25241b1a2f4ed25643f2f02ad61c to your computer and use it in GitHub Desktop.
Dynamic dashboard
enum DashboardCellDisplay {
case account
case transfer
case payment
case statment
case card
case contacts
var title: String? {
switch self {
case .transfer: return "Trasnferencias"
case .payment: return "Pagamentos"
case .statment: return "Extrato"
case .card: return "Cartão"
case .contacts: return "Contatos"
default: return nil
}
}
}
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var flowLayout: UICollectionViewFlowLayout! {
didSet {
flowLayout.scrollDirection = .vertical
flowLayout.sectionInset = UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding)
flowLayout.minimumLineSpacing = padding
flowLayout.minimumInteritemSpacing = padding
}
}
private let padding: CGFloat = 8
private lazy var items: [DashboardCellDisplay] = {
return [.account, .transfer, .payment, .statment, .card, .contacts]
}()
var updateBalanceRef: ((Double) -> Void)?
var toggleBalanceRef: (() -> Void)?
}
extension ViewController {
override func viewDidLoad() {
super.viewDidLoad()
collectionView.backgroundColor = #colorLiteral(red: 0.8980392157, green: 0.8980392157, blue: 0.8980392157, alpha: 1)
collectionView.register(DashboardAccountCell.self, forCellWithReuseIdentifier: "DashboardAccountCell")
collectionView.register(DashboardCell.self, forCellWithReuseIdentifier: "DashboardCell")
}
}
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let item = items[indexPath.row]
switch item {
case .account:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DashboardAccountCell", for: indexPath) as! DashboardAccountCell
updateBalanceRef = cell.updateBalanceRef
toggleBalanceRef = cell.toggleBalanceRef
return cell
case .transfer, .payment, .statment, .card, .contacts:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DashboardCell", for: indexPath) as! DashboardCell
return cell.renderProps(icon: nil, title: item.title, action: { self.onClick(item: item) })
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let avaliableWidth = collectionView.bounds.size.width - (padding * 2)
switch items[indexPath.row] {
case .account:
return CGSize(width: avaliableWidth, height: 90)
case .transfer, .payment, .statment, .card, .contacts:
return CGSize(width: (avaliableWidth - padding) / 2, height: 90)
}
}
private func onClick(item: DashboardDisplay) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment