// // EstuLogPhotoListViewController.swift // JiaPeiManage // // Created by Ning.ge on 2023/8/30. // import UIKit import RxSwift import RxCocoa import ReusableKit import Dollar import JXPhotoBrowser import Kingfisher final class EstuLogPhotoListViewController: BaseCollectionViewController { let cellIdentifier:String = "_EstuLogListCell" // MARK: 服务属性 private let elogCoachService: ElogCoachServiceType = ElogCoachService(networking: ElogCoachNetworking()) private struct Reusable { static let ephotoCell = ReusableCell() static let headerView = ReusableView() } // MARK: UI let let statusBar = UIView().then { $0.backgroundColor = UIColor.db_theme } let navBar = MeTraineeNavBar.loadFromNib().then { $0.search_button.isHidden = true } let estuLogHeaderView = EstuLogHeaderView.loadFromNib() //学员ID var stuId:Int = 0 var classId:String = "" var eteachLogPhotoDataModel:ETeachLogPhotoDataModel? var rows : [ETeachLogPhotoInfo] = [] private var datasources: [EstuLogPhotoCellReactor] = [] var page:Int = 1 var pageSize:Int = 10 override func setupConstraints() { statusBar.snp.makeConstraints { (make) in make.left.right.top.equalToSuperview() make.height.equalTo(Metric.statusBarHeight) } navBar.snp.remakeConstraints { (make) in make.left.right.equalToSuperview() make.height.equalTo(Metric.navBarHeight) make.top.equalTo(statusBar.snp.bottom) } collectionView.snp.remakeConstraints { make in make.top.equalTo(self.navBar.snp.bottom) make.left.right.bottom.equalTo(self.view) } self.loadViewIfNeeded() } // MARK: StatusBar override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.setNavigationBarHidden(true, animated: true) } required convenience init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() configureMagicController() setupUI() biandView() } // MARK: 私有方法 func setupUI(){ self.navBar.title_label.text = "电子教学日志" self.navBar.back_button.addTarget(self, action: #selector(actionBackdo), for: .touchUpInside) } func configureMagicController(){ self.view.addSubview(navBar) self.view.addSubview(statusBar) } func biandView(){ collectionView.backgroundColor = UIColor.db_theme // collectionView.frame = self.view.bounds collectionView.register(Reusable.ephotoCell) collectionView.register(Reusable.headerView, kind: UICollectionView.elementKindSectionHeader) collectionView.delegate = self collectionView.dataSource = self self.isEmptyDisplay = false self.hideLoadAnimation() setupRefreshHeader(collectionView,.rabbit) {[unowned self] in self.getStudentTeachLogPhotoList(isLoading: false) } // 设置上拉加载更多 collectionView.es.addInfiniteScrolling { [weak self] in self?.getLoadMore() } getStudentTeachLogPhotoList(isLoading: true) // collectionView点击事件 collectionView.rx.itemSelected.throttle(.seconds(1), scheduler: MainScheduler.instance) .subscribe(onNext: { [weak self]indexPath in print("点击\(indexPath)行") self?.openPhotoBrowser(with: self!.collectionView, indexPath: indexPath) }).disposed(by: disposeBag) } func getStudentTeachLogPhotoList(isLoading:Bool){ page = 1 if isLoading {NYTips.show()} self.elogCoachService.elogTmsStudentPhotoByIdRequest(city: LocalManager.userInfo.city!, classId: classId, id: stuId, pageNum: page, pageSize: pageSize).subscribe(onSuccess: {[unowned self] eteachLogPhotoDataModel in self.rows.removeAll() self.eteachLogPhotoDataModel = eteachLogPhotoDataModel self.rows += eteachLogPhotoDataModel.rows! print("elogTmsStudentPhotoByIdRequest成功") self.isEmptyDisplay = true self.hideLoadAnimation() self.stopRefresh() self.collectionView.reloadData() NYTips.hide() }, onError: { error in self.stopRefresh() NYTips.hide() }) .disposed(by: disposeBag) } //更多 func getLoadMore(){ //计算-页数 if (eteachLogPhotoDataModel != nil){ let total = pageSize*self.page if(total<(eteachLogPhotoDataModel?.total)!){ self.page+=1 }else{ self.stopLoad() return //已经显示完 } } //请求网络-加载 self.elogCoachService.elogTmsStudentPhotoByIdRequest(city: LocalManager.userInfo.city!, classId: classId, id: stuId, pageNum: page, pageSize: pageSize).subscribe(onSuccess: {[unowned self] eteachLogPhotoDataModel in self.eteachLogPhotoDataModel = eteachLogPhotoDataModel self.rows += eteachLogPhotoDataModel.rows! print("resultInfoModel成功") self.collectionView.reloadData() self.stopLoad() }, onError: { error in self.stopLoad() }) .disposed(by: disposeBag) } func openPhotoBrowser(with collectionView: UICollectionView, indexPath: IndexPath) { let browser = JXPhotoBrowser() browser.numberOfItems = { self.rows.count } browser.reloadCellAtIndex = { context in let url = self.rows[context.index].filepath.flatMap { URL(string: $0) } let browserCell = context.cell as? JXPhotoBrowserImageCell let collectionPath = IndexPath(item: context.index, section: indexPath.section) let collectionCell = collectionView.cellForItem(at: collectionPath) as? EstuLogPhotoCell let placeholder = collectionCell?.coverImageView.image // 用 Kingfisher 加载 browserCell?.imageView.kf.setImage(with: url, placeholder: placeholder) } browser.transitionAnimator = JXPhotoBrowserZoomAnimator(previousView: { index -> UIView? in let path = IndexPath(item: index, section: indexPath.section) let cell = collectionView.cellForItem(at: path) as? EstuLogPhotoCell return cell?.coverImageView }) browser.pageIndex = indexPath.item browser.show() } } extension EstuLogPhotoListViewController: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.rows.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeue(Reusable.ephotoCell, for: indexPath) // cell.reactor = datasources[indexPath.item] cell.setRowInfo(rowInfo: self.rows[indexPath.row]) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { collectionView.deselectItem(at: indexPath, animated: true) } // Provide header view for each section func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { let headerView = collectionView.dequeue(Reusable.headerView, kind: kind, for: indexPath) // (ofKind: kind, withReuseIdentifier: Reusable.headerView, for: indexPath) return headerView } } extension EstuLogPhotoListViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // let cellReactor = datasources[indexPath.item] return CGSize(width: NYFitReal.screenWidth*0.5, height: 174) //Reusable.rcmdCell.class.cellSize(reactor: cellReactor) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets.zero } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 0.f } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 0.f } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSize(width: NYFitReal.screenWidth, height: 99.f) //branchSectionDelegate.headerSize(collectionView: collectionView,section: dataSource[section]) } }