[Android] Streams: Implement DiffUtil

Geoffrey Métais git at videolan.org
Tue Mar 24 09:41:09 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Mar 19 16:44:28 2020 +0100| [5421e6d2aa681ef4bfb7a60ce1f77b06da098374] | committer: Nicolas Pomepuy

Streams: Implement DiffUtil

Because it's nicer

> https://code.videolan.org/videolan/vlc-android/commit/5421e6d2aa681ef4bfb7a60ce1f77b06da098374
---

 .../src/org/videolan/vlc/gui/network/MRLAdapter.kt | 43 ++++++++++++----------
 .../videolan/vlc/gui/network/MRLPanelFragment.kt   |  2 +-
 2 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.kt b/application/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.kt
index 2de59c785..138af806f 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.kt
@@ -30,12 +30,9 @@ import kotlinx.coroutines.channels.SendChannel
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.MrlItemBinding
+import org.videolan.vlc.gui.DiffUtilAdapter
 
-internal class MRLAdapter(private val eventActor: SendChannel<MrlAction>) : RecyclerView.Adapter<MRLAdapter.ViewHolder>() {
-    private var dataset: List<MediaWrapper>? = null
-
-    val isEmpty: Boolean
-        get() = itemCount == 0
+internal class MRLAdapter(private val eventActor: SendChannel<MrlAction>) : DiffUtilAdapter<MediaWrapper, MRLAdapter.ViewHolder>() {
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MRLAdapter.ViewHolder {
         val inflater = LayoutInflater.from(parent.context)
@@ -44,24 +41,21 @@ internal class MRLAdapter(private val eventActor: SendChannel<MrlAction>) : Recy
     }
 
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
-        val item = dataset?.get(position)
-        holder.binding.mrlItemUri.text = Uri.decode(item?.location)
-        holder.binding.mrlItemTitle.text = Uri.decode(item?.title)
-    }
-
-    fun setList(list: List<MediaWrapper>?) {
-        dataset = list
-        notifyDataSetChanged()
+        val item = dataset.get(position)
+        holder.binding.mrlItemUri.text = Uri.decode(item.location)
+        holder.binding.mrlItemTitle.text = Uri.decode(item.title)
     }
 
-    fun getItem(position: Int): MediaWrapper? = when {
-        position >= itemCount -> null
-        position < 0 -> null
-        else -> dataset?.get(position)
+    override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
+        if (payloads.isNullOrEmpty()) {
+            onBindViewHolder(holder, position)
+            return
+        }
+        for (payload in payloads) {
+            if (payload is String) holder.binding.mrlItemTitle.text = payload
+        }
     }
 
-    override fun getItemCount() = dataset?.size ?: 0
-
     inner class ViewHolder(val binding: MrlItemBinding) : RecyclerView.ViewHolder(binding.root), View.OnClickListener {
 
         init {
@@ -71,9 +65,18 @@ internal class MRLAdapter(private val eventActor: SendChannel<MrlAction>) : Recy
         }
 
         override fun onClick(v: View) {
-            dataset?.get(layoutPosition)?.let { eventActor.offer(Playmedia(it)) }
+            dataset.get(layoutPosition).let { eventActor.offer(Playmedia(it)) }
         }
     }
+
+    override fun createCB() = object : DiffCallback<MediaWrapper>() {
+        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
+            return oldList[oldItemPosition] == newList[newItemPosition]
+                    && oldList[oldItemPosition].title == newList[newItemPosition].title
+        }
+
+        override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int) = newList[newItemPosition].title
+    }
 }
 
 sealed class MrlAction
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
index e33f7f9f5..77ba74c76 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
@@ -122,7 +122,7 @@ class MRLPanelFragment : Fragment(), View.OnKeyListener, TextView.OnEditorAction
 
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
-        viewModel.dataset.observe(requireActivity(), Observer { adapter.setList(it as List<MediaWrapper>) })
+        viewModel.dataset.observe(requireActivity(), Observer { adapter.update(it) })
         viewModel.loading.observe(requireActivity(), Observer { (activity as? MainActivity)?.refreshing = it })
     }
 



More information about the Android mailing list