Advertisement
max745

Untitled

Oct 31st, 2023
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.53 KB | None | 0 0
  1. class AccountRecyclerAdapter(val context: Context?= null) :
  2.     ListAdapter<Any, AccountRecyclerAdapter.DataHolder>(ItemComparator()) {
  3.  
  4.     private val listOfSelectedCardViews = arrayListOf<MaterialCardView>()
  5.  
  6.     private var listener: OnRecyclerItemClickListener? = null
  7.  
  8.     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
  9.         DataHolder(
  10.             ItemBsOrSiBinding.inflate(
  11.                 LayoutInflater.from(parent.context),
  12.                 parent,
  13.                 false
  14.             )
  15.         )
  16.  
  17.     override fun onBindViewHolder(holder: DataHolder, position: Int) {
  18.         val currentItem = getItem(position)
  19.         if (currentItem is ResultSILA) {
  20.             holder.bind(currentItem)
  21.             holder.itemView.setOnClickListener {
  22.                 listener?.onItemClick(position)
  23.             }
  24.             holder.itemView.setOnLongClickListener {
  25.                 listener?.onLongItemClick(position)
  26.  
  27.                 val cardView = holder.itemView.findViewById<MaterialCardView>(R.id.card_view_item_bs_or_si)
  28.                 listOfSelectedCardViews.add(cardView)
  29.                 if(cardView.cardBackgroundColor == ContextCompat.getColorStateList(context!!, R.color.red_700_with_shading))
  30.                     cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.transparent))
  31.                 else if(cardView.cardBackgroundColor == ContextCompat.getColorStateList(context, R.color.transparent))
  32.                     cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.red_700_with_shading))
  33.  
  34.                 true
  35.             }
  36.         } else if (currentItem is ResultBSLA) {
  37.             holder.bind(currentItem)
  38.             holder.itemView.setOnClickListener {
  39.                 listener?.onItemClick(position)
  40.             }
  41.             holder.itemView.setOnLongClickListener {
  42.                 listener?.onLongItemClick(position)
  43.  
  44.                 val cardView = holder.itemView.findViewById<MaterialCardView>(R.id.card_view_item_bs_or_si)
  45.                 listOfSelectedCardViews.add(cardView)
  46.                 if(cardView.cardBackgroundColor == ContextCompat.getColorStateList(context!!, R.color.red_700_with_shading))
  47.                     cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.transparent))
  48.                 else if(cardView.cardBackgroundColor == ContextCompat.getColorStateList(context, R.color.transparent))
  49.                     cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.red_700_with_shading))
  50.  
  51.                 true
  52.             }
  53.         }
  54.     }
  55.  
  56.     fun setOnItemClickListener(listener: OnRecyclerItemClickListener?) {
  57.         this.listener = listener
  58.     }
  59.  
  60.     fun clearSelectedCardViewsBackground() {
  61.         listOfSelectedCardViews.forEach {
  62.             it.setCardBackgroundColor(ContextCompat.getColor(context!!, R.color.transparent))
  63.         }
  64.     }
  65.  
  66.     // converts "Mon, 14 Aug 2023 11:23:29 GMT" to "14.08.2023"
  67.     private fun formatDateShort(date: String?): String {
  68.         if(date == null)
  69.             return ""
  70.         val inputDate = date
  71.         val inputFormat = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US)
  72.         val date = inputFormat.parse(inputDate)
  73.  
  74.         val outputFormat = SimpleDateFormat("dd.MM.yyyy HH:mm:ss", Locale.US)
  75.         val outputDate = outputFormat.format(date)
  76.         return outputDate
  77.     }
  78.  
  79.     @SuppressLint("DiffUtilEquals")
  80.     class ItemComparator : DiffUtil.ItemCallback<Any>() {
  81.         override fun areItemsTheSame(
  82.             oldItem: Any,
  83.             newItem: Any
  84.         ): Boolean {
  85.             return oldItem === newItem
  86.         }
  87.  
  88.         override fun areContentsTheSame(
  89.             oldItem: Any,
  90.             newItem: Any
  91.         ): Boolean {
  92.             return oldItem == newItem
  93.         }
  94.     }
  95.  
  96.     inner class DataHolder(private val binding: ItemBsOrSiBinding) :
  97.         RecyclerView.ViewHolder(binding.root) {
  98.  
  99.         private fun pxFromDp(dp: Int): Int {
  100.             return (dp * context!!.resources.displayMetrics.density).toInt()
  101.         }
  102.  
  103.         fun bind(item: ResultSILA) = with(binding) {
  104.             if(item.name == null) {
  105.                 itemBsOrSiTv.text = item.sensorId.toString()
  106.                 itemBsOrSiId.visibility = View.GONE
  107.  
  108.                 val param = itemBsOrSiTv.layoutParams as ViewGroup.MarginLayoutParams
  109.                 param.setMargins(pxFromDp(20),pxFromDp(5),pxFromDp(20),pxFromDp(10))
  110.                 itemBsOrSiTv.layoutParams = param
  111.             } else {
  112.                 itemBsOrSiTv.text = item.name.toString()
  113.                 itemBsOrSiId.text = item.sensorId.toString()
  114.             }
  115.  
  116.             dateBsOrSiTv.text = formatDateShort(item.dtLastConnected)
  117.  
  118.             if (item.colorT == "yellow")
  119.                 itemBsOrSiTemperature.setTextColor(ResourcesCompat.getColor(context!!.resources, R.color.yellow_notification, null))
  120.             else if (item.colorT == "red")
  121.                 itemBsOrSiTemperature.setTextColor(ResourcesCompat.getColor(context!!.resources, R.color.red_notification, null))
  122.             itemBsOrSiTemperature.text = item.temperature+"°C"
  123.  
  124.             if (item.colorH == "yellow")
  125.                 itemBsOrSiHumidity.setTextColor(ResourcesCompat.getColor(context!!.resources, R.color.yellow_notification, null))
  126.             else if (item.colorH == "red")
  127.                 itemBsOrSiHumidity.setTextColor(ResourcesCompat.getColor(context!!.resources, R.color.red_notification, null))
  128.             itemBsOrSiHumidity.text = item.humidity+"%"
  129.         }
  130.  
  131.         fun bind(item: ResultBSLA) = with(binding) {
  132.             itemBsOrSiTv.text = item.bsId.toString()
  133.             dateBsOrSiTv.text = formatDateShort(item.dtLastConnected)
  134.  
  135.             itemBsOrSiId.visibility = View.GONE
  136.             val param = itemBsOrSiTv.layoutParams as ViewGroup.MarginLayoutParams
  137.             param.setMargins(pxFromDp(20),pxFromDp(5),pxFromDp(20),pxFromDp(10))
  138.             itemBsOrSiTv.layoutParams = param
  139.  
  140.             if(item.batVoltage != "Нет информации") {
  141.                 itemBsOrSiHumidityText.text = context!!.resources.getString(R.string.voltage)
  142.                 itemBsOrSiHumidity.text = item.batVoltage+"B"
  143.             } else
  144.                 itemBsOrSiHumidityContainer.visibility = View.GONE
  145.  
  146.             itemBsOrSiTemperatureContainer.visibility = View.GONE
  147.             itemBsOrSiSeparator1.visibility = View.GONE
  148.         }
  149.     }
  150. }
  151.  
  152. interface OnRecyclerItemClickListener {
  153.     fun onItemClick(position: Int)
  154.  
  155.     fun onLongItemClick(position: Int)
  156. }
Tags: bebe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement