暂无描述

HomeViewController.swift 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // HomeViewController.swift
  2. // PaiAi//
  3. // Created by zhengjianfei on 16/3/28.
  4. // Copyright © 2016年 FFIB. All rights reserved.
  5. //
  6. import UIKit
  7. import RxSwift
  8. import RxCocoa
  9. import RxDataSources
  10. import PaiaiDataKit
  11. import PaiaiUIKit
  12. import PullToRefresh
  13. final class HomeViewController: UIViewController {
  14. @IBOutlet weak var scanBtn: UIButton!
  15. @IBOutlet weak var createBtn: UIButton!
  16. @IBOutlet weak var scanLabel: UILabel!
  17. @IBOutlet weak var createLabel: UILabel!
  18. @IBOutlet weak var collectionView: UICollectionView!
  19. // MARK: data property
  20. fileprivate let disposeBag = DisposeBag()
  21. internal var viewModel: HomeViewModel!
  22. internal var userInfoViewModel: UserInfoViewModel!
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. initalize()
  26. }
  27. func initalize() {
  28. collectionView.register(UINib(nibName: "PhotoCell",
  29. bundle: Bundle(identifier: "com.Paiai-iOS")),
  30. forCellWithReuseIdentifier: "photoCell")
  31. setup()
  32. binding()
  33. }
  34. private func setup() {
  35. setupReloadControl()
  36. setupLoadingControl()
  37. }
  38. private func setupReloadControl() {
  39. collectionView.addPullToRefresh(PullToRefresh()) {
  40. [unowned self] in
  41. self.viewModel.reload()
  42. }
  43. }
  44. private func setupLoadingControl() {
  45. collectionView.addPullToRefresh(PullToRefresh(position: .bottom)) {
  46. [unowned self] in
  47. self.viewModel.preload()
  48. }
  49. }
  50. }
  51. /// UI bindings
  52. fileprivate extension HomeViewController {
  53. var dataSource: RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>> {
  54. return RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>>(configureCell:
  55. { (dataSource, collectionView, indexPath, item) -> UICollectionViewCell in
  56. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCell
  57. cell.setInfo(item, source: .home)
  58. return cell
  59. })
  60. }
  61. func binding() {
  62. bindInteraction()
  63. bindViewModelToRefreshing()
  64. bindCollectionViewDelegate()
  65. bindUserInfoViewModelToView()
  66. bindViewModelToCollectionView()
  67. bindCollectionViewToViewModel()
  68. }
  69. func bindInteraction() {
  70. createBtn.rx.tap.bind(to: viewModel.createBtnTapped).disposed(by: disposeBag)
  71. scanBtn.rx.tap.bind(to: viewModel.scanBtnTapped).disposed(by: disposeBag)
  72. }
  73. func bindViewModelToRefreshing() {
  74. viewModel.isLoading
  75. .subscribe(onNext: {[unowned self] flag in
  76. if flag {
  77. self.collectionView.endRefreshing(at: .top)
  78. } else {
  79. self.collectionView.endRefreshing(at: .bottom)
  80. }
  81. }).disposed(by: disposeBag)
  82. }
  83. func bindCollectionViewDelegate() {
  84. collectionView.rx.setDelegate(self).disposed(by: disposeBag)
  85. }
  86. func bindUserInfoViewModelToView() {
  87. userInfoViewModel.isLoggedIn
  88. .asDriver(onErrorJustReturn: ())
  89. .drive(onNext: { (_) in
  90. self.collectionView.startRefreshing(at: .top)
  91. }).disposed(by: disposeBag)
  92. }
  93. func bindViewModelToCollectionView() {
  94. viewModel.contents
  95. .bind(to: collectionView.rx.items(dataSource: dataSource))
  96. .disposed(by: disposeBag)
  97. }
  98. func bindCollectionViewToViewModel() {
  99. collectionView.rx.willDisplayCell
  100. .asDriver()
  101. .drive(onNext: { [unowned self] in self.preload(indexPath: $0.at) })
  102. .disposed(by: disposeBag)
  103. collectionView.rx.modelSelected(PhotoItem.self)
  104. .asDriver()
  105. .drive(onNext: { [unowned self] in self.viewModel.didSelect($0) })
  106. .disposed(by: disposeBag)
  107. }
  108. func preload(indexPath: IndexPath) {
  109. guard indexPath.row == collectionView.numberOfItems(inSection: 0) - 5 else { return }
  110. collectionView.startRefreshing(at: .bottom)
  111. }
  112. }
  113. extension HomeViewController: UICollectionViewDelegateFlowLayout {
  114. func collectionView(_ collectionView: UICollectionView,
  115. layout collectionViewLayout: UICollectionViewLayout,
  116. sizeForItemAt indexPath: IndexPath) -> CGSize {
  117. return viewModel.layoutSizeForIndexPath(indexPath)
  118. }
  119. }