杨锴
13 小时以前 876bce3064536c036112dd1834fb0d0013a57d16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
//  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
                }
}