|
//
// GroupViewController.swift
// PaiAi
//
// Created by zhengjianfei on 16/3/28.
// Copyright © 2016年 FFIB. All rights reserved.
//
import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import PaiaiUIKit
import PaiaiDataKit
import PullToRefresh
final class GroupViewController: UIViewController {
// MARK: Storyboard property
@IBOutlet var collectionView: UICollectionView!
@IBOutlet weak var photographBtn: UIButton!
// MARK: custom UI property
var navigationBarView: UIView = {
let view = UIView()
view.alpha = 0
return view
}()
var navigationBarViewTitle: UILabel = {
let label = UILabel()
label.textColor = UIColor.white
return label
}()
var navigationBarViewImage: UIImageView = {
let image = UIImageView()
image.cornerRadius = 20
return image
}()
// MARK: data property
var viewModel: GroupViewModel!
var groupItem: GroupItem!
fileprivate var navigationViewNotReady = true
fileprivate let disposeBag = DisposeBag()
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()
}
private func setupReloadControl() {
collectionView.addPullToRefresh(PullToRefresh()) {
[unowned self] in
self.viewModel.reload()
}
}
}
/// UI bindings
fileprivate extension GroupViewController {
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: .group)
return cell
})
}
func binding() {
bindInteraction()
bindViewModelToRefreshing()
bindCollectionViewDelegate()
bindViewModelToCollectionView()
bindCollectionViewToViewModel()
bindViewModelToNavigationBarImage()
bindViewModelToNavigationBarTitle()
}
func bindInteraction() {
photographBtn.rx.tap.bind(to: viewModel.photographBtnTapped).disposed(by: disposeBag)
}
func bindViewModelToRefreshing() {
viewModel.isLoading
.subscribe(onNext: {[unowned self] flag in
self.collectionView.endRefreshing(at: .top)
}).disposed(by: disposeBag)
}
func bindCollectionViewDelegate() {
collectionView.rx.setDelegate(self).disposed(by: disposeBag)
}
func bindViewModelToCollectionView() {
viewModel.contents
.bind(to: collectionView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
func bindCollectionViewToViewModel() {
collectionView.rx.modelSelected(PhotoItem.self)
.asDriver()
.drive(onNext: { [unowned self] in self.viewModel.didSelect($0) })
.disposed(by: disposeBag)
}
func bindViewModelToNavigationBarTitle() {
navigationBarViewTitle.text = groupItem.group_name
}
func bindViewModelToNavigationBarImage() {
navigationBarViewImage.setImage(groupItem.group_avatar,
placeholder: UIImage(named: "Group\(groupItem.group_default_avatar)"))
}
}
extension GroupViewController: NavigationBarInteractiveViewController {
var navigationView: UIView {
return navigationBarView
}
func setNavigationBar() {
guard navigationViewNotReady else { return }
setRightBarButtonItems()
construvtNaivgationViewHierarchy()
activateConstraintsNavigation()
navigationViewNotReady = false
}
private func construvtNaivgationViewHierarchy() {
navigationController?.navigationBar.addSubview(navigationBarView)
navigationBarView.addSubview(navigationBarViewImage)
navigationBarView.addSubview(navigationBarViewTitle)
}
private func setRightBarButtonItems() {
let item = UIBarButtonItem(images: [UIImage(named: "navigation-QR"),
UIImage(named: "navigation-right")],
btnSpace: 6,
target: self,
actions: [#selector(viewModel!.presentGroupQR),
#selector(viewModel!.navigateToGroupDetail)])
navigationItem.rightBarButtonItem = item
}
}
/// layout
fileprivate extension GroupViewController {
func activateConstraintsNavigation() {
activateConstraintsNavigationBarView()
activateConstraintsNavigationBarViewImage()
activateConstraintsNavigationBarViewTitle()
}
func activateConstraintsNavigationBarView() {
guard let barContentView = navigationController?.navigationBar else { return }
navigationBarView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
navigationBarView.topAnchor.constraint(equalTo: barContentView.topAnchor),
navigationBarView.bottomAnchor.constraint(equalTo: barContentView.bottomAnchor),
navigationBarView.centerXAnchor.constraint(equalTo: barContentView.centerXAnchor)
])
}
func activateConstraintsNavigationBarViewTitle() {
navigationBarViewTitle.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
navigationBarViewTitle.centerYAnchor.constraint(equalTo: navigationBarView.centerYAnchor),
navigationBarViewTitle.leadingAnchor.constraint(equalTo: navigationBarViewImage.trailingAnchor, constant: 6),
navigationBarViewTitle.trailingAnchor.constraint(equalTo: navigationBarView.trailingAnchor),
navigationBarViewTitle.widthAnchor.constraint(lessThanOrEqualToConstant: view.width - 200)
])
}
func activateConstraintsNavigationBarViewImage() {
navigationBarViewImage.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
navigationBarViewImage.widthAnchor.constraint(equalToConstant: 40),
navigationBarViewImage.heightAnchor.constraint(equalToConstant: 40),
navigationBarViewImage.centerYAnchor.constraint(equalTo: navigationBarView.centerYAnchor),
navigationBarViewImage.leadingAnchor.constraint(equalTo: navigationBarView.leadingAnchor),
])
}
}
extension GroupViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return viewModel.layoutSizeForIndexPath(indexPath)
}
}
/// MARK: imagepicker delegate
extension GroupViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBAction func takePhotoAction() {
let vc = UIImagePickerController()
#if (arch(i386) || arch(x86_64))
vc.sourceType = .photoLibrary
#else
vc.sourceType = .camera
#endif
vc.delegate = self
present(vc, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
dismiss(animated: true, completion: nil)
guard let image = info[.originalImage] as? UIImage else { return }
viewModel.submit(image: image)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
extension GroupViewController: NavigationBackViewController {}
extension GroupViewController: Storyboarded {
static func instantiate() -> GroupViewController {
let vc = UIStoryboard.group.instantiateController(GroupViewController.self)
return vc
}
}
|