[Android] Prevent sorting by filename for UPnP folders as file names are randomly generated

Nicolas Pomepuy git at videolan.org
Fri Sep 2 06:27:40 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Aug 29 11:27:41 2022 +0200| [f30e6704cef1fbad8b1a1315f47ee4bdca7a72d4] | committer: Nicolas Pomepuy

Prevent sorting by filename for UPnP folders as file names are randomly generated

Fixes #2651

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

 .../org/videolan/vlc/providers/BrowserProvider.kt  | 24 ++++++++++++++++------
 .../vlc/viewmodels/browser/BrowserModel.kt         |  6 ++----
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index 2825a240e..d3d2a1d60 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -21,6 +21,7 @@
 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
@@ -73,10 +74,11 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
 
     val descriptionUpdate = MutableLiveData<Pair<Int, String>>()
     internal val medialibrary = Medialibrary.getInstance()
-    val comparator : Comparator<MediaLibraryItem>
+    private val comparator : Comparator<MediaLibraryItem>?
         get() = when {
             Settings.showTvUi && sort == Medialibrary.SORT_ALPHA && desc -> tvDescComp
             Settings.showTvUi && sort == Medialibrary.SORT_ALPHA && !desc -> tvAscComp
+            url != null && Uri.parse(url)?.scheme == "upnp" -> null
             sort == Medialibrary.SORT_ALPHA && desc -> descComp
             sort == Medialibrary.SORT_ALPHA && !desc -> ascComp
             (sort == Medialibrary.SORT_FILENAME || sort == Medialibrary.SORT_DEFAULT) && desc -> filenameDescComp
@@ -161,7 +163,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
             discoveryJob = launch(coroutineContextProvider.Main) { filesFlow(url).collect { findMedia(it)?.let { item -> addMedia(item) } } }
         } else {
             val files = filesFlow(url).mapNotNull { findMedia(it) }.toList().toMutableList()
-            files.apply { this.sortWith(comparator) }
+            sort(files)
             dataset.value = files
             computeHeaders(files)
             parseSubDirectories(files)
@@ -169,6 +171,16 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         if (url != null ) loading.postValue(false)
     }
 
+    /**
+     * Sort the files using the comparator. If the comparator is null (UPnP) it keeps the
+     * files order (or reverse it in desc mode)
+     *
+     * @param files the files to sort
+     */
+    fun sort(files: MutableList<MediaLibraryItem>) {
+        comparator?.let { files.apply { this.sortWith(it) } } ?: if (desc) files.apply { reverse() }
+    }
+
     suspend fun browseUrl(url: String): List<MediaLibraryItem> {
         val deferred = CompletableDeferred<List<MediaLibraryItem>>()
         browserActor.post(BrowseUrl(url, deferred))
@@ -192,9 +204,9 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
     }
 
     protected open suspend fun refreshImpl() {
-        val files = filesFlow().mapNotNull { findMedia(it) }.toList()
-        files.apply { (this as MutableList).sortWith(comparator) }
-        dataset.value = files as MutableList<MediaLibraryItem>
+        val files = filesFlow().mapNotNull { findMedia(it) }.toList() as MutableList<MediaLibraryItem>
+        sort(files)
+        dataset.value = files
         computeHeaders(files)
         parseSubDirectories(files)
         loading.postValue(false)
@@ -303,7 +315,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
                             descriptionUpdate.value = Pair(position, it)
                         }
                         directories.addAll(files)
-                        comparator?.let { directories.sortWith(it) }
+                        sort(directories.toMutableList())
                         withContext(coroutineContextProvider.Main) { foldersContentMap.put(item, directories.toMutableList()) }
                     }
                     directories.clear()
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
index 5f47ada39..f41d56271 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
@@ -78,8 +78,7 @@ open class BrowserModel(
      */
     fun reSort() {
         viewModelScope.launch {
-            val comp = provider.comparator
-            dataset.value = withContext(coroutineContextProvider.Default) { dataset.value.apply { sortWith(comp) }.also { provider.computeHeaders(dataset.value) } }
+            dataset.value = withContext(coroutineContextProvider.Default) { dataset.value.apply { provider.sort(this) }.also { provider.computeHeaders(dataset.value) } }
         }
     }
 
@@ -101,8 +100,7 @@ open class BrowserModel(
             desc = if (sort == Medialibrary.SORT_DEFAULT) false else !desc
             provider.sort = sort
             provider.desc = desc
-            val comp = provider.comparator
-            dataset.value = withContext(coroutineContextProvider.Default) { dataset.value.apply { sortWith(comp) }.also { provider.computeHeaders(dataset.value) } }
+            dataset.value = withContext(coroutineContextProvider.Default) { dataset.value.apply { provider.sort(this) }.also { provider.computeHeaders(dataset.value) } }
             settings.putSingle(sortKey, sort)
             settings.putSingle("${sortKey}_desc", desc)
         }



More information about the Android mailing list