[Android] Wait for customDiresLocation

Habib Kazemi git at videolan.org
Thu Aug 30 10:10:06 CEST 2018


vlc-android | branch: master | Habib Kazemi <kazemihabib1996 at gmail.com> | Tue Aug 28 16:32:38 2018 +0430| [b2c6bd4d9befa2e31c532126ee602a5a4567a248] | committer: Geoffrey Métais

Wait for customDiresLocation

Signed-off-by: Geoffrey Métais <geoffrey.metais at gmail.com>

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

 .../vlc/gui/browser/StorageBrowserAdapter.kt       | 35 ++++++++++++----------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
index ef150c0d2..75c666e54 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
@@ -28,6 +28,7 @@ import android.net.Uri
 import android.preference.PreferenceManager
 import android.view.View
 import android.widget.CheckBox
+import kotlinx.coroutines.experimental.Job
 import kotlinx.coroutines.experimental.android.UI
 import kotlinx.coroutines.experimental.launch
 
@@ -45,28 +46,32 @@ internal class StorageBrowserAdapter(fragment: BaseBrowserFragment) : BaseBrowse
 
     private var mediaDirsLocation: MutableList<String> = mutableListOf()
     private lateinit var customDirsLocation: List<String>
+    private var job : Job? = null
 
     init {
         updateMediaDirs(fragment.requireContext())
     }
 
     override fun onBindViewHolder(holder: BaseBrowserAdapter.ViewHolder<*>, position: Int) {
-        val vh = holder as BaseBrowserAdapter.MediaViewHolder
-        var storage = getItem(position)
+        launch(UI.immediate) {
+            val vh = holder as BaseBrowserAdapter.MediaViewHolder
+            var storage = getItem(position)
 
-        if (storage?.itemType == MediaLibraryItem.TYPE_MEDIA) storage = Storage((storage as MediaWrapper).uri)
-        var storagePath = (storage as Storage).uri.path
-        if (!storagePath.endsWith("/")) storagePath += "/"
-        val hasContextMenu = customDirsLocation.contains(storagePath)
-        val checked = (fragment as StorageBrowserFragment).mScannedDirectory || mediaDirsLocation.contains(storagePath)
-        vh.binding.setItem(storage)
-        vh.binding.hasContextMenu = hasContextMenu
-        when {
-            checked -> vh.binding.browserCheckbox.state = ThreeStatesCheckbox.STATE_CHECKED
-            hasDiscoveredChildren(storagePath) -> vh.binding.browserCheckbox.state = ThreeStatesCheckbox.STATE_PARTIAL
-            else -> vh.binding.browserCheckbox.state = ThreeStatesCheckbox.STATE_UNCHECKED
+            if (storage?.itemType == MediaLibraryItem.TYPE_MEDIA) storage = Storage((storage as MediaWrapper).uri)
+            var storagePath = (storage as Storage).uri.path
+            if (!storagePath.endsWith("/")) storagePath += "/"
+            job?.join()
+            val hasContextMenu = customDirsLocation.contains(storagePath)
+            val checked = (fragment as StorageBrowserFragment).mScannedDirectory || mediaDirsLocation.contains(storagePath)
+            vh.binding.setItem(storage)
+            vh.binding.hasContextMenu = hasContextMenu
+            when {
+                checked -> vh.binding.browserCheckbox.state = ThreeStatesCheckbox.STATE_CHECKED
+                hasDiscoveredChildren(storagePath) -> vh.binding.browserCheckbox.state = ThreeStatesCheckbox.STATE_PARTIAL
+                else -> vh.binding.browserCheckbox.state = ThreeStatesCheckbox.STATE_UNCHECKED
+            }
+            vh.binding.checkEnabled = !fragment.mScannedDirectory
         }
-        vh.binding.checkEnabled = !fragment.mScannedDirectory
     }
 
     private fun hasDiscoveredChildren(path: String): Boolean {
@@ -82,7 +87,7 @@ internal class StorageBrowserAdapter(fragment: BaseBrowserFragment) : BaseBrowse
             mediaDirsLocation.add(Uri.decode(if (it.startsWith("file://")) it.substring(7) else it))
         }
 
-        launch(UI.immediate) {
+        job = launch(UI.immediate) {
             customDirsLocation = DirectoryRepository.getInstance(context).getCustomDirectories().map { it.path }
         }
     }



More information about the Android mailing list