add
无故事王国
2024-06-03 3d8ce4866799bea7e66699acdeb86b60b0ba033c
DolphinEnglishLearnStudent/Moudle/Market/MarketVC.swift
@@ -7,17 +7,51 @@
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.name}))
                                    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() {
@@ -28,39 +62,93 @@
                        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.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) {
                        let vc = MarketContentVC()
                        vc.title = "商品详情"
                        push(vc: vc)
                        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 {
                        return 30
                        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
                        cell.backgroundColor = .gray.withAlphaComponent(0.5)
                        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: 100, 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
            }
@@ -68,3 +156,12 @@
                        return 5
            }
}
extension MarketVC:QMUITextFieldDelegate{
            func textFieldShouldReturn(_ textField: UITextField) -> Bool {
                        view.endEditing(true)
                        viewModel.keywords.accept(textField.text!)
                        viewModel.beginRefresh()
                        return true
            }
}