// // SportDetailViewController.swift // Lookfit // // Created by lemo. on 2020/4/13. // Copyright © 2020 Sheldon. All rights reserved. // import UIKit fileprivate struct Metric { static let indexItemSize: CGSize = CGSize(width: (kScreenW - 80) / 2.0, height: kScaleHeight(83)) } fileprivate enum Resuable { static let headerView = ReusableView() static let headerHeight = kScaleHeight(193) static let gpsHeaderHeight = kScaleHeight(193 + 380) static let sportDetailCell = ReusableCell(nibName: "SportDetailCell") } class SportDetailViewController: ViewController { private lazy var headerView: SportDetailHeaderView = { let view = SportDetailHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: kScaleHeight(193) + kScaleHeight(380))) view.backgroundColor = .white return view }() private lazy var rxDataSource: RxCollectionViewSectionedReloadDataSource = { return RxCollectionViewSectionedReloadDataSource>( configureCell: { (dataSource, collectionView, indexPath, element) in let cell = collectionView.dequeue(Resuable.sportDetailCell, for: indexPath) cell.bind(to: element) return cell }, configureSupplementaryView: { [weak self] (_, cv, kind, ip) in let headerView = cv.dequeue(Resuable.headerView, kind: .header, for: ip) guard let `self` = self else { return headerView } if self.headerView.superview == nil { headerView.frame = self.headerView.frame headerView.addSubview(self.headerView) } self.flowLayout.headerReferenceSize = CGSize(width: kScreenW, height: self.headerView.height) return headerView }) }() private lazy var flowLayout: UICollectionViewFlowLayout = { let flowLayout = UICollectionViewFlowLayout().then { $0.itemSize = Metric.indexItemSize $0.sectionInset = UIEdgeInsets(top: 0, left: 39, bottom: 0, right: 39) } flowLayout.minimumInteritemSpacing = 0 flowLayout.minimumLineSpacing = 0 flowLayout.headerReferenceSize = CGSize(width: kScreenW, height: Resuable.headerHeight) return flowLayout }() private lazy var collectionView: UICollectionView = { let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout).then({ $0.backgroundColor = .white $0.showsVerticalScrollIndicator = false $0.register(Resuable.headerView, kind: .header) $0.register(Resuable.sportDetailCell) }) return collectionView }() override func viewDidLoad() { super.viewDidLoad() } override func makeUI() { super.makeUI() view.backgroundColor = kHexColor(0xFAFAFA) view.addSubview(collectionView) layoutUI() } func layoutUI() { collectionView.snp.makeConstraints { (make) in make.edges.equalToSuperview() } } override func bindViewModel() { super.bindViewModel() guard let viewModel = viewModel as? SportDetailViewModel else { return } let output = viewModel.transform(input: SportDetailViewModel.Input()) output.items .bind(to: collectionView.rx.items(dataSource: rxDataSource)) .disposed(by: rx.disposeBag) if let leftItemBtn = navigationItem.leftBarButtonItem?.customView as? UIButton { output.sportName .drive(leftItemBtn.rx.title(for: .normal)) .disposed(by: rx.disposeBag) } output.time .drive(headerView.timeLabel.rx.text) .disposed(by: rx.disposeBag) output.distance .drive(headerView.distanceLabel.rx.text) .disposed(by: rx.disposeBag) output.distanceUnit .drive(headerView.distanceUnitLabel.rx.text) .disposed(by: rx.disposeBag) output.trajectory .drive(headerView.rx.trajectory()) .disposed(by: rx.disposeBag) } }