ConsultSubPayDialog.kt 4.25 KB
Newer Older
fengquan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package com.yidianling.consultant.dialog

import `in`.xiandan.countdowntimer.CountDownTimerSupport
import `in`.xiandan.countdowntimer.OnCountDownTimerListener
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
17
import com.ydl.ydlcommon.utils.SharedPreferencesEditor
fengquan committed
18 19 20 21 22
import com.yidianling.common.tools.ToastUtil
import com.yidianling.consultant.R
import com.yidianling.consultant.model.bean.PromptPaymentBean
import java.text.DecimalFormat
import java.text.SimpleDateFormat
23
import java.util.*
fengquan committed
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


/**
 * 咨询助理浮层
 * Created by xj on 2019/10/30.
 */

class ConsultSubPayDialog(
    private val mContext: Context,
    private val promptPaymentBean: PromptPaymentBean
) : Dialog(mContext, R.style.dialog_default_style) {

    var mTimer: CountDownTimerSupport? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.consultant_expert_consult_sub_pay_dialog)
        val params = window.attributes
        params.width = WindowManager.LayoutParams.WRAP_CONTENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        params.gravity = Gravity.CENTER
        window.attributes = params
        setCanceledOnTouchOutside(false)
        initView();
    }

    private fun initView() {
        var titleTv = findViewById<TextView>(R.id.tv_title)
        var timeTv = findViewById<TextView>(R.id.tv_time)
        titleTv.paint.isFakeBoldText = true
        Glide.with(mContext)
            .load(promptPaymentBean.smallImage).into(findViewById<ImageView>(R.id.avaterIv))
        findViewById<TextView>(R.id.nameTv).text = promptPaymentBean.name
        findViewById<TextView>(R.id.desTv).text = promptPaymentBean.productName
        val decimalFormat = DecimalFormat("###.##")
        val price = decimalFormat.format(promptPaymentBean.price)
        findViewById<TextView>(R.id.priceTv).text = "¥$price"
        var format = SimpleDateFormat("HH:mm:ss");
62
        format.timeZone = TimeZone.getTimeZone("GMT+0");
刘鹏 committed
63 64
        var millisInFuture =
            promptPaymentBean.toPayTime?.minus(promptPaymentBean.currentTime ?: 0) ?: 0
fengquan committed
65 66 67 68 69 70 71 72
        mTimer = CountDownTimerSupport(millisInFuture, 1000)
        mTimer?.setOnCountDownTimerListener(object : OnCountDownTimerListener {
            override fun onTick(millisUntilFinished: Long) {
                val time = format.format(millisUntilFinished)
                timeTv.text = time
            }

            override fun onFinish() {
73 74
                var nextShowTime =
                    System.currentTimeMillis() + ((promptPaymentBean.orderToPayTime ?: 1) * 1000)
75
                SharedPreferencesEditor.putString("orderToPayTime", nextShowTime.toString())
fengquan committed
76 77 78 79 80 81 82 83 84
                dismiss()
            }

            override fun onCancel() {
                // 倒计时手动停止
            }
        })
        mTimer?.start()
        findViewById<TextView>(R.id.tv_close).setOnClickListener {
85 86
            var nextShowTime =
                System.currentTimeMillis() + ((promptPaymentBean.orderToPayTime ?: 1) * 1000)
87
            SharedPreferencesEditor.putString("orderToPayTime", nextShowTime.toString())
fengquan committed
88 89
            mTimer?.stop()
            dismiss()
90

fengquan committed
91 92 93 94 95 96 97 98 99
        }

        findViewById<View>(R.id.pay).setOnClickListener {
            if (!TextUtils.isEmpty(promptPaymentBean.orderDetailUrl)) {
                val h5Params = H5Params(promptPaymentBean.orderDetailUrl!!, null)
                NewH5Activity.start(context, h5Params)
                dismiss()
            } else {
                ToastUtil.toastShort("跳转失败")
100
                dismiss()
fengquan committed
101
            }
102 103
            var nextShowTime =
                System.currentTimeMillis() + ((promptPaymentBean.orderToPayTime ?: 1) * 1000)
104
            SharedPreferencesEditor.putString("orderToPayTime", nextShowTime.toString())
fengquan committed
105 106 107 108 109 110 111 112 113 114 115 116
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        if (mTimer != null) {
            mTimer?.stop()
            mTimer = null
        }
    }

}