123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- //
- // ADHBaseVC.swift
- // ADHTuanCan
- //
- // Created by 敖德亨 on 2023/9/30.
- //
- import UIKit
- import RxSwift
- import RxCocoa
- import IQKeyboardManagerSwift
- //import MJRefresh
- class ADHBaseVC: UIViewController {
-
- /// 导航栏
- var navBar: MCNavBarDelegate?{
- didSet{
- self.addNavBar()
- }
- }
-
- /// 内容距离顶部的高度约束
- var navBarHeightConstraint: NSLayoutConstraint?{
- didSet{
- if let navBarHeightConstraint = navBarHeightConstraint {
- self.navBar?.navBarHeightSubject.bind(to: navBarHeightConstraint.rx.constBind).disposed(by: disposeAutoRelease)
- }
- }
- }
-
- /// 设置空数据scrollView
- var emptyDataScrollView : UIScrollView?{
- didSet{
- self.setEmptyDataScrollView()
- }
- }
-
- /// hud 提示
- // lazy var hud : MCHud! = {
- // return MCHud()
- // }()
- lazy var hud : MCHud! = {
- return MCHud()
- }()
-
-
-
- /// 自动释放
- lazy var disposeAutoRelease = {
- return DisposeBag()
- }()
-
-
- @objc dynamic func navBack(){
- self.navigationController?.popViewController(animated: true)
- }
-
-
- //MARK: -
-
- override func viewDidLoad() {
- super.viewDidLoad()
- self.view.backgroundColor = kTBackgroundColor
- IQKeyboardManager.shared.shouldResignOnTouchOutside = true
- self.navigationController?.navigationBar.isHidden = true
- self.view.backgroundColor = kTBackgroundColor
- self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- // self.sx_disableInteractivePop = false
- }
-
- deinit {
-
- NotificationCenter.default.removeObserver(self)
- self.emptyDataScrollView?.emptyDataSetSource = nil
- self.emptyDataScrollView?.emptyDataSetDelegate = nil
- self.emptyDataScrollView = nil
-
- //处理定时器的释放
- Mirror(reflecting: self).children.forEach { (child) in
- if let _ = child.label{
- if child.value is DispatchSourceTimer{
- (child.value as? DispatchSourceTimer)?.cancel()
- }
- }
- }
-
- DLog("\(self) dealloc")
-
- }
-
- }
- //MARK: - Private Method
- extension ADHBaseVC{
-
- /// 添加navBar
- private func addNavBar(){
- for view in self.view.subviews{
- if view is MCNavBarDelegate{
- view.removeFromSuperview()
- break
- }
- }
- if let navBar = navBar {
- self.view.addSubview(navBar)
- navBar.navBarBackBlock = {[unowned self] in
- self.navBack()
- }
- navBar.mas_makeConstraints { make in
- make?.left.top()?.right()?.mas_equalTo()(self.view)
- make?.height.offset()(navBar.navBarHeight())
- }
- _ = navBar.navBarHeightSubject.takeUntil(self.rx.deallocated).subscribe(onNext:{_ in
- navBar.mas_updateConstraints { make in
- make?.height.offset()(navBar.navBarHeight())
- }
- })
- }
- }
-
- }
- //MARK: - 创建一个空数据或者错误页面
- extension ADHBaseVC{
-
- /// 直接显示错误界面
- /// - Parameter status: <#status description#>
- /// - Returns: <#description#>
- @discardableResult
- final func showEmptyView(_ status : MCHttpStatus = .noData) -> UIView{
- let emptyView = UINib.view(withType: DZNEmptyLoadView.self)!
- var text : String?
- var image : UIImage?
- if status == .noData{
- text = self.emptyTitle()
- }
- switch status{
- case .noData:
- text = self.emptyTitle()
- image = UIImage.init(named: self.emptyImage())
- case .noNetwork:
- text = self.noNetworkTitle()
- image = UIImage.init(named: self.noNetworkImage())
- case .error:
- text = self.errorTitle()
- image = UIImage.init(named: self.errorImage())
- case .timeout:
- text = self.networkTimeOutTitle()
- image = UIImage.init(named: self.failureImage())
- default:break
- }
- emptyView.emptyImageView.image = image
- emptyView.emptyLabel.text = text
- self.view.addSubview(emptyView)
- self.view.bringSubviewToFront(emptyView)
- emptyView.mas_makeConstraints { (make) in
- make?.right.left()?.bottom()?.mas_equalTo()(self.view)
- make?.top.mas_equalTo()(self.navBar == nil ? self.view:self.navBar!.mas_bottom)
- }
-
- emptyView.activity.isHidden = !(status == .normal)
- emptyView.emptyLabel.isHidden = (status == .normal)
- emptyView.emptyImageView.isHidden = (status == .normal)
-
- return emptyView
- }
- }
|