package com.cheng.blzb.ui.fragment.push.subscribe.source

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cheng.blzb.R
import com.cheng.blzb.bean.SourceEntity
import com.example.base.extensions.getColor
import com.example.base.utils.SpanUtils

class SearchSourceAdapter: BaseQuickAdapter<SourceEntity, BaseViewHolder>(R.layout.listitem_search_source_result) {
    private var keyword: String = ""

    override fun convert(holder: BaseViewHolder, item: SourceEntity) {
        val name = item.name
        val index = name.indexOf(keyword)
        if (index != -1) {
            val startText = name.take(index)
            val endText = name.substring(index + keyword.length, name.length)
            SpanUtils.with(holder.getView(R.id.tv_name))
                .append(startText)
                .append(keyword)
                .setForegroundColor(getColor(R.color.color_ff493c))
                .append(endText)
                .create()
        } else {
            holder.setText(R.id.tv_name, name)
        }
    }

    fun setKeyword(keyword: String) {
        this.keyword = keyword
    }
}