Advertisement
Vassa007

Areaadapter

Jun 3rd, 2024 (edited)
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.78 KB | None | 0 0
  1. package id.agrimart.agriino.activity.area
  2.  
  3. import android.content.Context
  4. import android.os.Bundle
  5. import android.view.LayoutInflater
  6. import android.view.ViewGroup
  7. import android.widget.Toast
  8. import androidx.core.content.ContentProviderCompat.requireContext
  9. import androidx.navigation.findNavController
  10. import androidx.recyclerview.widget.RecyclerView
  11. import com.google.android.material.dialog.MaterialAlertDialogBuilder
  12. import id.agrimart.agriino.R
  13. import id.agrimart.agriino.config.util.Dialog
  14. import id.agrimart.agriino.databinding.DialogAreaBinding
  15. import id.agrimart.agriino.databinding.DialogMapBinding
  16. import id.agrimart.agriino.databinding.ItemAreaBinding
  17. import id.agrimart.agriino.databinding.ItemFarmBoundaryBinding
  18. import retrofit2.http.Tag
  19.  
  20. class AreaAdapter(val context: Context, val list: MutableList<StreamArea>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
  21.  
  22.     companion object Expand {
  23.         const val PARENT = 0
  24.         const val CHILD = 1
  25.     }
  26.     inner class ParentViewHolder(val binding: ItemAreaBinding): RecyclerView.ViewHolder(binding.root) {
  27.         fun parentBind(streamArea: StreamArea) {
  28.             binding.apply {
  29.                 tvFarmDesc.text = streamArea.farm_description
  30.                 tvFarmAddress.text = "Alamat ${streamArea.address}"
  31.                 tvZip.text = "Zip Code ${streamArea.zip}"
  32.                 tbAddBoundary.setOnClickListener { it ->
  33.  
  34.                     Dialog(it.context)
  35.                         .setDialog("Pilihan", "import atau buat baru?")
  36.                         .setPositiveButton("Baru"){ dialog, which ->
  37.                             val bundle = Bundle()
  38.                             bundle.getString("status", "N")
  39.                             bundle.putString("farm_id", streamArea.farm_id)
  40.                             bundle.putString("farm_desc", streamArea.farm_description)
  41.                             it.findNavController().navigate(R.id.action_areaFragment_to_areaDetail, bundle)
  42.                         }.setNegativeButton("Import Kml") { dialog, which ->
  43.                             val bundle = Bundle()
  44.                             bundle.getString("status", "I")
  45.                             bundle.putString("farm_id", streamArea.farm_id)
  46.                             bundle.putString("farm_desc", streamArea.farm_description)
  47.                             it.findNavController().navigate(R.id.action_areaFragment_to_areaDetail, bundle)
  48.                         }.setNeutralButton("Cancel") { dialog, which ->
  49.                             dialog?.dismiss()
  50.                         }
  51.                         .show()
  52.                 }
  53.  
  54.                 ivExpandIcon.setImageResource(if (streamArea.isExpandable) R.drawable.baseline_keyboard_arrow_up_24 else R.drawable.baseline_keyboard_arrow_right_24)
  55.  
  56.                 cardAreaParent.setOnClickListener {
  57.                     streamArea.isExpandable = !streamArea.isExpandable
  58.                     notifyDataSetChanged()
  59.                 }
  60.             }
  61.         }
  62.     }
  63.  
  64.     inner class ChildViewHolder(val binding: ItemFarmBoundaryBinding): RecyclerView.ViewHolder(binding.root) {
  65.         fun childBind(farmBoundary: FarmBoundary) {
  66.             binding.apply {
  67.                 tvCountry.text = "${farmBoundary.country}"
  68.                 tvFarmTipe.text = "Type : ${farmBoundary.farm_type}"
  69.  
  70.                 tbShowPolygonArea.setOnClickListener {
  71.                     if (farmBoundary.shape != null) {
  72.                         val bundle = Bundle()
  73.                         bundle.putString("farm_boundary", "${farmBoundary.farm_boundary}")
  74.                         bundle.putString("shape", farmBoundary.shape.toString())
  75.                         it.findNavController().navigate(R.id.action_areaFragment_to_mapArea, bundle)
  76.                     } else {
  77.                         Toast.makeText(it.context, "Area Masih belum di tambahkan", Toast.LENGTH_SHORT).show()
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.  
  83.     }
  84.  
  85.     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
  86.         return if (viewType == PARENT) {
  87.             val binding = ItemAreaBinding.inflate(LayoutInflater.from(context), parent, false)
  88.             ParentViewHolder(binding)
  89.         } else {
  90.             val binding = ItemFarmBoundaryBinding.inflate(LayoutInflater.from(context), parent, false)
  91.             ChildViewHolder(binding)
  92.         }
  93.     }
  94.  
  95.     override fun getItemCount(): Int {
  96.         var count = 0
  97.         for (farm in list) {
  98.             count++ // For the parent item
  99.             if (farm.isExpandable) {
  100.                 count += farm.farm_boundary.size // For the child items
  101.             }
  102.         }
  103.         return count
  104.     }
  105.  
  106.     override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
  107.         val flatPosition = getFlatPosition(position)
  108.         if (flatPosition.second == null) {
  109.             (holder as ParentViewHolder).parentBind(list[flatPosition.first])
  110.         } else {
  111.             (holder as ChildViewHolder).childBind(list[flatPosition.first].farm_boundary[flatPosition.second!!])
  112.         }
  113.     }
  114.  
  115.     override fun getItemViewType(position: Int): Int {
  116.         val flatPosition = getFlatPosition(position)
  117.         return if (flatPosition.second == null) PARENT else CHILD
  118.     }
  119.  
  120.     private fun getFlatPosition(flatPosition: Int): Pair<Int, Int?> {
  121.         var count = 0
  122.         for (i in list.indices) {
  123.             if (count == flatPosition) return i to null
  124.             count++
  125.             if (list[i].isExpandable) {
  126.                 for (j in list[i].farm_boundary.indices) {
  127.                     if (count == flatPosition) return i to j
  128.                     count++
  129.                 }
  130.             }
  131.         }
  132.         throw IllegalStateException("Invalid flat position")
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement