aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-03-10 20:33:40 -0600
committerIván Ávalos <avalos@disroot.org>2022-03-10 20:33:40 -0600
commit18987c3b31c2b8fe1a911297e8b104d583dd5c83 (patch)
tree43bc8d56c873fb1c6b4f56f3750f45a60519984d /androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices
parent9b8eee4362c368e682bfe18d5cef44d6b9d109bf (diff)
downloadetbsa-trackermap-mobile-18987c3b31c2b8fe1a911297e8b104d583dd5c83.tar.gz
etbsa-trackermap-mobile-18987c3b31c2b8fe1a911297e8b104d583dd5c83.tar.bz2
etbsa-trackermap-mobile-18987c3b31c2b8fe1a911297e8b104d583dd5c83.zip
- Card on map now updates info when there is a change
- Huge device list performance improvements on search and updates
Diffstat (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices')
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt11
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesDiffCallback.kt20
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt7
3 files changed, 32 insertions, 6 deletions
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
index 4cfd6b5..0fbf597 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt
@@ -20,6 +20,7 @@ package mx.trackermap.TrackerMap.android.devices
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.updateLayoutParams
+import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.android.databinding.UnitItemBinding
@@ -28,10 +29,18 @@ import mx.trackermap.TrackerMap.android.shared.UnitRenderData
import mx.trackermap.TrackerMap.client.models.UnitInformation
class DevicesAdapter(
- private val units: List<UnitInformation>,
+ private val units: MutableList<UnitInformation>,
private val actionCallback: ActionCallback?
) : RecyclerView.Adapter<DevicesAdapter.ViewHolder>() {
+ fun setData(units: List<UnitInformation>) {
+ val diffCallback = DevicesDiffCallback(this.units, units)
+ val diffResult = DiffUtil.calculateDiff(diffCallback)
+ this.units.clear()
+ this.units.addAll(units)
+ diffResult.dispatchUpdatesTo(this)
+ }
+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = UnitItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesDiffCallback.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesDiffCallback.kt
new file mode 100644
index 0000000..40e6e9c
--- /dev/null
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesDiffCallback.kt
@@ -0,0 +1,20 @@
+package mx.trackermap.TrackerMap.android.devices
+
+import androidx.recyclerview.widget.DiffUtil
+import mx.trackermap.TrackerMap.client.models.UnitInformation
+
+class DevicesDiffCallback(
+ private val oldList: List<UnitInformation>,
+ private val newList: List<UnitInformation>
+): DiffUtil.Callback() {
+ override fun getOldListSize() = oldList.size
+
+ override fun getNewListSize() = newList.size
+
+ override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
+ oldList[oldItemPosition].device.id == newList[newItemPosition].device.id
+
+ override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
+ (oldList[oldItemPosition].position?.id == newList[newItemPosition].position?.id) && newItemPosition != 0
+
+} \ 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 ad93429..e5143cb 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
@@ -83,7 +83,7 @@ class DevicesFragment : Fragment() {
context, LinearLayoutManager.VERTICAL,
false
)
- binding.devicesList.adapter = DevicesAdapter(emptyList(), null)
+ binding.devicesList.adapter = DevicesAdapter(mutableListOf(), this::itemAction)
binding.infoLoading.root.visibility = View.VISIBLE
}
@@ -93,10 +93,7 @@ class DevicesFragment : Fragment() {
unitsViewModel.units.observe(viewLifecycleOwner) { units ->
Log.d("DevicesFragment", "Success $units")
binding.infoLoading.root.visibility = View.GONE
- binding.devicesList.swapAdapter(
- DevicesAdapter(units, this::itemAction),
- false
- )
+ (binding.devicesList.adapter as DevicesAdapter).setData(units)
}
}