宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
//  MineMerchantCouponListVC.swift
//  OKProject
//
//  Created by 无故事王国 on 2022/2/15.
//  Copyright © 2022 yangwang. All rights reserved.
//
 
import UIKit
 
class MineMerchantCouponListVC: YYTableViewController {
 
    let viewModel = MineMerchantCouponListViewModel()
//    private var avaibleBtn:UIButton!
//    private var unavaibleBtn:UIButton!
    private var dictValue = Dictionary<String,[MyMerchantCouponModel]>()
    private var shinkValue = Dictionary<String,Bool>()
    private var allKeys = [String]()
 
    init(state: CouponsState) {
        super.init(nibName: nil, bundle: nil)
        tableView = UITableView(frame: .zero, style: .grouped)
        viewModel.type.accept(state.parame())
        tableView.backgroundColor = UIColor(hexString: "#F3F4F5")
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
        viewModel.dataSource.subscribe(onNext: { data in
            self.dictValue.removeAll()
            for item in data {
                let keyName = String(format: "%@_%@", item.merchantName.pinyinInitial(),item.merchantName)
                if self.dictValue[keyName] == nil{
                    self.dictValue[keyName] = [MyMerchantCouponModel]()
                    self.dictValue[keyName]?.append(item)
                    self.shinkValue.append(dict:[keyName:false])
                }else{
                    self.dictValue[keyName]?.append(item)
                }
            }
            self.allKeys = Array(self.dictValue.keys).sorted()
            self.tableView.reloadData()
        }).disposed(by: disposeBag)
    }
 
    //MARK: - UI
    override func setupViews() {
        super.setupViews()
        view.backgroundColor = UIColor(hexString: "#F3F4F5")
        tableView.register(UINib(nibName: "MerchantCouponTCell", bundle: nil), forCellReuseIdentifier: "_MerchantCouponTCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(MineMerchantCouponHeadView.self, forHeaderFooterViewReuseIdentifier: "_MineMerchantCouponHeadView")
        tableView.register(MineMerchantCouponFootView.self, forHeaderFooterViewReuseIdentifier: "_MineMerchantCouponFootView")
        tableView.separatorStyle = .none
        viewModel.configure(tableView: tableView)
        tableView.mj_header?.beginRefreshing()
 
//        avaibleBtn = UIButton(type: .custom)
//        avaibleBtn.setTitle("可使用", for: .normal)
//        avaibleBtn.isSelected = true
//        avaibleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
//        avaibleBtn.cornerRadius = 15.5
//        avaibleBtn.maskToBounds = true
//        avaibleBtn.tag = 10
//        avaibleBtn.addTarget(self, action: #selector(sequeeceAction(_:)), for: .touchUpInside)
//        avaibleBtn.setTitleColor(UIColor.white, for: .selected)
//        avaibleBtn.setTitleColor(UIColor(hexString: "#8C9097"), for: .normal)
//        avaibleBtn.setBackgroundImage(UIImage(color: UIColor(hexString: "#00BF30")!), for: .selected)
//        avaibleBtn.setBackgroundImage(UIImage(color: UIColor.white), for: .normal)
//
//        unavaibleBtn = UIButton(type: .custom)
//        unavaibleBtn.setTitle("已失效", for: .normal)
//        unavaibleBtn.isSelected = false
//        unavaibleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
//        unavaibleBtn.cornerRadius = 15.5
//        unavaibleBtn.maskToBounds = true
//        unavaibleBtn.tag = 11
//        unavaibleBtn.addTarget(self, action: #selector(sequeeceAction(_:)), for: .touchUpInside)
//        unavaibleBtn.setTitleColor(UIColor.white, for: .selected)
//        unavaibleBtn.setTitleColor(UIColor(hexString: "#8C9097"), for: .normal)
//        unavaibleBtn.setBackgroundImage(UIImage(color: UIColor(hexString: "#00BF30")!), for: .selected)
//        unavaibleBtn.setBackgroundImage(UIImage(color: UIColor.white), for: .normal)
//
//        view.addSubview(avaibleBtn)
//        view.addSubview(unavaibleBtn)
    }
 
//    @objc func sequeeceAction(_ btn:UIButton){
//        var state:CouponsState = .unused
//        avaibleBtn.isSelected = btn.tag == 10
//        unavaibleBtn.isSelected = btn.tag == 11
//        state = btn.tag == 10 ? CouponsState.unused : CouponsState.used
//        viewModel.type.accept(state.parame())
//        tableView.beginRefreshing()
//    }
 
    @objc func showDetail(_ btn:UIButton){
        let section = btn.tag % 1000
        let row = (btn.tag / 1000) % 10
 
        let key = allKeys[section]
        let array = dictValue[key]
        let vc = MineCouponsDetailVC()
        vc.myMerchantCouponModel = array![row]
        yy_push(vc: vc)
    }
 
    @objc func tanhShrinkAction(_ btn:UIButton){
        let index = btn.tag - 10
        let name = allKeys[index]
        shinkValue[name] = !shinkValue[name]!
        tableView.reloadData()
    }
 
    //MARK: - Layouts
    override func defineLayouts() {
        super.defineLayouts()
        tableView.snp.remakeConstraints { (make) in
            make.top.equalTo(49)
            make.left.right.bottom.equalToSuperview()
        }
 
//        avaibleBtn.snp.makeConstraints { make in
//            make.top.equalTo(13)
//            make.left.equalTo(12)
//            make.width.equalTo(62)
//            make.height.equalTo(31)
//        }
//
//        unavaibleBtn.snp.makeConstraints { make in
//            make.top.equalTo(13)
//            make.left.equalTo(avaibleBtn.snp.right).offset(29)
//            make.width.equalTo(62)
//            make.height.equalTo(31)
//        }
    }
 
    //MARK: - Rx
    override func bindRx() {
        super.bindRx()
    }
}
 
 
// MARK: - UITableViewDelegate
extension MineMerchantCouponListVC:UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 122
    }
 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let key = allKeys[indexPath.section]
        let array = dictValue[key]
        let vc = MineCouponsDetailVC()
        vc.couponState = CouponStateType(rawValue: viewModel.type.value)!
        vc.myMerchantCouponModel = array![indexPath.row]
        yy_push(vc: vc)
    }
}
 
// MARK: - UITableViewDelegate
extension MineMerchantCouponListVC:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let key = allKeys[section]
        let array = dictValue[key]
        if shinkValue[key] == false && array!.count >= 2{
            return 2
        }else{
            return array?.count ?? 0
        }
    }
 
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let key = allKeys[section]
        let m = dictValue[key]?.first
        let headView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "_MineMerchantCouponHeadView") as! MineMerchantCouponHeadView
        headView.merchantNameL?.text = m?.merchantName
        headView.headImg?.load(url: m?.headImg ?? "")
        return headView
    }
 
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let key = allKeys[section]
        let footView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "_MineMerchantCouponFootView") as! MineMerchantCouponFootView
        footView.tanhShrinkBtn.isSelected = shinkValue[key] ?? false
        footView.tanhShrinkBtn.tag = 10 + section
        footView.tanhShrinkBtn.addTarget(self, action: #selector(tanhShrinkAction(_:)), for: .touchUpInside)
        return footView
    }
 
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        let key = allKeys[section]
        let m = dictValue[key]
        if (m?.count ?? 0) > 2{
            return 37
        }else{
            return 0
        }
 
    }
 
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 55
    }
 
    func numberOfSections(in tableView: UITableView) -> Int {
        return allKeys.count
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
        let key = allKeys[indexPath.section]
        let array = dictValue[key]
        let cell = tableView.dequeueReusableCell(withIdentifier: "_MerchantCouponTCell", for: indexPath) as! MerchantCouponTCell
        cell.type = ActiveStatus(rawValue: viewModel.type.value)!
        cell.myMerchantCouponModel = array![indexPath.row]
        cell.recordBtn.setTitle("查看详情", for: .normal)
        cell.selectionStyle = .none
        cell.tag = 1000 + indexPath.section + 10 + indexPath.row
        cell.recordBtn.isUserInteractionEnabled = false
//        cell.recordBtn.addTarget(self, action: #selector(showDetail(_:)), for: .touchUpInside)
        cell.checkImg.isHidden = true
        return cell
    }
}
 
public class MineMerchantCouponHeadView:UITableViewHeaderFooterView{
 
    var headImg:UIImageView?
    var merchantNameL:UILabel?
 
    public override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
 
        setUI()
    }
 
    private func setUI(){
        contentView.backgroundColor = .white
        headImg = UIImageView()
        headImg?.cornerRadius = 18
        headImg?.maskToBounds = true
        headImg?.contentMode = .scaleToFill
        contentView.addSubview(headImg!)
        headImg?.snp.makeConstraints({ make in
            make.top.equalTo(6)
            make.left.equalTo(4)
            make.width.height.equalTo(36)
        })
 
        merchantNameL = UILabel()
        merchantNameL?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        merchantNameL?.textColor = UIColor(hexString: "4C4C4C")
        contentView.addSubview(merchantNameL!)
        merchantNameL?.snp.makeConstraints { make in
            make.left.equalTo(headImg!.snp.right).offset(9)
            make.centerY.equalTo(headImg!)
        }
 
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
 
public class MineMerchantCouponFootView:UITableViewHeaderFooterView{
 
    let tanhShrinkBtn = UIButton()
    public override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
 
        setUI()
    }
 
    private func setUI(){
        contentView.backgroundColor = .white
        tanhShrinkBtn.setTitle("展开", for: .normal)
        tanhShrinkBtn.setTitle("收回", for: .selected)
        tanhShrinkBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        tanhShrinkBtn.setTitleColor(UIColor(hexString: "#4C4C4C"), for: .normal)
        tanhShrinkBtn.setImage(UIImage(named: "icon_nav_open"), for: .normal)
        tanhShrinkBtn.adjustImage(position: .right, spacing: 10)
        contentView.addSubview(tanhShrinkBtn)
        tanhShrinkBtn.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}