|
// HomeViewController.swift
// PaiAi//
// Created by zhengjianfei on 16/3/28.
// Copyright © 2016年 FFIB. All rights reserved.
//
import UIKit
import MapKit
import ObjectMapper
import RxSwift
import RxCocoa
import RxDataSources
import ESPullToRefresh
protocol HomeViewControllerDelegate: class {
func scanQR()
func createGroup()
func didSelect(_ item: PhotoItem)
}
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!
// MARK: data property
var homeViewModel = HomeViewModel()
let disposeBag = DisposeBag()
weak var delegate: HomeViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem?.isEnabled = false
collectionView.register(UINib(nibName: "PhotoCell", bundle: nil),
forCellWithReuseIdentifier: "photoCell")
configureRxDataSource()
addReloadControl()
addLoadingControl()
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.navigationBar.setBackgroundImage(UIImage(named: "标题栏 copy 2"), for: .default)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
guard SharedUserInfo.isLogin else {
let ctl = UIStoryboard.mainBoard.instantiateController(LoginController.self)
navigationController?.present(ctl, animated: false, completion: nil)
return
}
}
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)
return cell
})
}
func configureRxDataSource() {
collectionView.rx.setDelegate(self).disposed(by: disposeBag)
homeViewModel.isReload.subscribe(onNext: {
[weak self] _ in
guard let `self` = self else { return }
self.collectionView.es.stopPullToRefresh()
}).disposed(by: disposeBag)
homeViewModel.isloading.subscribe(onNext: {
[weak self] _ in
guard let `self` = self else { return }
self.collectionView.es.stopLoadingMore()
}).disposed(by: disposeBag)
homeViewModel.homePhotoContents
.bind(to: collectionView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
collectionView.rx.willDisplayCell
.subscribe(onNext: { [unowned self] in self.homeViewModel.preload(indexPath: $0.at) })
.disposed(by: disposeBag)
collectionView.rx.modelSelected(PhotoItem.self)
.subscribe(onNext: { [unowned self] in self.delegate?.didSelect($0) })
.disposed(by: disposeBag)
collectionView.collectionViewLayout = WaterfallFlowLayout()
}
func addReloadControl() {
collectionView.es.addPullToRefresh {
[unowned self] in
self.homeViewModel.reload()
}
collectionView.es.startPullToRefresh()
}
func addLoadingControl() {
collectionView.es.addInfiniteScrolling {}
}
@IBAction func createGroupAction(_ sender: UIButton) {
self.delegate?.createGroup()
}
@IBAction func scanAction(_ sender: UIButton) {
self.delegate?.scanQR()
}
}
extension HomeViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return homeViewModel.layoutSizeForItem(indexPath)
}
}
|