CouponDialog.kt 3.43 KB
Newer Older
徐健 committed
1 2 3 4 5 6 7 8
package com.yidianling.home.ui.view

import android.app.Activity
import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import com.yidianling.home.R
徐健 committed
9
import com.yidianling.home.model.bean.CouponBean
konghaorui committed
10
import kotlinx.android.synthetic.ydl.home_dialog_coupon.*
徐健 committed
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

/**
 * @author jiucheng
 * @描述:首页优惠券弹窗
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/8/6
 */
class CouponDialog : Dialog {
    private var activity: Activity? = null
    var status: Int = STATUS_RECEIVER
    var couponBean: CouponBean? = null
    var list: List<CouponBean>? = null
    var listener: OnClickEnsureListener? = null

    companion object {
        var STATUS_RECEIVER = 0 //立即领取
        var STATUS_GET = 1 //查看我的卡券
    }


    constructor(activity: Activity?, list: List<CouponBean>, listener: OnClickEnsureListener?) : super(activity) {
        this.activity = activity
        this.couponBean = list[0]
        this.list = list
        this.listener = listener
    }

    fun updateStatus(status: Int) {
        this.status = status
        updateUiByStatus()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
konghaorui committed
46
        setContentView(R.layout.home_dialog_coupon)
徐健 committed
47 48 49 50 51 52 53 54 55 56 57
        setCanceledOnTouchOutside(false)
        window.setBackgroundDrawable(ColorDrawable())
        if (couponBean != null) {
            if (!TextUtils.isEmpty(couponBean!!.title)) {
                tv_title.text = couponBean!!.title
            }
            tv_duration.text = couponBean!!.validity
            if (TextUtils.equals("1", couponBean!!.type)) {
                tv_money.text = couponBean!!.amount
                tv_unit.text = "元"
            } else {
徐健 committed
58
                tv_money.text = (couponBean!!.discount?:"0".toFloat() * 10).toString()
徐健 committed
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
                tv_unit.text = "折"
            }
            tv_limit.text = couponBean!!.condition
        }
        updateUiByStatus()
        tv_ensure.setOnClickListener {
            if (listener != null) {
                listener!!.clickEnsure(status)
            }
            if (status == STATUS_GET) {
                dismiss()
            }
        }
        iv_close.setOnClickListener {
            dismiss()
        }
    }

    private fun updateUiByStatus() {
        var title = ""
        var ensureText = ""
        var iamge = 0
        when (status) {
            STATUS_RECEIVER -> {
                if (list!!.size > 1) {
                    title = "获得${list!!.size}张新的优惠券"
                } else {
                    title = "获得新优惠券"
                }
                ensureText = "立即领取"
konghaorui committed
89
                iamge = R.drawable.home_coupon_receiver
徐健 committed
90 91 92 93 94 95 96 97
            }
            STATUS_GET -> {
                if (list!!.size > 1) {
                    title = "获得${list!!.size}张新的优惠券"
                } else {
                    title = "恭喜您,领取成功"
                }
                ensureText = "查看我的卡券"
konghaorui committed
98
                iamge = R.drawable.home_coupon_success
徐健 committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            }
        }
        iv_title.setImageResource(iamge)
        tv_status.text = title
        tv_ensure.text = ensureText
    }

    interface OnClickEnsureListener {
        fun clickEnsure(status: Int)
    }

    override fun show() {
        if (null == activity || activity!!.isFinishing) {
            return
        }
        super.show()
    }
}