From 6674ec486e3ffcf0a3d2a19e7f7bf7f963722264 Mon Sep 17 00:00:00 2001 From: Isidro Henoch Date: Tue, 14 Dec 2021 19:01:28 -0600 Subject: Creates the RV adapter to display the units on screen --- .../TrackerMap/android/devices/DevicesAdapter.kt | 46 ++++++++++++++++++++++ .../TrackerMap/android/devices/DevicesFragment.kt | 8 ++++ 2 files changed, 54 insertions(+) create mode 100644 androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt (limited to 'androidApp/src/main/java/mx/trackermap') diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt new file mode 100644 index 0000000..72d61e3 --- /dev/null +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt @@ -0,0 +1,46 @@ +package mx.trackermap.TrackerMap.android.devices + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import mx.trackermap.TrackerMap.android.databinding.UnitItemBinding +import mx.trackermap.TrackerMap.client.models.UnitInformation + +class DevicesAdapter(private val units: List) : + RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val binding = UnitItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) + + return ViewHolder(binding) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.toggleOptions(false) + + val unit = units[position] + holder.binding.apply { + unitName.text = unit.device.name + driverName.text = unit.device.contact + unitSpeed.text = "${unit.position?.speed ?: "--"} Km/h" + lastAddress.text = unit.position?.address ?: "Unknown location" + lastDate.text = "yyyy/mm/dd, hh:mm" + } + } + + override fun getItemCount(): Int = units.size + + inner class ViewHolder(val binding: UnitItemBinding) : RecyclerView.ViewHolder(binding.root) { + init { + binding.unitCard.setOnClickListener { + toggleOptions(binding.itemOptions.visibility == View.GONE) + } + } + + fun toggleOptions(shouldShow: Boolean) { + binding.itemOptions.visibility = if (shouldShow) View.VISIBLE else View.GONE + binding.unitCard.cardElevation = if (shouldShow) 5.0F else 0.0F + } + } +} \ No newline at end of file diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt index 98c98cc..4ac5d48 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt @@ -6,6 +6,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.databinding.DevicesFragmentBinding import mx.trackermap.TrackerMap.android.units.UnitsViewModel @@ -34,13 +35,20 @@ class DevicesFragment: Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + setupList() setupObservers() } + private fun setupList() { + binding.devicesList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) + binding.devicesList.adapter = DevicesAdapter(emptyList()) + } + @DelicateCoroutinesApi private fun setupObservers() { unitsViewModel.units.observe(this) { units -> Log.d("DevicesFragment", "Success $units") + binding.devicesList.swapAdapter(DevicesAdapter(units), false) } } } \ No newline at end of file -- cgit v1.2.3