//
|
// CourseOnlineListVC.swift
|
// WanPai
|
//
|
// Created by 无故事王国 on 2023/6/15.
|
//
|
|
import UIKit
|
import JQTools
|
import MJRefresh
|
import RxCocoa
|
import RxSwift
|
import QMUIKit
|
|
|
class VideoViewModel:RefreshModel<VideosModel>{
|
let position = BehaviorRelay<Int>(value: 1) //1=线上课得积分,2=看视频得奖励
|
let search = BehaviorRelay<String>(value: "")
|
|
override func api() -> (Observable<BaseResponse<[VideosModel]>>)? {
|
return Services.benefitsVideoList(position: position.value, search: search.value)
|
}
|
}
|
|
|
class CourseOnlineListVC: BaseVC {
|
|
private var viewModel = VideoViewModel()
|
|
lazy var searchView:CourseOnlineSearchView = {
|
let searchV = CourseOnlineSearchView.jq_loadNibView()
|
searchV.frame = CGRect(x: 0, y: JQ_NavBarHeight, width: JQ_ScreenW, height: JQ_ScreenW * 0.564 + 57)
|
searchV.alpha = 1
|
return searchV
|
}()
|
|
var collectionView:BaseCollectionView!
|
|
required init(position:Int){
|
super.init(nibName: nil, bundle: nil)
|
self.viewModel.position.accept(position)
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
|
var type:BannerPositionType!
|
if viewModel.position.value == 1{
|
type = .onlineCourse
|
}else{
|
type = .video
|
}
|
Services.bannerList(position: type).subscribe(onNext: {[weak self] data in
|
guard let weakSelf = self else { return }
|
if let models = data.data{
|
let imgs = models.map({$0.img})
|
var items = [CommonBannerModel]()
|
for (index,img) in imgs.enumerated(){
|
items.append(CommonBannerModel(index: index,resource: img,mediaType: .imageUrl))
|
}
|
|
if models.count > 0{
|
weakSelf.view.addSubview(weakSelf.searchView)
|
self?.searchView.btn_search.addTarget(self, action: #selector(weakSelf.beginSearch), for: .touchUpInside)
|
self?.searchView.tf_search.delegate = self!
|
let insertH = JQ_ScreenW * 0.564 + 57
|
self?.collectionView.contentInset = UIEdgeInsets(top: insertH, left: 14, bottom: 14, right: 14)
|
self?.collectionView.scrollIndicatorInsets = UIEdgeInsets(top: insertH, left: 0, bottom: 0, right: 0)
|
|
self?.searchView.bannerView.setItems(items: items,selectClouse: { m in
|
let index = m.index
|
jumpPage(model: models[index].model, page: models[index].page, type: models[index].type, id: models[index].turnId)
|
})
|
}
|
}
|
}).disposed(by: disposeBag)
|
|
viewModel.configure(collectionView,needMore: false)
|
viewModel.beginRefresh()
|
}
|
|
override func setUI() {
|
let flowlayout = OnlineLayout()
|
|
flowlayout.itemSize = CGSize(width: (JQ_ScreenW - 50) / 2, height: 195)
|
flowlayout.minimumLineSpacing = 22
|
flowlayout.minimumLineSpacing = 22
|
// flowlayout.sectionHeadersPinToVisibleBounds = true
|
flowlayout.headerReferenceSize = CGSize(width: JQ_ScreenW, height: 52)
|
flowlayout.footerReferenceSize = CGSize(width: JQ_ScreenW, height: 10)
|
collectionView = BaseCollectionView(frame: .zero, collectionViewLayout: flowlayout)
|
collectionView.register(UINib(nibName: "CourseOnlineCCell", bundle: nil), forCellWithReuseIdentifier: "_CourseOnlineCCell")
|
collectionView.register(UINib(nibName: "CourseOnlineHeadView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "_CourseOnlineHeadView")
|
collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "_footer")
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.backgroundColor = .white
|
collectionView.contentInset = UIEdgeInsets(top: JQ_ScreenW * 0.564 + 57, left: 14, bottom: 14, right: 14)
|
|
view.addSubview(collectionView)
|
collectionView.snp.makeConstraints { make in
|
make.edges.equalToSuperview()
|
}
|
}
|
|
override func setRx() {
|
searchView.tf_search.rx.text.orEmpty.bind(to: viewModel.search).disposed(by: disposeBag)
|
}
|
|
@objc func beginSearch(){
|
searchView.tf_search.resignFirstResponder()
|
viewModel.beginRefresh()
|
|
}
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
let insertH = scrollView.contentOffset.y + JQ_ScreenW * 0.5641 + 57
|
searchView.jq_y = -insertH
|
}
|
}
|
|
extension CourseOnlineListVC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
let model = viewModel.dataSource.value[indexPath.section].list[indexPath.row]
|
let vc = CourseVideoDetailVC(id: model.id)
|
vc.title = title ?? ""
|
push(vc: vc)
|
}
|
}
|
|
extension CourseOnlineListVC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
|
if kind == UICollectionView.elementKindSectionHeader{
|
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "_CourseOnlineHeadView", for: indexPath) as! CourseOnlineHeadView
|
headerView.indexPath = indexPath
|
headerView.moreClouse = { [weak self] index in
|
let id = self?.viewModel.dataSource.value[index.section].id
|
let vc = CourseOnlineSubListVC(classificationId: id!)
|
vc.title = self?.title ?? ""
|
self?.push(vc: vc)
|
}
|
headerView.label_name.text = viewModel.dataSource.value[indexPath.section].name
|
return headerView
|
}
|
|
if kind == UICollectionView.elementKindSectionFooter{
|
var footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "_footer", for: indexPath)
|
|
let lineView = UIView()
|
lineView.backgroundColor = UIColor(hexStr: "#979797").withAlphaComponent(0.29)
|
footerView.addSubview(lineView)
|
lineView.snp.makeConstraints { make in
|
make.left.right.bottom.equalToSuperview()
|
make.height.equalTo(1)
|
}
|
return footerView
|
}
|
|
|
return UICollectionReusableView()
|
}
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let model = viewModel.dataSource.value[indexPath.section].list[indexPath.row]
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_CourseOnlineCCell", for: indexPath) as! CourseOnlineCCell
|
cell.videoDetailModel = model
|
return cell
|
}
|
|
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
return viewModel.dataSource.value.count
|
}
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return viewModel.dataSource.value[section].list.count
|
}
|
}
|
|
class OnlineLayout:UICollectionViewFlowLayout{
|
|
}
|
|
extension CourseOnlineListVC:QMUITextFieldDelegate{
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
textField.resignFirstResponder()
|
viewModel.beginRefresh()
|
return true
|
}
|
}
|