package com.kuanzhai.user.ui.mine
|
|
import android.app.Activity
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import cn.sinata.xldutils.gone
|
import cn.sinata.xldutils.visible
|
import com.kuanzhai.user.R
|
import com.kuanzhai.user.network.HttpManager
|
import com.kuanzhai.user.network.entity.CouponOrCard
|
import com.kuanzhai.user.network.request
|
import com.kuanzhai.user.ui.TransparentStatusBarActivity
|
import com.kuanzhai.user.ui.mine.adapter.PayCardAdapter
|
import com.kuanzhai.user.ui.mine.adapter.PayCouponAdapter
|
import kotlinx.android.synthetic.main.activity_choose_discount.*
|
|
class ChooseDiscountActivity : TransparentStatusBarActivity() {
|
override fun setContentView() = R.layout.activity_choose_discount
|
|
private val orderId by lazy { intent.getIntExtra("id",0) }
|
private val orderType by lazy { intent.getIntExtra("type",0) }
|
private val money by lazy { intent.getDoubleExtra("money",0.0) }
|
|
private var couponId = ""
|
|
private val card = arrayListOf<CouponOrCard>()
|
private val coupon = arrayListOf<CouponOrCard>()
|
private val cardAdapter = PayCardAdapter(card)
|
private val couponAdapter = PayCouponAdapter(coupon)
|
|
override fun initClick() {
|
cardAdapter.setOnItemClickListener { view, position ->
|
card.forEach { it.check = false }
|
coupon.forEach { it.check = false }
|
card[position].check = true
|
cardAdapter.notifyDataSetChanged()
|
couponAdapter.notifyDataSetChanged()
|
cb_clear.isChecked = false
|
val discount = if (card[position].type == 2) card[position].money else (1.0-card[position].money/10)*money
|
setResult(Activity.RESULT_OK,intent.putExtra("discount",discount)
|
.putExtra("coupon",card[position].id.toString()).putExtra("discountType",2))
|
finish()
|
}
|
couponAdapter.setOnItemClickListener { view, position ->
|
card.forEach { it.check = false }
|
coupon.forEach { it.check = false }
|
coupon[position].check = true
|
cardAdapter.notifyDataSetChanged()
|
couponAdapter.notifyDataSetChanged()
|
cb_clear.isChecked = false
|
val discount = coupon[position].money
|
setResult(Activity.RESULT_OK,intent.putExtra("discount",discount)
|
.putExtra("coupon",coupon[position].id.toString()).putExtra("discountType",1))
|
finish()
|
}
|
cb_clear.setOnCheckedChangeListener { buttonView, isChecked ->
|
if (isChecked){
|
card.forEach { it.check = false }
|
coupon.forEach { it.check = false }
|
cardAdapter.notifyDataSetChanged()
|
couponAdapter.notifyDataSetChanged()
|
setResult(Activity.RESULT_OK,intent.putExtra("discount",0.0).putExtra("coupon","").putExtra("discountType",0))
|
finish()
|
}
|
}
|
}
|
|
override fun initView() {
|
title = "我的出行卡"
|
couponId = intent.getStringExtra("coupon")
|
if (couponId.isNullOrEmpty())
|
cb_clear.isChecked = true
|
rv_card.layoutManager = LinearLayoutManager(this)
|
rv_card.adapter = cardAdapter
|
rv_coupon.layoutManager = LinearLayoutManager(this)
|
rv_coupon.adapter = couponAdapter
|
getData()
|
}
|
|
private fun getData() {
|
HttpManager.queryCouponList(orderId,orderType).request(this){_,data->
|
data?.let {
|
val groupBy = it.groupBy { it.dataType }
|
val couponList = groupBy.get(1)
|
val cardList = groupBy.get(2)
|
if (!couponList.isNullOrEmpty()) {
|
tv_coupon.visible()
|
if (!couponId.isNullOrEmpty())
|
couponList.forEach { it.check = couponId == it.id.toString() }
|
coupon.addAll(couponList)
|
}
|
if (!cardList.isNullOrEmpty()) {
|
tv_card.visible()
|
if (!couponId.isNullOrEmpty())
|
cardList.forEach { it.check = couponId == it.id.toString() }
|
card.addAll(cardList)
|
}
|
cardAdapter.notifyDataSetChanged()
|
couponAdapter.notifyDataSetChanged()
|
if (coupon.isEmpty()&&card.isEmpty())
|
tv_empty.visible()
|
else
|
tv_empty.gone()
|
}
|
}
|
}
|
|
}
|