[Android] Change folder browsing to be a batch

Geoffrey Métais git at videolan.org
Wed Jul 31 15:15:50 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jul 30 15:23:31 2019 +0200| [79ef5e1c3492d2b503db45cd3daac9a1753efeb0] | committer: Geoffrey Métais

Change folder browsing to be a batch

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

 .../org/videolan/vlc/providers/BrowserProvider.kt  | 45 ++++++++--------------
 .../videolan/vlc/providers/FileBrowserProvider.kt  |  4 ++
 .../org/videolan/vlc/providers/NetworkProvider.kt  | 14 +++++++
 .../org/videolan/vlc/providers/StorageProvider.kt  | 17 ++++----
 4 files changed, 44 insertions(+), 36 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index de448533f..d384030a2 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -21,7 +21,6 @@
 package org.videolan.vlc.providers
 
 import android.content.Context
-import android.net.Uri
 import android.os.Handler
 import android.os.HandlerThread
 import android.os.Process
@@ -79,7 +78,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
             when (action) {
                 is Browse -> browseImpl(action.url)
                 BrowseRoot -> browseRootImpl()
-                Refresh -> refreshImpl()
+                Refresh -> browseImpl(url)
                 ParseSubDirectories -> parseSubDirectoriesImpl()
                 ClearListener -> withContext(Dispatchers.IO) { mediabrowser?.changeEventListener(null) }
                 Release -> withContext(Dispatchers.IO) {
@@ -94,6 +93,8 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         if (mediabrowser == null) mediabrowser = MediaBrowser(VLCInstance[context], this, browserHandler)
     }
 
+    protected abstract suspend fun requestBrowsing(url: String?) : Unit?
+
     open fun fetch() {
         val list by lazy(LazyThreadSafetyMode.NONE) { prefetchLists[url] }
         when {
@@ -115,15 +116,22 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         browserActor.post(Browse(url))
     }
 
-    private suspend fun browseImpl(url: String? = null) {
+    protected open suspend fun browseImpl(url: String? = null) {
         browserChannel = Channel(Channel.UNLIMITED)
         requestBrowsing(url)
-        for (media in browserChannel) findMedia(media)?.let {
-            if (url === null) loading.postValue(false)
-            addMedia(it)
+        if (url == null) {
+            for (media in browserChannel) findMedia(media)?.let {
+                loading.postValue(false)
+                addMedia(it)
+            }
+            if (dataset.value.isNotEmpty()) parseSubDirectories()
+            else dataset.clear() // send observable event when folder is empty
+        } else {
+            val value: MutableList<MediaLibraryItem> = browserChannel.mapNotNullTo(mutableListOf()) { findMedia(it) }
+            computeHeaders(value)
+            dataset.value = value
+            parseSubDirectories()
         }
-        if (dataset.value.isNotEmpty()) parseSubDirectories()
-        else dataset.clear() // send observable event when folder is empty
         loading.postValue(false)
     }
 
@@ -157,16 +165,6 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         browserActor.post(ParseSubDirectories)
     }
 
-    open suspend fun refreshImpl() {
-        browserChannel = Channel(Channel.UNLIMITED)
-        requestBrowsing(url)
-        val value: MutableList<MediaLibraryItem> = browserChannel.mapNotNullTo(mutableListOf()) { findMedia(it) }
-        computeHeaders(value)
-        dataset.value = value
-        parseSubDirectories()
-        loading.postValue(false)
-    }
-
     private suspend fun parseSubDirectoriesImpl() {
         if (dataset.value.isEmpty()) return
         val currentMediaList = withContext(Dispatchers.Main) { dataset.value.toList().also { if (url != null) computeHeaders(dataset.value) } }
@@ -271,17 +269,6 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         return flags
     }
 
-    protected suspend fun requestBrowsing(url: String?) = withContext(Dispatchers.IO) {
-        initBrowser()
-        mediabrowser?.let {
-            if (url != null) it.browse(Uri.parse(url), getFlags())
-            else {
-                it.changeEventListener(this at BrowserProvider)
-                it.discoverNetworkShares()
-            }
-        }
-    }
-
     open fun stop() {
         if (this at BrowserProvider::browserChannel.isInitialized) browserChannel.close()
         browserActor.offer(Release)
diff --git a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
index ee3858e5b..ff44db6fd 100644
--- a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
@@ -141,6 +141,10 @@ open class FileBrowserProvider(
         headers.clear()
     }
 
+    override suspend fun requestBrowsing(url: String?) = withContext(Dispatchers.IO) {
+        initBrowser()
+        mediabrowser?.let { if (url != null) it.browse(Uri.parse(url), getFlags()) }
+    }
 
     override fun browse(url: String?) {
         when {
diff --git a/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt b/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt
index cfc007357..3a26db227 100644
--- a/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt
@@ -21,9 +21,12 @@
 package org.videolan.vlc.providers
 
 import android.content.Context
+import android.net.Uri
 import androidx.lifecycle.Observer
+import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
+import kotlinx.coroutines.withContext
 import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.medialibrary.media.DummyItem
 import org.videolan.medialibrary.media.MediaLibraryItem
@@ -53,6 +56,17 @@ class NetworkProvider(context: Context, dataset: LiveDataset<MediaLibraryItem>,
 
     override fun fetch() {}
 
+    override suspend fun requestBrowsing(url: String?) = withContext(Dispatchers.IO) {
+        initBrowser()
+        mediabrowser?.let {
+            if (url != null) it.browse(Uri.parse(url), getFlags())
+            else {
+                it.changeEventListener(this at NetworkProvider)
+                it.discoverNetworkShares()
+            }
+        }
+    }
+
     override fun refresh() {
         val list by lazy(LazyThreadSafetyMode.NONE) { getList(url!!) }
         when {
diff --git a/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt b/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
index 9814aaa5d..d93d9cacd 100644
--- a/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
@@ -71,13 +71,16 @@ class StorageProvider(context: Context, dataset: LiveDataset<MediaLibraryItem>,
         super.addMedia(media)
     }
 
-    override suspend fun refreshImpl() {
-        browserChannel = Channel(Channel.UNLIMITED)
-        requestBrowsing(url)
-        val value: MutableList<MediaLibraryItem> = browserChannel.filter { it.isStorage() }.mapTo(mutableListOf()) { Storage(it.uri)}
-        dataset.value = value
-        parseSubDirectories()
-        loading.postValue(false)
+    override suspend fun browseImpl(url: String?) {
+        if (url == null) super.browseImpl(url)
+        else {
+            browserChannel = Channel(Channel.UNLIMITED)
+            requestBrowsing(url)
+            val value: MutableList<MediaLibraryItem> = browserChannel.filter { it.isStorage() }.mapTo(mutableListOf()) { Storage(it.uri)}
+            dataset.value = value
+            parseSubDirectories()
+            loading.postValue(false)
+        }
     }
 }
 



More information about the Android mailing list