ConfideHomeRecommendView.kt 9.97 KB
Newer Older
洪国微 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package com.ydl.confide.home.widget

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.text.TextUtils
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.ydl.ydl_image.module.GlideApp
import com.yidianling.common.tools.RxImageTool
import com.ydl.confide.R
import com.ydl.confide.home.bean.ConfideHomeBodyBean
import com.ydl.confide.home.config.IConfideHomeConfig
import com.ydl.confide.home.contract.IConfideHomeContract
import com.ydl.confide.home.event.IConfideHomeEvent
21
import kotlinx.android.synthetic.main.confide_recommend_view.view.*
洪国微 committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

/**
 * @author yuanwai
 * @描述:为你推荐
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/9/21 0011
 */
class ConfideHomeRecommendView(var view: IConfideHomeContract.View, context: Context, private var confideHomeEvent: IConfideHomeEvent) : FrameLayout(context) {

    private var dp96 = 0
    private var dp16 = 0
    private var dp4 = 0
    private var dp3 = 0

    init {
        initView()
    }

    private fun initView() {
        var params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams = params
44
        View.inflate(context, R.layout.confide_recommend_view, this)
洪国微 committed
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
        dp96 = RxImageTool.dip2px(96f)
        dp16 = RxImageTool.dip2px(16f)
        dp4 = RxImageTool.dip2px(4f)
        dp3 = RxImageTool.dip2px(3f)
    }

    fun initData(bodyBean: ConfideHomeBodyBean?, position: Int, recommendId: Int) {
        if (bodyBean == null) {
            visibility = View.GONE
            return
        }
        var data = bodyBean
        //设置在线状态
        setLineStatu(bodyBean)
        //设置头像
        setHead(bodyBean)
        //设置播放按钮
        setPlay(bodyBean, position, recommendId)
        //设置名称
        tv_name.text = bodyBean.confidedName
        //设置性别
        setSex(bodyBean)
        //设置向TA倾诉文案
        setConfideNum(bodyBean)
        //设置接通率文案
        setConnection(bodyBean)
        //设置价格
        setPrice(bodyBean)
        //设置红包、原价
        setCoupon(bodyBean)
        //设置标签
        setConfideTag(bodyBean)
        //设置咨询师简介
        tv_content.text = bodyBean.confideContent
        //设置向TA倾诉按钮状态
        setConfideButton(bodyBean)
        //设置页面点击事件
        this.setOnClickListener {

            confideHomeEvent.consultantClick(bodyBean.linkUrl)
        }
    }

    /**
     * 设置在线状态
     */
    private fun setLineStatu(bodyBean: ConfideHomeBodyBean) {
        when (bodyBean.confideLine) {//1在线 2离线 3通话中
            1, 4 -> {
                tv_isLine.text = "在线"
95
                tv_isLine.setBackgroundResource(R.drawable.confide_bg_main_color)
洪国微 committed
96 97 98
            }
            2 -> {
                tv_isLine.text = "离线"
99
                tv_isLine.setBackgroundResource(R.drawable.confide_bg_color_666666)
洪国微 committed
100 101 102
            }
            3 -> {
                tv_isLine.text = "通话中"
103
                tv_isLine.setBackgroundResource(R.drawable.confide_bg_color_ff8f38)
洪国微 committed
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
            }
//            4 -> {
//                tv_confide.text = "继续倾诉"
//                tv_confide.setBackgroundResource(R.drawable.confidehome_recent_btn_continue_bg)
//            }
        }
    }

    /**
     * 设置头像
     */
    private fun setHead(bodyBean: ConfideHomeBodyBean) {
        //这样处理 是防止 滑动过程中 反复去加载图片
        if (!TextUtils.isEmpty(bodyBean.confidedIcon) && img_head.getTag(R.id.img_head) != bodyBean.confidedIcon) {
            GlideApp.with(context)
                    .load(bodyBean.confidedIcon)
                    .override(dp96, dp96)
                    .into(img_head)
            img_head.setTag(R.id.img_head, bodyBean.confidedIcon)
        }
    }

    /**
     * 设置播放按钮
     */
    private fun setPlay(bodyBean: ConfideHomeBodyBean, position: Int, recommendId: Int) {
        if (TextUtils.isEmpty(bodyBean.confideVoice)) {
            iv_play.visibility = View.GONE
        } else {
            iv_play.visibility = View.VISIBLE
        }
        if (bodyBean.confideIsPlay) {
136
            iv_play.setImageResource(R.drawable.confide_playing)
洪国微 committed
137
        } else {
138
            iv_play.setImageResource(R.drawable.confide_sond_play)
洪国微 committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        }
        iv_play.setOnClickListener {
            if (bodyBean.confideIsPlay!!) {
                confideHomeEvent.pauseVoice()
            } else {
                confideHomeEvent.playVoice(IConfideHomeConfig.TYPE_RECOMMEND, position, recommendId, bodyBean.confideVoice, bodyBean.confidedName)
            }
        }
    }

    /**
     * 设置性别
     */
    private fun setSex(bodyBean: ConfideHomeBodyBean) {
        if (bodyBean.confideSex == 1) {
154
            img_sex.setImageResource(R.drawable.confide_new_male)
洪国微 committed
155
        } else {
156
            img_sex.setImageResource(R.drawable.confide_new_female)
洪国微 committed
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
        }
    }

    /**
     * 设置向TA倾诉文案
     */
    private fun setConfideNum(bodyBean: ConfideHomeBodyBean) {
        if (TextUtils.isEmpty(bodyBean.confideNum)) {
            tv_confideNum.visibility = View.GONE
            tv_tv_confideNumContent.visibility = View.GONE
        } else {
            tv_confideNum.visibility = View.VISIBLE
            tv_tv_confideNumContent.visibility = View.VISIBLE
            tv_confideNum.text = bodyBean.confideNum
        }
    }

    /**
     * 设置接通率文案
     */
    private fun setConnection(bodyBean: ConfideHomeBodyBean) {
        if (TextUtils.isEmpty(bodyBean.confideConnection)) {
            tv_Connection.visibility = View.INVISIBLE
            tv_ConnectionContent.visibility = View.INVISIBLE
        } else {
            tv_Connection.visibility = View.VISIBLE
            tv_ConnectionContent.visibility = View.VISIBLE
            tv_Connection.text = bodyBean.confideConnection
        }
    }

    /**
     * 设置价格
     */
    private fun setPrice(bodyBean: ConfideHomeBodyBean) {
        val sb = StringBuffer()
        if (TextUtils.isEmpty(bodyBean.confideFee)) {
            sb.append("0")
        } else {
            sb.append(bodyBean.confideFee)
        }
        tv_price.text = sb.toString()
    }

    /**
     * 设置标签 todo 待优化
     */
    private fun setConfideTag(bodyBean: ConfideHomeBodyBean) {
        flowlayout_tag.removeAllViews()
        if (null == bodyBean.confidedTag || bodyBean.confidedTag.isEmpty()) {
            return
        }
        var i = 0
        for (tag in bodyBean.confidedTag!!) {
            if (i > 4) break
            var params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dp16)
            params.setMargins(0, 0, dp4, 0)
            var textView = TextView(context)
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f)
            textView.setTextColor(Color.parseColor("#808080"))
            textView.gravity = Gravity.CENTER
            textView.setPadding(dp3, 0, dp3, 0)
219
            textView.setBackgroundResource(R.drawable.confide_bg_tag)
洪国微 committed
220 221 222 223 224 225 226 227 228 229 230 231 232
            textView.layoutParams = params
            textView.text = tag
            flowlayout_tag.addView(textView)
            i++
        }
    }

    /**
     * 设置向TA倾诉按钮状态
     */
    private fun setConfideButton(bodyBean: ConfideHomeBodyBean) {
        when (bodyBean.confideLine) {//1在线 2离线 3通话中  4继续倾诉
            1 -> {
233
                tv_confide.setBackgroundResource(R.drawable.confide_line_1)
洪国微 committed
234 235
            }
            2 -> {
236
                tv_confide.setBackgroundResource(R.drawable.confide_line_2)
洪国微 committed
237 238
            }
            3 -> {
239
                tv_confide.setBackgroundResource(R.drawable.confide_line_3)
洪国微 committed
240 241
            }
            4 -> {
242
                tv_confide.setBackgroundResource(R.drawable.confide_line_4)
洪国微 committed
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
            }
        }
        tv_confide.setOnClickListener {
            click(bodyBean)
        }
    }

    /**
     * 设置红包、原价
     */
    private fun setCoupon(bodyBean: ConfideHomeBodyBean) {
        if (!TextUtils.isEmpty(bodyBean.couponText)) {
            tvCoupon.text = bodyBean.couponText
            tvCoupon.visibility = View.VISIBLE
        } else {
            tvCoupon.visibility = View.GONE
        }
        if (TextUtils.isEmpty(bodyBean.confideFee) || TextUtils.isEmpty(bodyBean.couponMoney) || bodyBean.couponMoney!!.toFloat() <= 0) {
            tvOriginalPrice.visibility = View.GONE
        } else {
            try {
                val couponMoney = bodyBean.couponMoney.toFloat().toInt()
                val confideFee = bodyBean.confideFee!!.toFloat().toInt()

                tvOriginalPrice.visibility = View.VISIBLE
                val originalBuffer = StringBuffer()
                originalBuffer.append(bodyBean.confideFee).append("元/次")
                tvOriginalPrice.text = originalBuffer.toString()
                //添加删除线
                tvOriginalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                if (couponMoney >= confideFee) {
                    tv_price.text = "0"
                } else {
                    val price = confideFee - couponMoney
                    tv_price.text = price.toString()
                }
            } catch (e: Exception) {
                //防止数据类型变化引起奔溃
            }

        }
    }

    /**
     * 点击事件
     */
    private fun click(bodyBean: ConfideHomeBodyBean) {
        when (bodyBean.confideLine) {//1在线 2离线 3通话中
            1, 3, 4 -> {
                if (bodyBean.confideLine == 1) {
                }
                if (bodyBean.confideLine == 3) {
                }

                confideHomeEvent.consultantClick(bodyBean.linkUrl)
            }
            2 -> {
                //跳转私聊 并发送自定义消息
                confideHomeEvent.toChatForMsg("" + bodyBean.uid)
            }
        }
    }
}