//
|
// MarketVC.swift
|
// DolphinEnglishLearnManager
|
//
|
// Created by 无故事王国 on 2024/5/21.
|
//
|
|
import UIKit
|
import QMUIKit
|
import RxRelay
|
import RxSwift
|
|
class MarketListViewModel:RefreshInnerModel<MarketModel>{
|
|
let keywords = BehaviorRelay<String>(value:"")
|
let types = BehaviorRelay<[String]>(value:[])
|
let menuTypes = BehaviorRelay<[MarketTypeModel]>(value:[])
|
var selectMenuTypes = Set<MarketTypeModel>(){
|
didSet{
|
types.accept(selectMenuTypes.map({"\($0.id)"}))
|
beginRefresh()
|
}
|
}
|
|
override func api() -> (Observable<BaseResponse<BaseResponseList<MarketModel>>>)? {
|
Services.goodsList(keywords: keywords.value, page: page, type: types.value)
|
}
|
}
|
|
|
class MarketVC: BaseVC {
|
@IBOutlet weak var tf_search: QMUITextField!
|
@IBOutlet weak var menu_collectView: UICollectionView!
|
@IBOutlet weak var content_collectionView: UICollectionView!
|
@IBOutlet weak var label_surplusCoin: UILabel!
|
|
private let viewModel = MarketListViewModel()
|
|
private var cellW = (JQ_ScreenW - 130 - 15) / 4.0
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
|
viewModel.configure(content_collectionView,needMore: true)
|
viewModel.beginRefresh()
|
|
Services.getIntegral().subscribe(onNext: {[weak self] result in
|
self?.label_surplusCoin.text = "\(result.data ?? 0)"
|
}).disposed(by: disposeBag)
|
|
Services.goodTypeStudy().subscribe(onNext: {[weak self] result in
|
self?.viewModel.menuTypes.accept(result.data ?? [])
|
self?.menu_collectView.reloadData()
|
}).disposed(by: disposeBag)
|
}
|
|
override func setUI() {
|
super.setUI()
|
tf_search.placeholderColor = UIColor.black.withAlphaComponent(0.81)
|
content_collectionView.delegate = self
|
content_collectionView.dataSource = self
|
content_collectionView.backgroundColor = .clear
|
content_collectionView.contentInset = UIEdgeInsets(top: 0, left: 65, bottom: 0, right:65)
|
content_collectionView.register(UINib(nibName: "MarketCCell", bundle: nil), forCellWithReuseIdentifier: "_MarketCCell")
|
tf_search.returnKeyType = .search
|
tf_search.delegate = self
|
|
menu_collectView.backgroundColor = .clear
|
menu_collectView.dataSource = self
|
menu_collectView.delegate = self
|
menu_collectView.showsHorizontalScrollIndicator = false
|
menu_collectView.showsVerticalScrollIndicator = false
|
menu_collectView.register(UINib(nibName: "MarketTagCCell", bundle: nil), forCellWithReuseIdentifier: "_MarketTagCCell")
|
}
|
|
@IBAction func searchAction(_ sender: UIButton) {
|
view.endEditing(true)
|
viewModel.keywords.accept(tf_search.text!)
|
viewModel.beginRefresh()
|
}
|
}
|
|
extension MarketVC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
if collectionView == menu_collectView{
|
let model = viewModel.menuTypes.value[indexPath.row]
|
if viewModel.selectMenuTypes.contains(model){
|
viewModel.selectMenuTypes.remove(model)
|
}else{
|
viewModel.selectMenuTypes.insert(model)
|
}
|
collectionView.reloadData()
|
}else{
|
let v = viewModel.dataSource.value?.records[indexPath.row]
|
let vc = MarketContentVC(goodsId: v?.id ?? 0)
|
vc.title = "商品详情"
|
push(vc: vc)
|
}
|
}
|
}
|
|
extension MarketVC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
if collectionView == menu_collectView{
|
return viewModel.menuTypes.value.count
|
}
|
|
return viewModel.dataSource.value?.records.count ?? 0
|
}
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
if collectionView == menu_collectView{
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_MarketTagCCell", for: indexPath) as! MarketTagCCell
|
let model = viewModel.menuTypes.value[indexPath.row]
|
cell.label_name.text = model.name
|
|
if viewModel.selectMenuTypes.contains(model){
|
cell.view_container.backgroundColor = UIColor(hexString: "#41A2EB")
|
cell.view_container.jq_borderWidth = 0
|
cell.label_name.textColor = .white
|
}else{
|
cell.view_container.backgroundColor = UIColor.white
|
cell.view_container.jq_borderWidth = 1
|
cell.label_name.textColor = UIColor(hexString: "#41A2EB")
|
}
|
return cell
|
}
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_MarketCCell", for: indexPath) as! MarketCCell
|
let model = viewModel.dataSource.value?.records[indexPath.row]
|
cell.marketModel = model
|
cell.backgroundColor = .white
|
return cell
|
}
|
}
|
|
extension MarketVC:UICollectionViewDelegateFlowLayout{
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
|
if collectionView == menu_collectView{
|
return CGSize(width: String.jq_getWidth(text: viewModel.menuTypes.value[indexPath.row].name, height: 41, font: 16) + 35, height: 41)
|
}
|
|
return CGSize(width: cellW, height: cellW * 1.09)
|
}
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
if collectionView == menu_collectView{
|
return 29
|
}
|
return 24
|
}
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
|
return 5
|
}
|
}
|
|
extension MarketVC:QMUITextFieldDelegate{
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
view.endEditing(true)
|
viewModel.keywords.accept(textField.text!)
|
viewModel.beginRefresh()
|
return true
|
}
|
}
|