Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AccountRecyclerAdapter(val context: Context?= null) :
- ListAdapter<Any, AccountRecyclerAdapter.DataHolder>(ItemComparator()) {
- private val listOfSelectedCardViews = arrayListOf<MaterialCardView>()
- private var listener: OnRecyclerItemClickListener? = null
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
- DataHolder(
- ItemBsOrSiBinding.inflate(
- LayoutInflater.from(parent.context),
- parent,
- false
- )
- )
- override fun onBindViewHolder(holder: DataHolder, position: Int) {
- val currentItem = getItem(position)
- if (currentItem is ResultSILA) {
- holder.bind(currentItem)
- holder.itemView.setOnClickListener {
- listener?.onItemClick(position)
- }
- holder.itemView.setOnLongClickListener {
- listener?.onLongItemClick(position)
- val cardView = holder.itemView.findViewById<MaterialCardView>(R.id.card_view_item_bs_or_si)
- listOfSelectedCardViews.add(cardView)
- if(cardView.cardBackgroundColor == ContextCompat.getColorStateList(context!!, R.color.red_700_with_shading))
- cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.transparent))
- else if(cardView.cardBackgroundColor == ContextCompat.getColorStateList(context, R.color.transparent))
- cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.red_700_with_shading))
- true
- }
- } else if (currentItem is ResultBSLA) {
- holder.bind(currentItem)
- holder.itemView.setOnClickListener {
- listener?.onItemClick(position)
- }
- holder.itemView.setOnLongClickListener {
- listener?.onLongItemClick(position)
- val cardView = holder.itemView.findViewById<MaterialCardView>(R.id.card_view_item_bs_or_si)
- listOfSelectedCardViews.add(cardView)
- if(cardView.cardBackgroundColor == ContextCompat.getColorStateList(context!!, R.color.red_700_with_shading))
- cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.transparent))
- else if(cardView.cardBackgroundColor == ContextCompat.getColorStateList(context, R.color.transparent))
- cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.red_700_with_shading))
- true
- }
- }
- }
- fun setOnItemClickListener(listener: OnRecyclerItemClickListener?) {
- this.listener = listener
- }
- fun clearSelectedCardViewsBackground() {
- listOfSelectedCardViews.forEach {
- it.setCardBackgroundColor(ContextCompat.getColor(context!!, R.color.transparent))
- }
- }
- // converts "Mon, 14 Aug 2023 11:23:29 GMT" to "14.08.2023"
- private fun formatDateShort(date: String?): String {
- if(date == null)
- return ""
- val inputDate = date
- val inputFormat = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US)
- val date = inputFormat.parse(inputDate)
- val outputFormat = SimpleDateFormat("dd.MM.yyyy HH:mm:ss", Locale.US)
- val outputDate = outputFormat.format(date)
- return outputDate
- }
- @SuppressLint("DiffUtilEquals")
- class ItemComparator : DiffUtil.ItemCallback<Any>() {
- override fun areItemsTheSame(
- oldItem: Any,
- newItem: Any
- ): Boolean {
- return oldItem === newItem
- }
- override fun areContentsTheSame(
- oldItem: Any,
- newItem: Any
- ): Boolean {
- return oldItem == newItem
- }
- }
- inner class DataHolder(private val binding: ItemBsOrSiBinding) :
- RecyclerView.ViewHolder(binding.root) {
- private fun pxFromDp(dp: Int): Int {
- return (dp * context!!.resources.displayMetrics.density).toInt()
- }
- fun bind(item: ResultSILA) = with(binding) {
- if(item.name == null) {
- itemBsOrSiTv.text = item.sensorId.toString()
- itemBsOrSiId.visibility = View.GONE
- val param = itemBsOrSiTv.layoutParams as ViewGroup.MarginLayoutParams
- param.setMargins(pxFromDp(20),pxFromDp(5),pxFromDp(20),pxFromDp(10))
- itemBsOrSiTv.layoutParams = param
- } else {
- itemBsOrSiTv.text = item.name.toString()
- itemBsOrSiId.text = item.sensorId.toString()
- }
- dateBsOrSiTv.text = formatDateShort(item.dtLastConnected)
- if (item.colorT == "yellow")
- itemBsOrSiTemperature.setTextColor(ResourcesCompat.getColor(context!!.resources, R.color.yellow_notification, null))
- else if (item.colorT == "red")
- itemBsOrSiTemperature.setTextColor(ResourcesCompat.getColor(context!!.resources, R.color.red_notification, null))
- itemBsOrSiTemperature.text = item.temperature+"°C"
- if (item.colorH == "yellow")
- itemBsOrSiHumidity.setTextColor(ResourcesCompat.getColor(context!!.resources, R.color.yellow_notification, null))
- else if (item.colorH == "red")
- itemBsOrSiHumidity.setTextColor(ResourcesCompat.getColor(context!!.resources, R.color.red_notification, null))
- itemBsOrSiHumidity.text = item.humidity+"%"
- }
- fun bind(item: ResultBSLA) = with(binding) {
- itemBsOrSiTv.text = item.bsId.toString()
- dateBsOrSiTv.text = formatDateShort(item.dtLastConnected)
- itemBsOrSiId.visibility = View.GONE
- val param = itemBsOrSiTv.layoutParams as ViewGroup.MarginLayoutParams
- param.setMargins(pxFromDp(20),pxFromDp(5),pxFromDp(20),pxFromDp(10))
- itemBsOrSiTv.layoutParams = param
- if(item.batVoltage != "Нет информации") {
- itemBsOrSiHumidityText.text = context!!.resources.getString(R.string.voltage)
- itemBsOrSiHumidity.text = item.batVoltage+"B"
- } else
- itemBsOrSiHumidityContainer.visibility = View.GONE
- itemBsOrSiTemperatureContainer.visibility = View.GONE
- itemBsOrSiSeparator1.visibility = View.GONE
- }
- }
- }
- interface OnRecyclerItemClickListener {
- fun onItemClick(position: Int)
- fun onLongItemClick(position: Int)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement