SendRedPacketActivity.kt 10.3 KB
Newer Older
konghaorui committed
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
package com.yidianling.user.mine

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Instrumentation
import android.content.Intent
import android.os.Bundle
import android.os.SystemClock
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.bean.StatusBarOptions
import com.ydl.ydlcommon.utils.remind.HttpErrorUtils
import com.ydl.ydlcommon.utils.remind.ToastHelper
import com.ydl.ydlcommon.view.RoundCornerButton
import com.yidianling.common.tools.LogUtil
import com.yidianling.common.tools.ToastUtil
import com.yidianling.user.R
import com.yidianling.user.http.UserHttpImpl
import com.yidianling.user.mine.bean.RedPacketId
import com.yidianling.user.mine.bean.RedPacketIdCmd
import com.yidianling.user.mine.view.RedPacketGridView
import com.yidianling.ydl_pay.pay.PayActivity
import com.yidianling.ydl_pay.pay.PayParams
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_send_redpacket.*
import java.math.BigDecimal
import java.util.*


/**
 * Created by wi1ls on 2017/1/10.
 */
41
@Route(path = "/user/sendRedPacket")
konghaorui committed
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
class SendRedPacketActivity : BaseActivity(), View.OnClickListener, TextWatcher {

    override fun afterTextChanged(s: Editable?) {
        if (TextUtils.isEmpty(s.toString())) {
            tv_price_hint.text = "0.0"
            return
        }
        var str = s.toString()
        if (s.toString().startsWith(".")) {
            str = "0" + str
            LogUtil.d("change str: " + str)
        }
        LogUtil.d("str: " + str)
        var bd = BigDecimal(str)
        bd = bd.setScale(1, BigDecimal.ROUND_DOWN)
        tv_price_hint.text = bd.toString()
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }

    companion object {
        fun newIntent(activity: Activity, toUid: String): Intent {
            val intent = Intent(activity, SendRedPacketActivity::class.java)
            val bundle = Bundle()
            bundle.putString("to_uid", toUid)
            intent.putExtra("bundle", bundle)
            return intent
        }
    }

    override fun getStatusViewOptions(): StatusBarOptions {
        val statusBarOptions = StatusBarOptions(true, true)
        statusBarOptions.statusColor = "#d3574c"
        return statusBarOptions
    }
    override fun layoutResId(): Int {
        return R.layout.activity_send_redpacket
    }

    override fun initDataAndEvent() {
        to_uid = if (intent.hasExtra("to_uid")){
            intent.getStringExtra("to_uid")
        }else{
            var bundle = intent.getBundleExtra("bundle")
            bundle.getString("to_uid")
        }
        init()
    }

    private val prices = ArrayList<String>()

    private var mChild: List<RoundCornerButton>? = null


    private var charge_money: Float? = null

    private var thanksTitle: String = "一点心意,请笑纳!"


    internal var to_uid: String? = null

    internal fun init() {

        rcb_next.setOnClickListener(this)

        initGV()

        recharge_et.addTextChangedListener(SendRedPacketActivity@ this)
        recharge_thanks_content.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                thanksTitle = s.toString()
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
        recharge_tv!!.setOnClickListener {
            recharge_tv!!.isClickable = false
            val translateAnimation = AnimationUtils.loadAnimation(this@SendRedPacketActivity, R.anim.recharge_text)
            recharge_tv!!.startAnimation(translateAnimation)
            translateAnimation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation) {
                    for (i in mChild!!.indices) {
                        mChild!![i].setBg(-0x111112)
                        mChild!![i].setTextColor(-0x1000000)
                    }
                    charge_money = 0f
                }

                override fun onAnimationEnd(animation: Animation) {
                    recharge_et!!.visibility = View.VISIBLE
                    recharge_et!!.requestFocus()
                    recharge_et!!.setOnClickListener {
                        recharge_et!!.isCursorVisible = true
                        for (i in mChild!!.indices) {
                            mChild!![i].setBg(-0x111112)
                            mChild!![i].setTextColor(-0x1000000)
                        }
                        if (recharge_et.text.toString().isNullOrEmpty()){
                            charge_money = 0f
                            tv_price_hint.text = charge_money.toString()
                        }
                    }

                    val local = IntArray(2)
                    recharge_et!!.getLocationOnScreen(local)

                    val timer = Timer()
                    val timerTask = object : TimerTask() {
                        override fun run() {
                            val mInst = Instrumentation()
                            mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                                    SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, local[0].toFloat(), local[1].toFloat(), 0))    //x,y 即是事件的坐标
                            mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                                    SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, local[0].toFloat(), local[1].toFloat(), 0))

                        }
                    }
                    timer.schedule(timerTask, 500)
                }

                override fun onAnimationRepeat(animation: Animation) {

                }
            })
        }

    }

    private fun initGV() {
        prices.add("2元")
        prices.add("5元")
        prices.add("10元")
        prices.add("15元")
        prices.add("20元")
        prices.add("30元")

        recharge_rgv!!.setTexts(prices)
        charge_money = 2f
        mChild = recharge_rgv!!.childList
        mChild!![0].setBg(-0x2ca8b4)
        mChild!![0].setTextColor(-0x1)
        tv_price_hint.text = charge_money.toString()
        recharge_rgv!!.setItemClickListener(object : RedPacketGridView.ItemClickListener{
            override fun clickItem(view: View, position: Int) {
                recharge_rgv.requestFocus()
                if (recharge_et!!.visibility == View.VISIBLE) {
                    recharge_et!!.setText("")
                    recharge_et!!.isCursorVisible = false
                }
                for (i in mChild!!.indices) {
                    mChild!![i].setBg(-0x111112)
                    mChild!![i].setTextColor(-0x1000000)
                }
                charge_money = java.lang.Float.valueOf(prices.get(position).replace("元", ""))
                mChild!![position].setBg(-0x2ca8b4)
                mChild!![position].setTextColor(-0x1)
                tv_price_hint.text = charge_money.toString()
            }
        })
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.rcb_next -> if (judge()) {
                //获取红包支付或者充值单号
                getRechageId()
            }
        }
    }

    /**
     * 获取充值单号
     */
    @SuppressLint("CheckResult")
    private fun getRechageId() {
        showProgressDialog("")
        val cmd = RedPacketIdCmd(to_uid, charge_money.toString(), thanksTitle)
        LogUtil.d("thanksTitle: $thanksTitle")
        UserHttpImpl.getInstance().getRedPacketId(cmd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    dismissProgressDialog()
                    try {
                        if (resp.code == 0) {
                            val redPacketId = resp.data as RedPacketId
                            val payParams = PayParams().apply {
                                type = PayParams.PAY
                                title = "红包"
                                LogUtil.d("title: " + title)
                                payId = redPacketId.payId.toString()
                                needPay = charge_money!!
                            }
                            this@SendRedPacketActivity.startActivityForResult(PayActivity.newIntent(this@SendRedPacketActivity, payParams), 44)

//                            YdlCommonOut.startPayActivity(this, payParams, 44)
//                            startActivityForResult(intent, 44)
                        } else {
                            ToastUtil.toastShort(resp.msg)
                        }
                    } catch (e: Throwable) {
                        LogUtil.d(e.message)
                    }
                }, { throwable ->
                    HttpErrorUtils.handleError(mContext, throwable)
                    dismissProgressDialog()
                })

    }

    /**
     * 判断是否符合金额要求
     *
     * @return
     */
    private fun judge(): Boolean {
        charge_money =tv_price_hint.text.toString().toFloat()
        if (charge_money!!.toFloat().compareTo(0f)<=0) {//输入的金额是空
            ToastUtil.toastShort("请输入金额")
            recharge_et!!.setText("")
            return false
        }
        if (charge_money.toString().compareTo("0.1") < 0) {
            ToastUtil.toastShort("红包金额不可低于0.1元")
            return false
        }
        return true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 44 && resultCode == Activity.RESULT_OK) {
            ToastHelper.show("付款成功")
            finish()
        }
    }
}