Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import UIKit
import PKHUD
class WorkoutsController: UICollectionViewController {
// MARK: - Properties
fileprivate let reuseIdentifier = "WorkoutCell"
fileprivate let itemsPerRow: CGFloat = 2
fileprivate let sectionInsets = UIEdgeInsets(top: 0, left: 0, bottom: 00, right: 0)
fileprivate let titleHeightAdjustement: CGFloat = 65.0
let viewModel: HomeWorkoutsCollectionViewViewModel = HomeWorkoutsCollectionViewViewModel()
override func viewDidLoad() {
super.viewDidLoad()
bindViewModel()
viewModel.getHomeWorkouts()
}
func bindViewModel() {
viewModel.workoutCells.bindAndFire() { [weak self] _ in
self?.collectionView?.reloadData()
}
viewModel.onShowError = { [weak self] alert in
self?.presentSingleButtonDialog(alert: alert)
}
viewModel.showLoadingHud.bind() { visible in
PKHUD.sharedHUD.contentView = PKHUDSystemActivityIndicatorView()
visible ? PKHUD.sharedHUD.show() : PKHUD.sharedHUD.hide()
}
}
}
// MARK: - UICollectionViewDataSource
extension WorkoutsController {
override func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.viewModel.workoutCells.value.count
}
override func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch viewModel.workoutCells.value[indexPath.row] {
case .normal(let viewModel):
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? HomeWorkoutCollectionViewCell else {
return UICollectionViewCell()
}
cell.viewModel = viewModel
return cell
case .error(let message):
let cell = UICollectionViewCell()
//cell.textLabel?.text = message
return cell
case .empty:
let cell = UICollectionViewCell()
//cell.textLabel?.text = "No data available"
return cell
}
}
}
// MARK: - UICollectionViewDelegateFlowLayout
extension WorkoutsController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let widthPerItem = view.frame.width / itemsPerRow
return CGSize(width: widthPerItem, height: widthPerItem + titleHeightAdjustement)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return sectionInsets
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return sectionInsets.left
}
}
extension WorkoutsController: SingleButtonDialogPresenter { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.