96 lines
3.7 KiB
Kotlin
96 lines
3.7 KiB
Kotlin
package com.simplemobiletools.smsmessenger.adapters
|
|
|
|
import android.util.TypedValue
|
|
import android.view.Menu
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import com.bumptech.glide.Glide
|
|
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
|
import com.simplemobiletools.commons.extensions.getTextSize
|
|
import com.simplemobiletools.commons.extensions.highlightTextPart
|
|
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
|
|
import com.simplemobiletools.commons.views.MyRecyclerView
|
|
import com.simplemobiletools.smsmessenger.R
|
|
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
|
import com.simplemobiletools.smsmessenger.models.SearchResult
|
|
import kotlinx.android.synthetic.main.item_search_result.view.*
|
|
import java.util.*
|
|
|
|
class SearchResultsAdapter(
|
|
activity: SimpleActivity, var searchResults: ArrayList<SearchResult>, recyclerView: MyRecyclerView, highlightText: String, itemClick: (Any) -> Unit
|
|
) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
|
|
|
|
private var fontSize = activity.getTextSize()
|
|
private var textToHighlight = highlightText
|
|
|
|
override fun getActionMenuId() = 0
|
|
|
|
override fun prepareActionMode(menu: Menu) {}
|
|
|
|
override fun actionItemPressed(id: Int) {}
|
|
|
|
override fun getSelectableItemCount() = searchResults.size
|
|
|
|
override fun getIsItemSelectable(position: Int) = false
|
|
|
|
override fun getItemSelectionKey(position: Int) = searchResults.getOrNull(position)?.hashCode()
|
|
|
|
override fun getItemKeyPosition(key: Int) = searchResults.indexOfFirst { it.hashCode() == key }
|
|
|
|
override fun onActionModeCreated() {}
|
|
|
|
override fun onActionModeDestroyed() {}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_search_result, parent)
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
val searchResult = searchResults[position]
|
|
holder.bindView(searchResult, true, false) { itemView, layoutPosition ->
|
|
setupView(itemView, searchResult)
|
|
}
|
|
bindViewHolder(holder)
|
|
}
|
|
|
|
override fun getItemCount() = searchResults.size
|
|
|
|
fun updateItems(newItems: ArrayList<SearchResult>, highlightText: String = "") {
|
|
if (newItems.hashCode() != searchResults.hashCode()) {
|
|
searchResults = newItems.clone() as ArrayList<SearchResult>
|
|
textToHighlight = highlightText
|
|
notifyDataSetChanged()
|
|
} else if (textToHighlight != highlightText) {
|
|
textToHighlight = highlightText
|
|
notifyDataSetChanged()
|
|
}
|
|
}
|
|
|
|
private fun setupView(view: View, searchResult: SearchResult) {
|
|
view.apply {
|
|
search_result_title.apply {
|
|
text = searchResult.title.highlightTextPart(textToHighlight, adjustedPrimaryColor)
|
|
setTextColor(textColor)
|
|
setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize * 1.2f)
|
|
}
|
|
|
|
search_result_snippet.apply {
|
|
text = searchResult.snippet.highlightTextPart(textToHighlight, adjustedPrimaryColor)
|
|
setTextColor(textColor)
|
|
setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize * 0.9f)
|
|
}
|
|
|
|
search_result_date.apply {
|
|
text = searchResult.date
|
|
setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize * 0.8f)
|
|
}
|
|
|
|
SimpleContactsHelper(context).loadContactImage(searchResult.photoUri, search_result_image, searchResult.title)
|
|
}
|
|
}
|
|
|
|
override fun onViewRecycled(holder: ViewHolder) {
|
|
super.onViewRecycled(holder)
|
|
if (!activity.isDestroyed && !activity.isFinishing && holder.itemView.search_result_image != null) {
|
|
Glide.with(activity).clear(holder.itemView.search_result_image)
|
|
}
|
|
}
|
|
}
|