//
|
// MineCouponsListVC.swift
|
// OKProject
|
//
|
// Created by alvin_y on 2020/6/15.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
import HandyJSON
|
/// 优惠券状态
|
enum CouponsState:Int,HandyJSONEnum{
|
/// 未使用
|
case unused = 1
|
/// 已使用
|
case used = 2
|
/// 已过期
|
case exceed = 3
|
|
func parame() -> Int {
|
switch self {
|
case .unused:
|
return 1
|
case .used:
|
return 2
|
case .exceed:
|
return 3
|
}
|
}
|
}
|
|
|
/// 优惠券列表
|
class MineCouponsListVC: YYTableViewController {
|
|
let viewModel = MineCouponsListViewModel()
|
// private var avaibleBtn:UIButton!
|
// private var unavaibleBtn:UIButton!
|
// private var presentBtn:UIButton!
|
|
init(state: CouponsState) {
|
super.init(nibName: nil, bundle: nil)
|
viewModel.state.accept(state.parame())
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
|
// Do any additional setup after loading the view.
|
}
|
|
//MARK: - UI
|
override func setupViews() {
|
super.setupViews()
|
view.backgroundColor = UIColor(hexString: "#F3F4F5")
|
tableView.register(cellName: "TravelCouponsCell", identifier: "item")
|
tableView.delegate = self
|
tableView.dataSource = self
|
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)
|
//
|
//
|
// presentBtn = UIButton(type: .custom)
|
// presentBtn.setTitle("赠送", for: .normal)
|
// presentBtn.setTitleColor(UIColor(hexString: "#FB692A"), for: .normal)
|
// presentBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
|
// presentBtn.setImage(UIImage(named: "icon_arrow_left-1"), for: .normal)
|
// presentBtn.adjustImage(position: .right, spacing: 9)
|
// presentBtn.addTarget(self, action: #selector(give), for: .touchUpInside)
|
//
|
// view.addSubview(avaibleBtn)
|
// view.addSubview(unavaibleBtn)
|
// view.addSubview(presentBtn)
|
}
|
|
|
//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)
|
// }
|
//
|
// presentBtn.snp.makeConstraints { make in
|
// make.right.equalToSuperview().offset(-14)
|
// make.centerY.equalTo(avaibleBtn)
|
// make.height.equalTo(30)
|
// }
|
}
|
|
// @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.state.accept(state.parame())
|
// tableView.mj_header?.beginRefreshing()
|
// }
|
|
// /// 赠送
|
// @objc func give() {
|
// let vc = MineGiveCouponsListVC()
|
// self.yy_push(vc: vc)
|
// }
|
|
//MARK: - Rx
|
override func bindRx() {
|
super.bindRx()
|
viewModel.delMyCouponSubject
|
.subscribe(onNext: { (state) in
|
switch state{
|
case .loading:
|
self.show()
|
break
|
case .success(_):
|
self.hide()
|
var data = self.viewModel.dataSource.value
|
data.remove(at: self.viewModel.row.value)
|
self.viewModel.dataSource.accept(data)
|
self.tableView.reloadData()
|
break
|
case .error(let error):
|
self.hide()
|
alert(text: error.localizedDescription)
|
break
|
}
|
}).disposed(by: disposeBag)
|
}
|
}
|
|
|
// MARK: - UITableViewDelegate
|
extension MineCouponsListVC:UITableViewDelegate{
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
return UITableView.automaticDimension
|
}
|
|
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
return true
|
}
|
|
// 设置确认删除按钮的文字
|
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
|
return "删除"
|
}
|
|
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
|
return .delete
|
}
|
|
|
|
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
self.viewModel.row.accept(indexPath.row)
|
self.viewModel.id.accept(viewModel.dataSource.value[indexPath.row].id)
|
self.viewModel.delMyCoupon()
|
}
|
}
|
|
// MARK: - UITableViewDelegate
|
extension MineCouponsListVC:UITableViewDataSource{
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
return viewModel.dataSource.value.count
|
}
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
let cell = tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath) as! TravelCouponsCell
|
cell.myConfigure(model: viewModel.dataSource.value[indexPath.row])
|
cell.button_select.isHidden = true
|
if viewModel.state.value == 2{
|
cell.image_front_bg.image = UIImage.init(named: "bg_gray_front")
|
cell.image_expired.isHidden = false
|
}
|
cell.selectionStyle = .none
|
return cell
|
}
|
|
|
}
|