ConfideHomeRecommendView.kt 12 KB
Newer Older
洪国微 committed
1 2 3 4 5 6 7
package com.ydl.confide.home.widget

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
8 9 10
import com.blankj.utilcode.util.SpanUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
洪国微 committed
11 12 13 14 15
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
16
import com.ydl.ydl_image.module.GlideApp
万齐军 committed
17
import com.ydl.ydlcommon.utils.Utils
万齐军 committed
18
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
19 20
import com.yidianling.common.tools.RxImageTool
import kotlinx.android.synthetic.main.confide_recommend_view_new.view.*
洪国微 committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/**
 * @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
43
        View.inflate(context, R.layout.confide_recommend_view_new, this)
洪国微 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        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
        //设置性别
65
//        setSex(bodyBean)
洪国微 committed
66 67 68 69 70
        //设置向TA倾诉文案
        setConfideNum(bodyBean)
        //设置接通率文案
        setConnection(bodyBean)
        //设置价格
71
//        setPrice(bodyBean)
洪国微 committed
72
        //设置红包、原价
73
//        setCoupon(bodyBean)
洪国微 committed
74 75 76 77 78 79
        //设置标签
        setConfideTag(bodyBean)
        //设置咨询师简介
        tv_content.text = bodyBean.confideContent
        //设置向TA倾诉按钮状态
        setConfideButton(bodyBean)
80 81

        setListenAndScore(bodyBean)
洪国微 committed
82 83
        //设置页面点击事件
        this.setOnClickListener {
万齐军 committed
84 85 86 87 88 89 90 91 92 93 94 95
            ActionCountUtils.record(
                "listen_counselor_list_page",
                "listen_counselor_card_click",
                bodyBean.confidedId ?: ""
            )

            ActionCountUtils.record(
                "listen_counselor_popupwindows_page",
                "popupwindows_page_visit",
                bodyBean.confidedId ?: "",
                "1"
            )
洪国微 committed
96

万齐军 committed
97
            confideHomeEvent.consultantClick(bodyBean.doctorId, bodyBean.confidedId, bodyBean.uid?.toString())
洪国微 committed
98
        }
万齐军 committed
99 100 101 102
        img_head.setOnClickListener {
            ActionCountUtils.record("listen_counselor_list_page", "head_portrait_click", "2")
            this.performClick()
        }
洪国微 committed
103 104
    }

105 106 107 108 109 110 111 112 113
    private fun setListenAndScore(bodyBean: ConfideHomeBodyBean) {
        SpanUtils.with(price_content).append(bodyBean.confideFee.toString())
            .setForegroundColor(resources.getColor(R.color.confide_fe6040)).setFontSize(18, true)
            .append("元").setForegroundColor(resources.getColor(R.color.confide_fe6040))
            .setFontSize(11, true)
            .append("/25分钟").setForegroundColor(resources.getColor(R.color.confide_aaaeba))
            .setFontSize(11, true)
            .create()
        tv_score.text = bodyBean.confidePraiseScore
万齐军 committed
114
        if (bodyBean.listenFree == true && bodyBean.confideLine == 1) {
115 116 117 118 119 120 121 122 123 124
            confide_free_logo.visibility = VISIBLE
            Glide.with(context)
                .load(R.drawable.confide__free)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .into(confide_free_logo)
        } else {
            confide_free_logo.visibility = GONE
        }
    }

洪国微 committed
125 126 127 128 129 130 131
    /**
     * 设置在线状态
     */
    private fun setLineStatu(bodyBean: ConfideHomeBodyBean) {
        when (bodyBean.confideLine) {//1在线 2离线 3通话中
            1, 4 -> {
                tv_isLine.text = "在线"
132
                tv_isLine.setBackgroundResource(R.drawable.confide_bg_main_color)
洪国微 committed
133 134 135
            }
            2 -> {
                tv_isLine.text = "离线"
136
                tv_isLine.setBackgroundResource(R.drawable.confide_bg_color_666666)
洪国微 committed
137 138 139
            }
            3 -> {
                tv_isLine.text = "通话中"
140
                tv_isLine.setBackgroundResource(R.drawable.confide_bg_color_ff8f38)
洪国微 committed
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
            }
//            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) {
173
            iv_play.setImageResource(R.drawable.confide_playing)
洪国微 committed
174
        } else {
175
            iv_play.setImageResource(R.drawable.confide_sond_play)
洪国微 committed
176 177
        }
        iv_play.setOnClickListener {
万齐军 committed
178 179 180 181 182 183
            ActionCountUtils.record(
                "listen_counselor_list_page",
                "listen_counselor_icon_click",
                bodyBean.confidedId ?: "",
                "4"
            )
洪国微 committed
184 185 186
            if (bodyBean.confideIsPlay!!) {
                confideHomeEvent.pauseVoice()
            } else {
187
                confideHomeEvent.playVoice(IConfideHomeConfig.TYPE_RECOMMEND, position, recommendId, bodyBean.confideVoice, bodyBean.confidedName,bodyBean.confidedIcon)
洪国微 committed
188 189 190 191 192 193 194
            }
        }
    }

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

    /**
     * 设置向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
        }
    }

    /**
     * 设置价格
     */
234 235 236 237 238 239 240 241 242
//    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()
//    }
洪国微 committed
243 244

    private fun setConfideTag(bodyBean: ConfideHomeBodyBean) {
万齐军 committed
245
        if (null == bodyBean.confidedTag || bodyBean.confidedTag!!.isEmpty()) {
洪国微 committed
246 247 248
            return
        }
        var i = 0
万齐军 committed
249
        val sb = StringBuilder()
万齐军 committed
250
        for (tag in bodyBean.confidedTag!!) {
洪国微 committed
251
            if (i > 4) break
万齐军 committed
252
            sb.append(tag).append(" | ")
洪国微 committed
253 254
            i++
        }
万齐军 committed
255 256 257 258
        if (sb.length > 3) {
            sb.setLength(sb.length - 3)
        }
        flowlayout_tag.text = sb.toString()
洪国微 committed
259 260 261 262 263 264
    }

    /**
     * 设置向TA倾诉按钮状态
     */
    private fun setConfideButton(bodyBean: ConfideHomeBodyBean) {
万齐军 committed
265
        var sign2 = 1
洪国微 committed
266 267
        when (bodyBean.confideLine) {//1在线 2离线 3通话中  4继续倾诉
            1 -> {
268
                tv_confide.setBackgroundResource(R.drawable.confide_line_1)
洪国微 committed
269 270
            }
            2 -> {
霍志良 committed
271
                //男他女她
万齐军 committed
272
                sign2 = 3
霍志良 committed
273 274 275 276 277
                if (bodyBean.confideSex == 1) {
                    tv_confide.setBackgroundResource(R.drawable.confide_line_sex_boy)
                } else {
                    tv_confide.setBackgroundResource(R.drawable.confide_line_sex_girl)
                }
洪国微 committed
278 279
            }
            3 -> {
万齐军 committed
280
                sign2 = 2
281
                tv_confide.setBackgroundResource(R.drawable.confide_line_3)
洪国微 committed
282 283
            }
            4 -> {
284
                tv_confide.setBackgroundResource(R.drawable.confide_line_4)
洪国微 committed
285 286 287
            }
        }
        tv_confide.setOnClickListener {
万齐军 committed
288 289 290 291 292 293 294 295 296
            if (!Utils.isFastClick()) {
                ActionCountUtils.record(
                    "listen_counselor_list_page",
                    "listen_counselor_icon_click",
                    bodyBean.confidedId ?: "",
                    sign2.toString()
                )
                click(bodyBean)
            }
洪国微 committed
297 298 299 300 301 302
        }
    }

    /**
     * 设置红包、原价
     */
303
    /*private fun setCoupon(bodyBean: ConfideHomeBodyBean) {
洪国微 committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
        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) {
                //防止数据类型变化引起奔溃
            }

        }
334
    }*/
洪国微 committed
335 336 337 338 339 340 341 342 343 344 345 346

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

万齐军 committed
347
                confideHomeEvent.consultantClick(bodyBean.doctorId, bodyBean.confidedId, bodyBean.uid?.toString())
洪国微 committed
348 349 350 351 352 353 354 355
            }
            2 -> {
                //跳转私聊 并发送自定义消息
                confideHomeEvent.toChatForMsg("" + bodyBean.uid)
            }
        }
    }
}