[Android] File browsing: Fix performance

Geoffrey Métais git at videolan.org
Tue Feb 4 17:41:42 CET 2020


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb  4 16:52:59 2020 +0100| [908fccb3b1cae611ee93b85e5d81b9765ab31fcb] | committer: Geoffrey Métais

File browsing: Fix performance

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

 .../org/videolan/vlc/providers/BrowserProvider.kt  | 39 ++++++++++++----------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index 8f2584f48..14ab1e3f0 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -77,7 +77,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
             when (action) {
                 is Browse -> browseImpl(action.url)
                 BrowseRoot -> browseRootImpl()
-                Refresh -> browseImpl(url)
+                Refresh -> refreshImpl()
                 is ParseSubDirectories -> parseSubDirectoriesImpl(action.list)
                 ClearListener -> withContext(Dispatchers.IO) { mediabrowser?.changeEventListener(null) }
                 Release -> withContext(Dispatchers.IO) {
@@ -119,33 +119,38 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
     protected open suspend fun browseImpl(url: String? = null) {
         if (url == null) {
             coroutineScope {
-                discoveryJob = launch { filesFlow(url).collect { findMedia(it)?.let { item -> addMedia(item) } } }
+                discoveryJob = launch { filesFlow().collect { findMedia(it)?.let { item -> addMedia(item) } } }
             }
         } else {
-            val files = filesFlow(url).mapNotNull { findMedia(it) }.toList()
+            val files = filesFlow().mapNotNull { findMedia(it) }.onEach { addMedia(it) }.toList()
             computeHeaders(files)
-            dataset.value = files as MutableList<MediaLibraryItem>
             parseSubDirectories(files)
+            loading.postValue(false)
         }
-        if (url != null ) loading.postValue(false)
     }
 
-    private suspend fun filesFlow(url: String? = this.url, interact : Boolean = true) = withContext(Dispatchers.IO) {
-        channelFlow {
-            val listener = object : EventListener {
-                override fun onMediaAdded(index: Int, media: Media) {
-                    if (!isClosedForSend) offer(media.apply { retain() })
-                }
+    protected open suspend fun refreshImpl() {
+        val files = filesFlow(url, false).mapNotNull { findMedia(it) }.toList()
+        dataset.value = files as MutableList<MediaLibraryItem>
+        computeHeaders(files)
+        parseSubDirectories(files)
+        loading.postValue(false)
+    }
 
-                override fun onBrowseEnd() {
-                    if (!isClosedForSend) close()
-                }
+    private fun filesFlow(url: String? = this.url, interact : Boolean = true) = channelFlow {
+        val listener = object : EventListener {
+            override fun onMediaAdded(index: Int, media: Media) {
+                if (!isClosedForSend) offer(media.apply { retain() })
+            }
 
-                override fun onMediaRemoved(index: Int, media: Media) {}
+            override fun onBrowseEnd() {
+                if (!isClosedForSend) close()
             }
-            requestBrowsing(url, listener, interact)
-            awaitClose { if (url != null) browserActor.post(ClearListener) }
+
+            override fun onMediaRemoved(index: Int, media: Media) {}
         }
+        requestBrowsing(url, listener, interact)
+        awaitClose { if (url != null) browserActor.post(ClearListener) }
     }.buffer(Channel.UNLIMITED)
 
     protected open fun addMedia(media: MediaLibraryItem) = dataset.add(media)



More information about the Android mailing list