ConfideHomeRecommendExpertItemView.kt 8.27 KB
Newer Older
洪国微 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package com.ydl.confide.home.widget

import android.content.Context
import android.graphics.Color
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.confide.R
import com.ydl.confide.home.bean.ConfideHomeBodyBean
import com.ydl.confide.home.config.IConfideHomeConfig
import com.ydl.confide.home.event.IConfideHomeEvent
万齐军 committed
17
import com.ydl.ydl_image.module.GlideApp
洪国微 committed
18
import com.ydl.ydlcommon.utils.ScreenUtil
万齐军 committed
19
import com.yidianling.common.tools.RxImageTool
20
import kotlinx.android.synthetic.main.confide_recommend_expert_item_view.view.*
洪国微 committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/**
 * @author yuanwai
 * @描述:倾诉首页--最佳倾诉榜单item view
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/9/21
 */
class ConfideHomeRecommendExpertItemView(mContext: Context, private var confideHomeEvent: IConfideHomeEvent) : FrameLayout(mContext) {
    private var bgWidth: Int = 0
    private var dp100: Int = 0
    private var dp16 = 0
    private var dp4 = 0
    private var dp3 = 0

    init {
        initView()
    }

    private fun initView() {
41
        View.inflate(context, R.layout.confide_recommend_expert_item_view, this)
洪国微 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
        bgWidth = ScreenUtil.screenWidth - RxImageTool.dip2px(30f)
        dp100 = RxImageTool.dip2px(100f)
        dp16 = RxImageTool.dip2px(16f)
        dp4 = RxImageTool.dip2px(4f)
        dp3 = RxImageTool.dip2px(3f)
    }

    fun initData(bodyBean: ConfideHomeBodyBean?, position: Int) {
        if (null == bodyBean || null == bodyBean.bodyData || bodyBean.bodyData!!.isEmpty()) {
            visibility = View.GONE
            return
        }
        var dataBean = bodyBean.bodyData!![0]
        visibility = View.VISIBLE
        //设置标题
        if (!TextUtils.isEmpty(bodyBean.confidedTitle)) {
            tv_title.text = bodyBean.confidedTitle
        }
        //设置背景图
        setBGUrl(bodyBean)
        //设置在线状态
        setLineStatu(dataBean)
        //设置头像
        setHead(dataBean)
        //设置播放状态
        setPlay(dataBean, bodyBean.confideIsPlay, position)
        //设置名称
        if (!TextUtils.isEmpty(dataBean.confidedName)) {
            tv_name.text = dataBean.confidedName
        }
        //设置性别
        setSex(dataBean)
        //设置向TA倾诉文案
        setConfideNum(dataBean)
        //设置接通率文案
        setConnection(dataBean)
        //设置标签
        setConfideTag(dataBean)
        //设置咨询师简介
        tv_content.text = dataBean.confideContent
        //设置按钮文案
        setConfideButton(dataBean)
        //点击事件
        setOnClickListener {
            click(dataBean)
        }
    }

    /**
     * 设置背景图片
     */
    private fun setBGUrl(bodyBean: ConfideHomeBodyBean) {
        //这样处理 是防止 滑动过程中 反复去加载图片
        if (!TextUtils.isEmpty(bodyBean.confideBgUrl) && img_bg.getTag(R.id.img_bg) != bodyBean.confideBgUrl) {
            GlideApp.with(context)
                    .load(bodyBean.confideBgUrl)
                    .override(bgWidth, RxImageTool.dip2px(250f))
                    .into(img_bg)
            img_bg.setTag(R.id.img_bg, bodyBean.confideBgUrl)
        }
    }

    /**
     * 设置在线状态
     */
    private fun setLineStatu(bodyBean: ConfideHomeBodyBean) {
        when (bodyBean.confideLine) {//1在线 2离线 3通话中 4继续通话
            1, 4 -> {
                tv_isLine.text = "在线"
111
                tv_isLine.setBackgroundResource(R.drawable.confide_bg_main_color)
洪国微 committed
112 113 114
            }
            2 -> {
                tv_isLine.text = "离线"
115
                tv_isLine.setBackgroundResource(R.drawable.confide_bg_color_666666)
洪国微 committed
116 117 118
            }
            3 -> {
                tv_isLine.text = "通话中"
119
                tv_isLine.setBackgroundResource(R.drawable.confide_bg_color_ff8f38)
洪国微 committed
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
            }
        }
    }

    /**
     * 设置头像
     */
    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(dp100, dp100)
                    .into(img_head)
            img_head.setTag(R.id.img_head, bodyBean.confidedIcon)
        }
    }

    /**
     * 设置播放按钮
     */
    private fun setPlay(bodyBean: ConfideHomeBodyBean, confideIsPlay: Boolean, position: Int) {
        if (TextUtils.isEmpty(bodyBean.confideVoice)) {
            img_play.visibility = View.GONE
        } else {
            img_play.visibility = View.VISIBLE
        }
        if (confideIsPlay) {
148
            img_play.setImageResource(R.drawable.confide_playing)
洪国微 committed
149
        } else {
150
            img_play.setImageResource(R.drawable.confide_sond_play)
洪国微 committed
151 152 153 154 155
        }
        img_play.setOnClickListener {
            if (confideIsPlay) {
                confideHomeEvent.pauseVoice()
            } else {
156
                confideHomeEvent.playVoice(IConfideHomeConfig.TYPE_VIDEO_SHOW, position, bodyBean.confideVoice)
洪国微 committed
157 158 159 160 161 162 163 164 165
            }
        }
    }

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

    /**
     * 设置向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.GONE
            tv_ConnectionContent.visibility = View.GONE
        } else {
            tv_Connection.visibility = View.VISIBLE
            tv_ConnectionContent.visibility = View.VISIBLE
            tv_Connection.text = bodyBean.confideConnection
        }
    }

    /**
     * 设置标签 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)
218
            textView.setBackgroundResource(R.drawable.confide_bg_tag)
洪国微 committed
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
            textView.layoutParams = params
            textView.text = tag
            flowlayout_tag.addView(textView)
            i++
        }
    }

    /**
     * 设置向TA倾诉按钮状态
     */
    private fun setConfideButton(bodyBean: ConfideHomeBodyBean) {
        when (bodyBean.confideLine) {//1在线 2离线 3通话中
            1, 3, 4 -> {
                tv_confide.text = "向TA倾诉"
            }
            2 -> {
                tv_confide.text = "喊TA上线"
            }
        }
    }

    /**
     * 点击事件
     */
    private fun click(bodyBean: ConfideHomeBodyBean) {
        when (bodyBean.confideLine) {//1在线 2离线 3通话中
            1, 3, 4 -> {
万齐军 committed
246
                confideHomeEvent.consultantClick(bodyBean.doctorId, bodyBean.confidedId, bodyBean.uid?.toString())
洪国微 committed
247 248 249 250 251 252 253 254
            }
            2 -> {
                //跳转私聊 并发送自定义消息
                confideHomeEvent.toChatForMsg(""+bodyBean.uid)
            }
        }
    }
}