Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package id.agrimart.agriino.activity.area
- import android.content.Context
- import android.os.Bundle
- import android.view.LayoutInflater
- import android.view.ViewGroup
- import android.widget.Toast
- import androidx.core.content.ContentProviderCompat.requireContext
- import androidx.navigation.findNavController
- import androidx.recyclerview.widget.RecyclerView
- import com.google.android.material.dialog.MaterialAlertDialogBuilder
- import id.agrimart.agriino.R
- import id.agrimart.agriino.config.util.Dialog
- import id.agrimart.agriino.databinding.DialogAreaBinding
- import id.agrimart.agriino.databinding.DialogMapBinding
- import id.agrimart.agriino.databinding.ItemAreaBinding
- import id.agrimart.agriino.databinding.ItemFarmBoundaryBinding
- import retrofit2.http.Tag
- class AreaAdapter(val context: Context, val list: MutableList<StreamArea>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
- companion object Expand {
- const val PARENT = 0
- const val CHILD = 1
- }
- inner class ParentViewHolder(val binding: ItemAreaBinding): RecyclerView.ViewHolder(binding.root) {
- fun parentBind(streamArea: StreamArea) {
- binding.apply {
- tvFarmDesc.text = streamArea.farm_description
- tvFarmAddress.text = "Alamat ${streamArea.address}"
- tvZip.text = "Zip Code ${streamArea.zip}"
- tbAddBoundary.setOnClickListener { it ->
- Dialog(it.context)
- .setDialog("Pilihan", "import atau buat baru?")
- .setPositiveButton("Baru"){ dialog, which ->
- val bundle = Bundle()
- bundle.getString("status", "N")
- bundle.putString("farm_id", streamArea.farm_id)
- bundle.putString("farm_desc", streamArea.farm_description)
- it.findNavController().navigate(R.id.action_areaFragment_to_areaDetail, bundle)
- }.setNegativeButton("Import Kml") { dialog, which ->
- val bundle = Bundle()
- bundle.getString("status", "I")
- bundle.putString("farm_id", streamArea.farm_id)
- bundle.putString("farm_desc", streamArea.farm_description)
- it.findNavController().navigate(R.id.action_areaFragment_to_areaDetail, bundle)
- }.setNeutralButton("Cancel") { dialog, which ->
- dialog?.dismiss()
- }
- .show()
- }
- ivExpandIcon.setImageResource(if (streamArea.isExpandable) R.drawable.baseline_keyboard_arrow_up_24 else R.drawable.baseline_keyboard_arrow_right_24)
- cardAreaParent.setOnClickListener {
- streamArea.isExpandable = !streamArea.isExpandable
- notifyDataSetChanged()
- }
- }
- }
- }
- inner class ChildViewHolder(val binding: ItemFarmBoundaryBinding): RecyclerView.ViewHolder(binding.root) {
- fun childBind(farmBoundary: FarmBoundary) {
- binding.apply {
- tvCountry.text = "${farmBoundary.country}"
- tvFarmTipe.text = "Type : ${farmBoundary.farm_type}"
- tbShowPolygonArea.setOnClickListener {
- if (farmBoundary.shape != null) {
- val bundle = Bundle()
- bundle.putString("farm_boundary", "${farmBoundary.farm_boundary}")
- bundle.putString("shape", farmBoundary.shape.toString())
- it.findNavController().navigate(R.id.action_areaFragment_to_mapArea, bundle)
- } else {
- Toast.makeText(it.context, "Area Masih belum di tambahkan", Toast.LENGTH_SHORT).show()
- }
- }
- }
- }
- }
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
- return if (viewType == PARENT) {
- val binding = ItemAreaBinding.inflate(LayoutInflater.from(context), parent, false)
- ParentViewHolder(binding)
- } else {
- val binding = ItemFarmBoundaryBinding.inflate(LayoutInflater.from(context), parent, false)
- ChildViewHolder(binding)
- }
- }
- override fun getItemCount(): Int {
- var count = 0
- for (farm in list) {
- count++ // For the parent item
- if (farm.isExpandable) {
- count += farm.farm_boundary.size // For the child items
- }
- }
- return count
- }
- override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
- val flatPosition = getFlatPosition(position)
- if (flatPosition.second == null) {
- (holder as ParentViewHolder).parentBind(list[flatPosition.first])
- } else {
- (holder as ChildViewHolder).childBind(list[flatPosition.first].farm_boundary[flatPosition.second!!])
- }
- }
- override fun getItemViewType(position: Int): Int {
- val flatPosition = getFlatPosition(position)
- return if (flatPosition.second == null) PARENT else CHILD
- }
- private fun getFlatPosition(flatPosition: Int): Pair<Int, Int?> {
- var count = 0
- for (i in list.indices) {
- if (count == flatPosition) return i to null
- count++
- if (list[i].isExpandable) {
- for (j in list[i].farm_boundary.indices) {
- if (count == flatPosition) return i to j
- count++
- }
- }
- }
- throw IllegalStateException("Invalid flat position")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement