BaseCollectionViewController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // BaseCollectionViewController.swift
  3. // JSJP_Student_sw
  4. //
  5. // Created by Ning.ge on 2023/5/31.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import EmptyKit
  10. class BaseCollectionViewController: BaseViewController,Refreshable {
  11. var isEmptyDisplay: Bool = true
  12. var allowListenScroll: Bool = false
  13. let collectionView: UICollectionView = UICollectionView(
  14. frame: .zero,
  15. collectionViewLayout: UICollectionViewFlowLayout()
  16. ).then{
  17. $0.backgroundColor = .db_gray
  18. $0.alwaysBounceVertical = true
  19. $0.showsVerticalScrollIndicator = false
  20. }
  21. override func setupConstraints() {
  22. collectionView.snp.makeConstraints { (make) in
  23. make.edges.equalTo(0)
  24. }
  25. }
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. collectionView.ept.dataSource = self
  29. collectionView.ept.delegate = self
  30. view.addSubview(collectionView)
  31. showLoadAnimation()
  32. registerNetErrorNotification()
  33. }
  34. //MARK: Public Method
  35. func startRefresh() {
  36. self.collectionView.setContentOffset(.zero, animated: true)
  37. DispatchQueue.delay(time: 0.3, action: {
  38. self.collectionView.es.startPullToRefresh()
  39. })
  40. }
  41. func registerNetErrorNotification() {
  42. NotificationCenter.default.rx.notification(custom: .netError)
  43. .subscribe(onNext: {[unowned self] (_) in
  44. self.stopRefresh()
  45. self.showNetErrorView()
  46. })
  47. .disposed(by: disposeBag)
  48. }
  49. func showNetErrorView() {
  50. if totalItems() > 0 { return }
  51. self.showAnimationView(self.collectionView, animationType: .failure)
  52. }
  53. func showLoadAnimation() {
  54. self.showAnimationView(self.collectionView)
  55. }
  56. func hideLoadAnimation() {
  57. self.hideAnimationView(self.collectionView)
  58. }
  59. func stopRefresh() {
  60. guard let isRefreshing = collectionView.header?.isRefreshing else { return }
  61. if isRefreshing {
  62. collectionView.es.stopPullToRefresh()
  63. }
  64. }
  65. //MARK: Private Method
  66. private func stopLoad() {
  67. guard let isLoading = collectionView.footer?.isRefreshing else { return }
  68. if isLoading {
  69. collectionView.es.stopLoadingMore()
  70. }
  71. }
  72. }
  73. //MARK: Notification
  74. extension BaseCollectionViewController {
  75. func totalItems() -> Int {
  76. var totalItems: Int = 0
  77. let sectionCount = self.collectionView.numberOfSections
  78. for i in 0 ..< sectionCount {
  79. let items = self.collectionView.numberOfItems(inSection: i)
  80. totalItems += items
  81. }
  82. return totalItems
  83. }
  84. }
  85. extension BaseCollectionViewController:EmptyDelegate,EmptyDataSource {
  86. func customViewForEmpty(in view: UIView) -> UIView? {
  87. return EmptyView.loadFromNib()
  88. }
  89. func emptyShouldAllowScroll(in view: UIView) -> Bool {
  90. return true
  91. }
  92. func emptyShouldDisplay(in view: UIView) -> Bool {
  93. return isEmptyDisplay
  94. }
  95. }
  96. extension BaseCollectionViewController: UIScrollViewDelegate {
  97. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  98. // guard self.collectionView.enableDirection,
  99. // let homeParentVc = UIViewController.topMost?.parent as? HomeParentViewController
  100. // else { return }
  101. // homeParentVc.scrollViewDidScroll(scrollView)
  102. }
  103. }