lmw
2023-06-13 adf8013576cbdd12e5ebea8ff7e32baf5d558b27
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.kuanzhai.user.dialog
 
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import androidx.fragment.app.DialogFragment
import cn.sinata.xldutils.activity.BaseActivity
import cn.sinata.xldutils.utils.SpanBuilder
import cn.sinata.xldutils.utils.myToast
import com.kuanzhai.user.R
import com.kuanzhai.user.network.HttpManager
import com.kuanzhai.user.network.entity.Card
import com.kuanzhai.user.network.request
import kotlinx.android.synthetic.main.dialog_pay_thank.*
import org.jetbrains.anko.matchParent
import org.jetbrains.anko.support.v4.find
import org.jetbrains.anko.wrapContent
import java.lang.Exception
 
class PayThankDialog: DialogFragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.dialog_pay_thank,container,false)
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_FRAME,R.style.Dialog)
    }
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        dialog?.window?.setLayout(matchParent, wrapContent)
        dialog?.window?.setGravity(Gravity.BOTTOM)
        dialog?.setCanceledOnTouchOutside(false)
    }
 
 
    private var money = 2.0 //打赏金额
    private var balance = 0.0
 
    private var payWay = 3 //1=微信,2=支付宝,3=余额
 
    var etWatcher = true //true 监听输入器,false不监听
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
 
        HttpManager.queryUserInfo().request(requireActivity() as BaseActivity,false) { _, data ->
            data?.balance?.apply {
                balance = this
                rb_balance.text = "余额(余额:%.2f元)".format(this)
            }
        }
 
        tv_action.setOnClickListener {
            if (money == 0.0)
                myToast("请选择打赏金额")
            else if (payWay == 3&&balance<money)
                myToast("余额不足,请选择其他支付方式")
            else{
                callback?.onOk(payWay,money)
                dismissAllowingStateLoss()
            }
        }
        iv_close.setOnClickListener {
            dismissAllowingStateLoss()
        }
 
        rg_thank_money.setOnCheckedChangeListener { group, checkedId ->
            if (checkedId == R.id.rb_2&&find<RadioButton>(group.checkedRadioButtonId).isChecked){
                money = 2.0
                clearEt()
            }
            else if (checkedId == R.id.rb_5&&find<RadioButton>(group.checkedRadioButtonId).isChecked){
                money = 5.0
                clearEt()
            }
            else if (checkedId == R.id.rb_10&&find<RadioButton>(group.checkedRadioButtonId).isChecked){
                money = 10.0
                clearEt()
            }
        }
 
        et_money.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
 
            }
 
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
 
            override fun afterTextChanged(s: Editable?) {
                if (etWatcher){
                    if (s.isNullOrEmpty()){
                        money = 0.0
                        tv_action.text = "立即支付"
                    }else{
                        rg_thank_money.clearCheck()
                        try {
                            money = s.toString().toDouble()
                            tv_action.text = "立即支付%.2f元".format(money)
                        }catch (e:Exception){
                            money = 0.0
                            tv_action.text = "立即支付"
                        }
                    }
                }
                etWatcher = true
            }
        })
 
        rg_pay.setOnCheckedChangeListener { _, i ->
            payWay = when(i){
                R.id.rb_wx->1
                R.id.rb_ali->2
                else->3
            }
        }
    }
 
    private fun clearEt() {
        etWatcher = false
        et_money.setText("")
        tv_action.text = "立即支付%.2f元".format(money)
    }
 
    interface Callback{
        fun onOk(way: Int,money:Double)
    }
 
    private var callback: Callback? = null
 
    fun setCallback(callback: Callback){
        this.callback = callback
    }
}