123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import UIKit
- import RxSwift
- import RxCocoa
- import RxDataSources
- import PaiaiDataKit
- import PaiaiUIKit
- import PullToRefresh
- final class HomeViewController: UIViewController {
- @IBOutlet weak var scanBtn: UIButton!
- @IBOutlet weak var createBtn: UIButton!
- @IBOutlet weak var scanLabel: UILabel!
- @IBOutlet weak var createLabel: UILabel!
- @IBOutlet weak var collectionView: UICollectionView!
-
- fileprivate let disposeBag = DisposeBag()
-
- internal var viewModel: HomeViewModel!
- internal var userInfoViewModel: UserInfoViewModel!
- override func viewDidLoad() {
- super.viewDidLoad()
- initalize()
- }
-
- func initalize() {
- collectionView.register(UINib(nibName: "PhotoCell",
- bundle: Bundle(identifier: "com.Paiai-iOS")),
- forCellWithReuseIdentifier: "photoCell")
-
- setup()
- binding()
- }
-
- private func setup() {
- setupReloadControl()
- setupLoadingControl()
- }
-
- private func setupReloadControl() {
- collectionView.addPullToRefresh(PullToRefresh()) {
- [unowned self] in
- self.viewModel.reload()
- }
- }
-
- private func setupLoadingControl() {
- collectionView.addPullToRefresh(PullToRefresh(position: .bottom)) {
- [unowned self] in
- self.viewModel.preload()
- }
- }
- }
- fileprivate extension HomeViewController {
-
- var dataSource: RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>> {
- return RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>>(configureCell:
- { (dataSource, collectionView, indexPath, item) -> UICollectionViewCell in
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCell
- cell.setInfo(item, source: .home)
- return cell
- })
- }
-
- func binding() {
- bindInteraction()
- bindViewModelToRefreshing()
- bindCollectionViewDelegate()
- bindUserInfoViewModelToView()
- bindViewModelToCollectionView()
- bindCollectionViewToViewModel()
- }
-
- func bindInteraction() {
- createBtn.rx.tap.bind(to: viewModel.createBtnTapped).disposed(by: disposeBag)
- scanBtn.rx.tap.bind(to: viewModel.scanBtnTapped).disposed(by: disposeBag)
- }
-
- func bindViewModelToRefreshing() {
- viewModel.isLoading
- .subscribe(onNext: {[unowned self] flag in
- if flag {
- self.collectionView.endRefreshing(at: .top)
- } else {
- self.collectionView.endRefreshing(at: .bottom)
- }
- }).disposed(by: disposeBag)
- }
-
- func bindCollectionViewDelegate() {
- collectionView.rx.setDelegate(self).disposed(by: disposeBag)
- }
-
- func bindUserInfoViewModelToView() {
- userInfoViewModel.isLoggedIn
- .asDriver(onErrorJustReturn: ())
- .drive(onNext: { (_) in
- self.collectionView.startRefreshing(at: .top)
- }).disposed(by: disposeBag)
- }
-
- func bindViewModelToCollectionView() {
- viewModel.contents
- .bind(to: collectionView.rx.items(dataSource: dataSource))
- .disposed(by: disposeBag)
- }
-
- func bindCollectionViewToViewModel() {
- collectionView.rx.willDisplayCell
- .asDriver()
- .drive(onNext: { [unowned self] in self.preload(indexPath: $0.at) })
- .disposed(by: disposeBag)
-
- collectionView.rx.modelSelected(PhotoItem.self)
- .asDriver()
- .drive(onNext: { [unowned self] in self.viewModel.didSelect($0) })
- .disposed(by: disposeBag)
- }
-
- func preload(indexPath: IndexPath) {
- guard indexPath.row == collectionView.numberOfItems(inSection: 0) - 5 else { return }
- collectionView.startRefreshing(at: .bottom)
- }
- }
- extension HomeViewController: UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView,
- layout collectionViewLayout: UICollectionViewLayout,
- sizeForItemAt indexPath: IndexPath) -> CGSize {
- return viewModel.layoutSizeForIndexPath(indexPath)
- }
- }
|