| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- import UIKit
- import RxSwift
- import RxCocoa
- import RxDataSources
- import PaiaiUIKit
- import PaiaiDataKit
- import PullToRefresh
- final class GroupViewController: UIViewController {
-
- @IBOutlet var collectionView: UICollectionView!
- @IBOutlet weak var photographBtn: UIButton!
-
- 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
- }()
-
-
- 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()
- }
- }
- }
- 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
- }
- }
- 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)
- }
- }
- 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
- }
- }
|