[Android] Folders: Prevent IllegalArgumentException

Geoffrey Métais git at videolan.org
Thu Oct 17 14:48:58 CEST 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Oct 17 14:48:14 2019 +0200| [fa8a38f9fee9066bcbf302eefc828009e842f80d] | committer: Geoffrey Métais

Folders: Prevent IllegalArgumentException

Coroutines dispatching could cause desync operations

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

 .../src/org/videolan/vlc/gui/folders/FoldersAdapter.kt    | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/folders/FoldersAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/folders/FoldersAdapter.kt
index 1ea06f72e..f2db1d1b4 100644
--- a/vlc-android/src/org/videolan/vlc/gui/folders/FoldersAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/folders/FoldersAdapter.kt
@@ -7,11 +7,8 @@ import android.view.View
 import android.view.ViewGroup
 import androidx.paging.PagedListAdapter
 import androidx.recyclerview.widget.DiffUtil
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.*
 import kotlinx.coroutines.channels.SendChannel
-import kotlinx.coroutines.launch
-import kotlinx.coroutines.withContext
 import org.videolan.libvlc.util.AndroidUtil
 import org.videolan.medialibrary.interfaces.media.AbstractFolder
 import org.videolan.tools.MultiSelectAdapter
@@ -36,10 +33,11 @@ class FoldersAdapter(val actor: SendChannel<FolderAction>) : PagedListAdapter<Ab
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         val folder = getItem(position)
         holder.binding.folder = folder
-        launch {
+        holder.job = launch(start = CoroutineStart.UNDISPATCHED) {
             val count = withContext(Dispatchers.IO) { folder?.mediaCount(AbstractFolder.TYPE_FOLDER_VIDEO) ?: 0 }
             holder.binding.folderDesc.visibility = if (count == 0) View.GONE else View.VISIBLE
             if (count > 0) holder.binding.folderDesc.text = holder.itemView.context.resources.getQuantityString(R.plurals.videos_quantity, count, count)
+            holder.job = null
         }
     }
 
@@ -55,10 +53,17 @@ class FoldersAdapter(val actor: SendChannel<FolderAction>) : PagedListAdapter<Ab
         }
     }
 
+    override fun onViewRecycled(holder: ViewHolder) {
+        holder.job?. cancel()
+        holder.job = null
+        super.onViewRecycled(holder)
+    }
+
     override fun getItem(position: Int): AbstractFolder? = super.getItem(position)
 
     @TargetApi(Build.VERSION_CODES.M)
     inner class ViewHolder(binding: FolderItemBinding) : SelectorViewHolder<FolderItemBinding>(binding) {
+        var job : Job? = null
         init {
             binding.holder = this
             itemView.setOnLongClickListener {



More information about the Android mailing list