[Android] Storage: override refresh function to filter items

Geoffrey Métais git at videolan.org
Mon Jun 17 18:10:52 CEST 2019


vlc-android | branch: 3.1.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jun 14 12:12:26 2019 +0200| [0655ce5407aac6cd30e9ea2fa4c418234b9a6f58] | committer: Geoffrey Métais

Storage: override refresh function to filter items

Fix #897

(cherry picked from commit 082fb7eee10da5f728d0b83ca17ca1440c6bf404)

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

 .../src/org/videolan/vlc/providers/BrowserProvider.kt   |  6 +++---
 .../src/org/videolan/vlc/providers/StorageProvider.kt   | 17 ++++++++++++++++-
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index 1f31183d4..45e6627bf 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -57,7 +57,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
     protected var mediabrowser: MediaBrowser? = null
 
     private val foldersContentMap = SimpleArrayMap<MediaLibraryItem, MutableList<MediaLibraryItem>>()
-    private lateinit var browserChannel : Channel<Media>
+    protected lateinit var browserChannel : Channel<Media>
     protected var job : Job? = null
     private val showAll = Settings.getInstance(context).getBoolean("browser_show_all_files", true)
 
@@ -125,7 +125,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         if (!browserActor.isClosedForSend) browserActor.offer(ParseSubDirectories)
     }
 
-    open fun refreshImpl() {
+    open suspend fun refreshImpl() {
         browserChannel = Channel(Channel.UNLIMITED)
         requestBrowsing(url)
         job = launch {
@@ -228,7 +228,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         return flags
     }
 
-    private fun requestBrowsing(url: String?) = launch(Dispatchers.IO) {
+    protected fun requestBrowsing(url: String?) = launch(Dispatchers.IO) {
         mutex.withLock {
             initBrowser()
             mediabrowser?.let {
diff --git a/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt b/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
index 865aa9a50..e5957d378 100644
--- a/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
@@ -23,6 +23,10 @@ package org.videolan.vlc.providers
 import android.content.Context
 import android.net.Uri
 import android.text.TextUtils
+import kotlinx.coroutines.channels.Channel
+import kotlinx.coroutines.channels.filter
+import kotlinx.coroutines.channels.mapTo
+import org.videolan.libvlc.Media
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.medialibrary.media.Storage
@@ -66,4 +70,15 @@ class StorageProvider(context: Context, dataset: LiveDataset<MediaLibraryItem>,
         } else if (media.itemType != MediaLibraryItem.TYPE_STORAGE) return
         super.addMedia(media)
     }
-}
\ No newline at end of file
+
+    override suspend fun refreshImpl() {
+        browserChannel = Channel(Channel.UNLIMITED)
+        requestBrowsing(url)
+        val value: MutableList<MediaLibraryItem> = browserChannel.filter { it.isStorage() }.mapTo(mutableListOf()) { Storage(it.uri)}
+        dataset.postValue(value)
+        parseSubDirectories()
+        loading.postValue(false)
+    }
+}
+
+private fun Media.isStorage() = type == Media.Type.Directory



More information about the Android mailing list