CouponHintView.kt 3.79 KB
Newer Older
严久程 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package com.yidianling.course.widget

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.ydl.ydl_router.manager.YDLRouterManager
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.ydl.ydlcommon.router.IYDLRouterConstant
import com.yidianling.course.R
import com.yidianling.course.bean.CouponHintParam
import com.yidianling.course.bean.CouponHintResponseBean
严久程 committed
16
import com.yidianling.course.net.CourseRetrofitUtils
严久程 committed
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
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.coupon_hint_view.view.*

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述:我的课程优惠码View
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/12/27
 */
class CouponHintView : RelativeLayout{
    private var jumpType : Int = JUMP_TO_COUPON_LIST

    private var mCouponHintCallBack : CouponHintCallBack? = null
    //是否显示
    private var couponHintVisible : Int = View.VISIBLE

    private var couponUrl:String? = null

    companion object {
        /**
         * 跳转类型
         * 0.跳转红包
         */
        const val JUMP_TO_COUPON_LIST          = 0
    }

    constructor(context:Context):super(context){
        initView()
    }

    constructor(context: Context,attributeSet: AttributeSet):super(context,attributeSet){
        initView()
    }

    /**
     * 界面初始化
     */
    private fun initView(){
        View.inflate(context, R.layout.coupon_hint_view,this)
        setOnClickListener {
            when(jumpType){
                JUMP_TO_COUPON_LIST ->{//我的红包
                    if (TextUtils.isEmpty(couponUrl)) {
                        YDLRouterManager.router(IYDLRouterConstant.ROUTER_MINE_REDPACKET)
                    }else {
                        YDLRouterManager.router(couponUrl)
                    }
                }
            }
            visibility = View.GONE
        }
        imgClose.setOnClickListener {
            visibility = View.GONE
        }
    }

    /**
     * 请求红包列表数据
     */
    @SuppressLint("CheckResult")
    fun initData(visible : Int){
        this.couponHintVisible = visible

严久程 committed
82
        CourseRetrofitUtils.getCouponHint(CouponHintParam(0)).compose(RxUtils.resultJavaData())
严久程 committed
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
                .map { it }
                .filter { it != null }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Consumer {
                    if (null != it) {
                        setData(it)
                        couponUrl = it.url
                    }
                }, object: ThrowableConsumer() {
                    override fun accept(msg: String) {
                        visibility = View.GONE
                    }
                })
    }
    //设置跳转到我的红包
    fun setCouponList(){
        jumpType = JUMP_TO_COUPON_LIST
    }

    /**
     * 数据赋值
     */
    private fun setData(couponData : CouponHintResponseBean){
        if (TextUtils.isEmpty(couponData.msg)){
            visibility = View.GONE
            return
        }
        //因为数据请求是异步的 会出现 MainActivity设置了隐藏 但是到了这里 依然显示的现象
        visibility = couponHintVisible
        val sb = StringBuffer()
        sb.append(couponData.msg)
        tvMsg.text = sb.toString()
    }

    fun setCloseHide(){
        imgClose.visibility = View.GONE
    }

    fun setRedPacketCallBack(mRedPacketCallBack : CouponHintCallBack){
        this.mCouponHintCallBack = mRedPacketCallBack
    }

    interface CouponHintCallBack{
        fun jumpInfo() //暂未设置跳转
    }
}