[Android] DiffUtil: Simplify code

Geoffrey Métais git at videolan.org
Fri Jul 27 17:07:19 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jul 27 17:06:50 2018 +0200| [8a8505d5f1ba8215ad1f4bc459656ba546a623b8] | committer: Geoffrey Métais

DiffUtil: Simplify code

> https://code.videolan.org/videolan/vlc-android/commit/8a8505d5f1ba8215ad1f4bc459656ba546a623b8
---

 .../src/org/videolan/vlc/gui/DiffUtilAdapter.kt    | 26 +++++++++++-----------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt
index 8c65b429f..d57a40590 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt
@@ -1,21 +1,20 @@
 package org.videolan.vlc.gui
 
 import android.support.annotation.MainThread
-import android.support.annotation.WorkerThread
 import android.support.v7.util.DiffUtil
 import android.support.v7.widget.RecyclerView
+import kotlinx.coroutines.experimental.CommonPool
 import kotlinx.coroutines.experimental.android.UI
 import kotlinx.coroutines.experimental.channels.Channel
 import kotlinx.coroutines.experimental.channels.actor
-import kotlinx.coroutines.experimental.launch
-import java.util.*
+import kotlinx.coroutines.experimental.withContext
 
 abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>() {
 
     protected var dataset: List<D> = listOf()
     private set
     private val diffCallback by lazy(LazyThreadSafetyMode.NONE) { createCB() }
-    private val updateActor = actor<List<D>>(capacity = Channel.CONFLATED) {
+    private val updateActor = actor<List<D>>(UI, Channel.CONFLATED) {
         for (list in channel) internalUpdate(list)
     }
     protected abstract fun onUpdateFinished()
@@ -25,18 +24,19 @@ abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.A
         updateActor.offer(list)
     }
 
-    @WorkerThread
+    @MainThread
     private suspend fun internalUpdate(list: List<D>) {
-        val finalList = prepareList(list)
-        val result = DiffUtil.calculateDiff(diffCallback.apply { update(dataset, finalList) }, detectMoves())
-        launch(UI) {
-            dataset = finalList
-            result.dispatchUpdatesTo(this at DiffUtilAdapter)
-            onUpdateFinished()
-        }.join()
+        val (finalList, result) = withContext(CommonPool) {
+            val finalList = prepareList(list)
+            val result = DiffUtil.calculateDiff(diffCallback.apply { update(dataset, finalList) }, detectMoves())
+            Pair(finalList, result)
+        }
+        dataset = finalList
+        result.dispatchUpdatesTo(this at DiffUtilAdapter)
+        onUpdateFinished()
     }
 
-    protected open fun prepareList(list: List<D>) : List<D> = ArrayList(list)
+    protected open fun prepareList(list: List<D>) : List<D> = list.toList()
 
     @MainThread
     fun isEmpty() = dataset.isEmpty()



More information about the Android mailing list