ConfideHomeRecommendView.kt 12.1 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

97
            confideHomeEvent.consultantClick(bodyBean.doctorId, bodyBean.confidedId, bodyBean.uid?.toString(), bodyBean.listenFree,bodyBean.linkUrl)
98

洪国微 committed
99
        }
万齐军 committed
100 101 102 103
        img_head.setOnClickListener {
            ActionCountUtils.record("listen_counselor_list_page", "head_portrait_click", "2")
            this.performClick()
        }
洪国微 committed
104 105
    }

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

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

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

    /**
     * 设置价格
     */
235 236 237 238 239 240 241 242 243
//    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
244 245

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

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

    /**
     * 设置红包、原价
     */
304
    /*private fun setCoupon(bodyBean: ConfideHomeBodyBean) {
洪国微 committed
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 334
        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) {
                //防止数据类型变化引起奔溃
            }

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

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

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