BaseGroupTableViewController.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // BaseGroupTableViewController.swift
  3. // JiaPeiManage
  4. //
  5. // Created by Ning.ge on 2023/6/21.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import EmptyKit
  10. class BaseGroupTableViewController: BaseViewController,Refreshable {
  11. var isEmptyDisplay: Bool = true
  12. let tableView: UITableView = UITableView(frame: CGRectZero, style: .grouped).then{
  13. $0.backgroundColor = .db_theme
  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 hideLoadAnimation() {
  53. self.hideAnimationView(self.tableView)
  54. }
  55. func stopRefresh() {
  56. guard let isRefreshing = tableView.header?.isRefreshing else { return }
  57. if isRefreshing {
  58. tableView.es.stopPullToRefresh()
  59. }
  60. }
  61. //MARK: Private Method
  62. func stopLoad() {
  63. guard let isLoading = tableView.footer?.isRefreshing else { return }
  64. if isLoading {
  65. tableView.es.stopLoadingMore()
  66. }
  67. }
  68. }
  69. //MARK: Notification
  70. extension BaseGroupTableViewController {
  71. func totalItems() -> Int {
  72. var totalItems: Int = 0
  73. let sectionCount = self.tableView.numberOfSections
  74. for i in 0 ..< sectionCount {
  75. let items = self.tableView.numberOfRows(inSection: i)
  76. totalItems += items
  77. }
  78. return totalItems
  79. }
  80. }
  81. extension BaseGroupTableViewController:EmptyDelegate,EmptyDataSource {
  82. func customViewForEmpty(in view: UIView) -> UIView? {
  83. return EmptyView.loadFromNib()
  84. }
  85. func emptyShouldAllowScroll(in view: UIView) -> Bool {
  86. return true
  87. }
  88. func emptyShouldDisplay(in view: UIView) -> Bool {
  89. return isEmptyDisplay
  90. }
  91. }