No Description

GroupViewController.swift 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // GroupViewController.swift
  3. // PaiAi
  4. //
  5. // Created by zhengjianfei on 16/3/28.
  6. // Copyright © 2016年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. import RxSwift
  10. import RxCocoa
  11. import RxDataSources
  12. import PaiaiUIKit
  13. import PaiaiDataKit
  14. import PullToRefresh
  15. final class GroupViewController: UIViewController {
  16. // MARK: Storyboard property
  17. @IBOutlet var collectionView: UICollectionView!
  18. @IBOutlet weak var photographBtn: UIButton!
  19. // MARK: custom UI property
  20. var navigationBarView: UIView = {
  21. let view = UIView()
  22. view.alpha = 0
  23. return view
  24. }()
  25. var navigationBarViewTitle: UILabel = {
  26. let label = UILabel()
  27. label.textColor = UIColor.white
  28. return label
  29. }()
  30. var navigationBarViewImage: UIImageView = {
  31. let image = UIImageView()
  32. image.cornerRadius = 20
  33. return image
  34. }()
  35. // MARK: data property
  36. var viewModel: GroupViewModel!
  37. var groupItem: GroupItem!
  38. fileprivate var navigationViewNotReady = true
  39. fileprivate let disposeBag = DisposeBag()
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. initalize()
  43. }
  44. func initalize() {
  45. collectionView.register(UINib(nibName: "PhotoCell",
  46. bundle: Bundle(identifier: "com.Paiai-iOS")),
  47. forCellWithReuseIdentifier: "photoCell")
  48. setup()
  49. binding()
  50. }
  51. private func setup() {
  52. setupReloadControl()
  53. }
  54. private func setupReloadControl() {
  55. collectionView.addPullToRefresh(PullToRefresh()) {
  56. [unowned self] in
  57. self.viewModel.reload()
  58. }
  59. }
  60. }
  61. /// UI bindings
  62. fileprivate extension GroupViewController {
  63. var dataSource: RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>> {
  64. return RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>>(configureCell:
  65. { (dataSource, collectionView, indexPath, item) -> UICollectionViewCell in
  66. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCell
  67. cell.setInfo(item, source: .group)
  68. return cell
  69. })
  70. }
  71. func binding() {
  72. bindInteraction()
  73. bindViewModelToRefreshing()
  74. bindCollectionViewDelegate()
  75. bindViewModelToCollectionView()
  76. bindCollectionViewToViewModel()
  77. bindViewModelToNavigationBarImage()
  78. bindViewModelToNavigationBarTitle()
  79. }
  80. func bindInteraction() {
  81. photographBtn.rx.tap.bind(to: viewModel.photographBtnTapped).disposed(by: disposeBag)
  82. }
  83. func bindViewModelToRefreshing() {
  84. viewModel.isLoading
  85. .subscribe(onNext: {[unowned self] flag in
  86. self.collectionView.endRefreshing(at: .top)
  87. }).disposed(by: disposeBag)
  88. }
  89. func bindCollectionViewDelegate() {
  90. collectionView.rx.setDelegate(self).disposed(by: disposeBag)
  91. }
  92. func bindViewModelToCollectionView() {
  93. viewModel.contents
  94. .bind(to: collectionView.rx.items(dataSource: dataSource))
  95. .disposed(by: disposeBag)
  96. }
  97. func bindCollectionViewToViewModel() {
  98. collectionView.rx.modelSelected(PhotoItem.self)
  99. .asDriver()
  100. .drive(onNext: { [unowned self] in self.viewModel.didSelect($0) })
  101. .disposed(by: disposeBag)
  102. }
  103. func bindViewModelToNavigationBarTitle() {
  104. navigationBarViewTitle.text = groupItem.group_name
  105. }
  106. func bindViewModelToNavigationBarImage() {
  107. navigationBarViewImage.setImage(groupItem.group_avatar,
  108. placeholder: UIImage(named: "Group\(groupItem.group_default_avatar)"))
  109. }
  110. }
  111. extension GroupViewController: NavigationBarInteractiveViewController {
  112. var navigationView: UIView {
  113. return navigationBarView
  114. }
  115. func setNavigationBar() {
  116. guard navigationViewNotReady else { return }
  117. setRightBarButtonItems()
  118. construvtNaivgationViewHierarchy()
  119. activateConstraintsNavigation()
  120. navigationViewNotReady = false
  121. }
  122. private func construvtNaivgationViewHierarchy() {
  123. navigationController?.navigationBar.addSubview(navigationBarView)
  124. navigationBarView.addSubview(navigationBarViewImage)
  125. navigationBarView.addSubview(navigationBarViewTitle)
  126. }
  127. private func setRightBarButtonItems() {
  128. let item = UIBarButtonItem(images: [UIImage(named: "navigation-QR"),
  129. UIImage(named: "navigation-right")],
  130. btnSpace: 6,
  131. target: self,
  132. actions: [#selector(viewModel!.presentGroupQR),
  133. #selector(viewModel!.navigateToGroupDetail)])
  134. navigationItem.rightBarButtonItem = item
  135. }
  136. }
  137. /// layout
  138. fileprivate extension GroupViewController {
  139. func activateConstraintsNavigation() {
  140. activateConstraintsNavigationBarView()
  141. activateConstraintsNavigationBarViewImage()
  142. activateConstraintsNavigationBarViewTitle()
  143. }
  144. func activateConstraintsNavigationBarView() {
  145. guard let barContentView = navigationController?.navigationBar else { return }
  146. navigationBarView.translatesAutoresizingMaskIntoConstraints = false
  147. NSLayoutConstraint.activate([
  148. navigationBarView.topAnchor.constraint(equalTo: barContentView.topAnchor),
  149. navigationBarView.bottomAnchor.constraint(equalTo: barContentView.bottomAnchor),
  150. navigationBarView.centerXAnchor.constraint(equalTo: barContentView.centerXAnchor)
  151. ])
  152. }
  153. func activateConstraintsNavigationBarViewTitle() {
  154. navigationBarViewTitle.translatesAutoresizingMaskIntoConstraints = false
  155. NSLayoutConstraint.activate([
  156. navigationBarViewTitle.centerYAnchor.constraint(equalTo: navigationBarView.centerYAnchor),
  157. navigationBarViewTitle.leadingAnchor.constraint(equalTo: navigationBarViewImage.trailingAnchor, constant: 6),
  158. navigationBarViewTitle.trailingAnchor.constraint(equalTo: navigationBarView.trailingAnchor),
  159. navigationBarViewTitle.widthAnchor.constraint(lessThanOrEqualToConstant: view.width - 200)
  160. ])
  161. }
  162. func activateConstraintsNavigationBarViewImage() {
  163. navigationBarViewImage.translatesAutoresizingMaskIntoConstraints = false
  164. NSLayoutConstraint.activate([
  165. navigationBarViewImage.widthAnchor.constraint(equalToConstant: 40),
  166. navigationBarViewImage.heightAnchor.constraint(equalToConstant: 40),
  167. navigationBarViewImage.centerYAnchor.constraint(equalTo: navigationBarView.centerYAnchor),
  168. navigationBarViewImage.leadingAnchor.constraint(equalTo: navigationBarView.leadingAnchor),
  169. ])
  170. }
  171. }
  172. extension GroupViewController: UICollectionViewDelegateFlowLayout {
  173. func collectionView(_ collectionView: UICollectionView,
  174. layout collectionViewLayout: UICollectionViewLayout,
  175. sizeForItemAt indexPath: IndexPath) -> CGSize {
  176. return viewModel.layoutSizeForIndexPath(indexPath)
  177. }
  178. }
  179. /// MARK: imagepicker delegate
  180. extension GroupViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  181. @IBAction func takePhotoAction() {
  182. let vc = UIImagePickerController()
  183. #if (arch(i386) || arch(x86_64))
  184. vc.sourceType = .photoLibrary
  185. #else
  186. vc.sourceType = .camera
  187. #endif
  188. vc.delegate = self
  189. present(vc, animated: true, completion: nil)
  190. }
  191. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  192. dismiss(animated: true, completion: nil)
  193. guard let image = info[.originalImage] as? UIImage else { return }
  194. viewModel.submit(image: image)
  195. }
  196. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  197. dismiss(animated: true, completion: nil)
  198. }
  199. }
  200. extension GroupViewController: NavigationBackViewController {}
  201. extension GroupViewController: Storyboarded {
  202. static func instantiate() -> GroupViewController {
  203. let vc = UIStoryboard.group.instantiateController(GroupViewController.self)
  204. return vc
  205. }
  206. }