123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // BaseTableViewController.swift
- // JSJP_Student_sw
- //
- // Created by Ning.ge on 2023/6/1.
- //
- import UIKit
- import RxSwift
- import EmptyKit
- class BaseTableViewController: BaseViewController,Refreshable {
-
- var isEmptyDisplay: Bool = true
-
- let tableView: UITableView = UITableView().then{
- $0.backgroundColor = .db_gray
- $0.showsVerticalScrollIndicator = false
- $0.tableFooterView = UIView()
- }
-
- override func setupConstraints() {
- tableView.snp.makeConstraints { (make) in
- make.edges.equalTo(0)
- }
- }
-
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- tableView.ept.dataSource = self
- tableView.ept.delegate = self
-
- view.addSubview(tableView)
-
- showLoadAnimation()
-
- registerNetErrorNotification()
-
- }
-
- //MARK: Public Method
- func startRefresh() {
-
- self.tableView.setContentOffset(.zero, animated: true)
- DispatchQueue.delay(time: 0.3, action: {
- self.tableView.es.startPullToRefresh()
- })
- }
-
- func registerNetErrorNotification() {
-
- NotificationCenter.default.rx.notification(custom: .netError)
- .subscribe(onNext: {[unowned self] (_) in
-
- self.stopRefresh()
-
- self.showNetErrorView()
-
- })
- .disposed(by: disposeBag)
- }
-
-
- func showNetErrorView() {
-
- if totalItems() > 0 { return }
-
- self.showAnimationView(self.tableView, animationType: .failure)
- }
-
- func showLoadAnimation() {
- self.showAnimationView(self.tableView)
- }
-
- func stopRefresh() {
-
- guard let isRefreshing = tableView.header?.isRefreshing else { return }
-
- if isRefreshing {
- tableView.es.stopPullToRefresh()
- }
- }
-
- //MARK: Private Method
- private func stopLoad() {
-
- guard let isLoading = tableView.footer?.isRefreshing else { return }
-
- if isLoading {
- tableView.es.stopLoadingMore()
- }
- }
- }
- //MARK: Notification
- extension BaseTableViewController {
-
- func totalItems() -> Int {
-
- var totalItems: Int = 0
-
- let sectionCount = self.tableView.numberOfSections
-
- for i in 0 ..< sectionCount {
-
- let items = self.tableView.numberOfRows(inSection: i)
-
- totalItems += items
- }
-
- return totalItems
- }
- }
- extension BaseTableViewController:EmptyDelegate,EmptyDataSource {
-
- func customViewForEmpty(in view: UIView) -> UIView? {
-
- return EmptyView.loadFromNib()
- }
-
- func emptyShouldAllowScroll(in view: UIView) -> Bool {
- return true
- }
-
- func emptyShouldDisplay(in view: UIView) -> Bool {
- return isEmptyDisplay
- }
- }
|