HomeAssuageGriefItemView.kt 10.2 KB
Newer Older
徐健 committed
1 2 3 4 5 6 7 8 9 10 11 12 13
package com.yidianling.home.ui.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.text.*
import android.text.style.ForegroundColorSpan
import android.text.style.ImageSpan
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.yidianling.home.R
14
import com.yidianling.home.event.IHomeBaseEvent
徐健 committed
15 16 17 18
import com.yidianling.home.model.bean.HomeAskBean
import com.ydl.ydl_image.config.SimpleImageOpConfiger
import com.ydl.ydl_image.manager.YDLImageCacheManager
import com.ydl.ydl_router.manager.YDLRouterManager
徐健 committed
19 20 21 22
import com.ydl.ydlcommon.modular.ModularServiceManager
import com.ydl.ydlcommon.router.IYDLRouterConstant
import com.ydl.ydlcommon.utils.MoonUtil
import com.ydl.ydlcommon.utils.Utils
徐健 committed
23 24
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxImageTool
徐健 committed
25
import com.yidianling.user.api.service.IUserService
konghaorui committed
26
import kotlinx.android.synthetic.ydl.home_assuage_grief_item_view.view.*
徐健 committed
27 28 29 30 31 32 33 34 35


/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 解忧*问答模块item
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/15
 */
36
class HomeAssuageGriefItemView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) : LinearLayout(mContext) {
徐健 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    private var homeAssuageGriefImageItemView: HomeAssuageGriefImageItemView? = null
    private var focusClick: Boolean = false;

    init {
        initView()
    }

    private fun initView() {
        val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params.setMargins((15 * RxDeviceTool.getScreenDensity(mContext)).toInt()
                , (0 * RxDeviceTool.getScreenDensity(mContext)).toInt()
                , (15 * RxDeviceTool.getScreenDensity(mContext)).toInt()
                , (16 * RxDeviceTool.getScreenDensity(mContext)).toInt())
        layoutParams = params
konghaorui committed
52
        View.inflate(mContext, R.layout.home_assuage_grief_item_view, this)
徐健 committed
53 54 55 56 57 58 59 60 61


    }

    fun updateData(bean: HomeAskBean.DataBean, position: Int, index: Int) {
        this.setOnClickListener {
            homeEvent?.askItemClick(bean)
        }
        val op = SimpleImageOpConfiger()
徐健 committed
62 63
        op.errorPic = R.drawable.platform_head_place_hold_pic
        op.loadingPic = R.drawable.platform_head_place_hold_pic
徐健 committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        YDLImageCacheManager.showImage(mContext, bean?.header, homeModuleAssuageGriefViewPersonImg, op)
        homeModuleAssuageGriefViewPersonName.text = bean?.name
        homeModuleAssuageGriefViewTime.text = bean?.timeStr
        MoonUtil.identifyFaceExpression(mContext, homeModuleAssuageGriefViewContent,
                bean?.content, ImageSpan.ALIGN_BOTTOM)
        homeModuleAssuageGriefViewTag.text = bean?.askTag
        homeModuleAssuageGriefViewCommentsCount.text = bean?.commentsCount.toString()
        homeModuleAssuageGriefViewZanCount.text = bean?.zanCount.toString()


        /**
         * 点赞
         */
        ll_zan.setOnClickListener {
            //防止连击
            if (!Utils.isFastClick()) {
徐健 committed
80
                if (!ModularServiceManager.provide(IUserService::class.java).isLogin()) {
徐健 committed
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
                    //跳转登录
                    YDLRouterManager.router(IYDLRouterConstant.ROUTER_MINE_LOGIN)
                    return@setOnClickListener
                }
                bean.isZan = if (bean.isZan == 1 ) 0 else 1

                if (bean.isZan == 1) {
                    homeModuleAssuageGriefViewIsZan.progress = 0f
                    homeModuleAssuageGriefViewIsZan.visibility = View.VISIBLE
                    homeModuleAssuageGriefViewIsNotZan.visibility = View.GONE
                    homeModuleAssuageGriefViewIsZan.playAnimation()
                } else {
                    homeModuleAssuageGriefViewIsZan.visibility = View.GONE
                    homeModuleAssuageGriefViewIsNotZan.visibility = View.VISIBLE
                }

                bean.zanCount =  if (bean.isZan == 1 ) bean.zanCount+1 else bean.zanCount-1
                homeModuleAssuageGriefViewZanCount.text = bean?.zanCount.toString()
                homeEvent?.askItemZanClick(position, index, bean.id.toString())
            }
        }

        homeModuleAssuageGriefItemViewIsFollow.setOnClickListener {
            //防止连击
            if (!Utils.isFastClick()) {
                if (bean.isFocused == 1) {
                    homeEvent?.askItemClick(bean)
                } else {
徐健 committed
109
                    if (!ModularServiceManager.provide(IUserService::class.java).isLogin()) {
徐健 committed
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 136 137 138 139 140 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 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
                        //跳转登录
                        YDLRouterManager.router(IYDLRouterConstant.ROUTER_MINE_LOGIN)
                        return@setOnClickListener
                    }
                    bean.isFocused = if(bean.isFocused == 1) 0 else 1
                    setFollowType(bean.isFocused == 1)
                    homeEvent?.askItemFocusClick(position, bean.uid.toString())
                }
            }
        }

        if (bean.uid == 0) {
            //匿名,隐藏关注按钮
            homeModuleAssuageGriefItemViewIsFollow.visibility = View.GONE
        } else {
            /**
             * 关注
             */
            if (bean.isFocused == 1) {
                setFollowType(true)
            } else {
                setFollowType(false)
            }
        }


        /**
         * 标题
         */
        if (!TextUtils.isEmpty(bean?.title)) {
            homeModuleAssuageGriefViewTitle.visibility = View.VISIBLE
            homeModuleAssuageGriefViewTitle.text = bean?.title
        }

        /**
         * 点赞
         */
        if (bean?.isZan == 1) {
            homeModuleAssuageGriefViewIsZan.visibility = View.VISIBLE
            homeModuleAssuageGriefViewIsZan.progress = 1.0f
            homeModuleAssuageGriefViewIsNotZan.visibility = View.GONE
        } else {
            homeModuleAssuageGriefViewIsZan.visibility = View.GONE
            homeModuleAssuageGriefViewIsNotZan.visibility = View.VISIBLE
        }

        /**
         * 图片
         */
        if (null != bean?.smallAttach && bean?.smallAttach!!.isNotEmpty()) {
            if (homeAssuageGriefImageItemView != null) {
                homeModuleAssuageGriefViewChildLayout.removeView(homeAssuageGriefImageItemView)
            }
            homeAssuageGriefImageItemView = HomeAssuageGriefImageItemView(mContext, bean?.smallAttach as List<String>, homeEvent)
            homeModuleAssuageGriefViewChildLayout.addView(homeAssuageGriefImageItemView, 1)
        } else {
            if (null != homeAssuageGriefImageItemView) {
                homeModuleAssuageGriefViewChildLayout.removeView(homeAssuageGriefImageItemView)
            }
        }

        /**
         * 评论
         */
        initComment(bean)

    }

    private fun setFollowType(boolean: Boolean) {
        if (boolean) {
            homeModuleAssuageGriefItemViewFollow.visibility = View.VISIBLE
            homeModuleAssuageGriefItemViewUnFollow.visibility = View.GONE
        } else {
            homeModuleAssuageGriefItemViewFollow.visibility = View.GONE
            homeModuleAssuageGriefItemViewUnFollow.visibility = View.VISIBLE
        }
    }

    private fun initComment(bean: HomeAskBean.DataBean) {
        if (bean?.commentsCount > 0) {
            homeModuleAssuageGriefViewComment.visibility = View.VISIBLE

            var index: Int = 0
            var commentBean = bean?.comments!![0]

            if (null != commentBean.name && commentBean.name!!.isNotEmpty())
                setPartTextColor(homeModuleAssuageGriefViewHotViewComment, commentBean.name,
                        0, commentBean.name?.length!!, Color.parseColor("#1DA1F2"))

            // userType为2 ,专家
            if (commentBean.userType == 2) {
                setImage(mContext, homeModuleAssuageGriefViewHotViewComment, "vip",
konghaorui committed
202
                        R.drawable.home_assuage_grief_item_user_type_v,
徐健 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
                        RxImageTool.dp2px(13f),
                        RxImageTool.dp2px(13f))
            }

            setAppendPartTextColor(homeModuleAssuageGriefViewHotViewComment, ":",
                    0,
                    1
                    , Color.parseColor("#1DA1F2"))

            MoonUtil.identifyFaceExpression(mContext, homeModuleAssuageGriefViewHotViewCommentContent,
                    commentBean.content, ImageSpan.ALIGN_BOTTOM)

            var emptyString = ""
            if (null != commentBean.name && commentBean.name!!.isNotEmpty()) {
                if (commentBean.userType == 2) {
                    emptyString += "缩"
                }
                for (index in 0 until commentBean.name!!.length) {
                    emptyString += commentBean.name!![index]
                }
                emptyString += "缩"
            }

            val span = SpannableStringBuilder(emptyString + homeModuleAssuageGriefViewHotViewCommentContent.text)
            span.setSpan(ForegroundColorSpan(Color.TRANSPARENT), 0, emptyString.length,
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
            homeModuleAssuageGriefViewHotViewCommentContent.text = span

        }
    }

    fun setPartTextColor(textView: TextView, text: String?,
                         start: Int, end: Int, colorId: Int) {
        var ss: SpannableString = SpannableString(text)
        ss.setSpan(ForegroundColorSpan(colorId), start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        textView.text = ss
    }

    fun setAppendPartTextColor(textView: TextView, text: String?,
                               start: Int, end: Int, colorId: Int) {
        var ss: SpannableString = SpannableString(text)
        ss.setSpan(ForegroundColorSpan(colorId), start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        textView.append(ss)
    }

    fun setImage(context: Context, textView: TextView, bitmapText: String, resId: Int, width: Int, height: Int) {
        var ss: SpannableString = SpannableString(bitmapText)
        var drawable: Drawable = context.resources.getDrawable(resId)
        drawable.setBounds(0, 0, width, height)
        var imageSpan: ImageSpan = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
        ss.setSpan(imageSpan, 0, bitmapText.length,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        textView.append(ss)
    }
}