lmw
2023-06-16 03972ad1d3ce6ffe0be0395c0a4d5dcb4474031f
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
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()
            }
        }
    }
 
}