//
|
// 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")
|
}
|
}
|