BaseTableViewController.swift 2.9 KB

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