[Android] File browser: display name or filename for items depending on the used sort
Nicolas Pomepuy
git at videolan.org
Fri Jun 17 13:30:07 UTC 2022
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Jun 17 10:10:25 2022 +0200| [f920b29f308f2165cf93bb8fd037f52594e4b54b] | committer: Duncan McNamara
File browser: display name or filename for items depending on the used sort
Fixes #2558
> https://code.videolan.org/videolan/vlc-android/commit/f920b29f308f2165cf93bb8fd037f52594e4b54b
---
.../videolan/vlc/gui/browser/BaseBrowserAdapter.kt | 45 ++++++++++++++++++++--
.../vlc/gui/browser/BaseBrowserFragment.kt | 9 ++++-
.../vlc/gui/browser/MediaBrowserFragment.kt | 2 +-
.../vlc/viewmodels/browser/BrowserModel.kt | 2 +-
4 files changed, 52 insertions(+), 6 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.kt
index 2d11d55ef..0d4e0e739 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.kt
@@ -34,6 +34,7 @@ import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Job
import org.videolan.libvlc.util.AndroidUtil
+import org.videolan.medialibrary.interfaces.Medialibrary
import org.videolan.medialibrary.interfaces.media.MediaWrapper
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.medialibrary.media.MediaLibraryItem.TYPE_MEDIA
@@ -52,7 +53,7 @@ import org.videolan.vlc.gui.DiffUtilAdapter
import org.videolan.vlc.gui.helpers.*
import org.videolan.vlc.util.getDescriptionSpan
-open class BaseBrowserAdapter(val browserContainer: BrowserContainer<MediaLibraryItem>) : DiffUtilAdapter<MediaLibraryItem, BaseBrowserAdapter.ViewHolder<ViewDataBinding>>(), MultiSelectAdapter<MediaLibraryItem> {
+open class BaseBrowserAdapter(val browserContainer: BrowserContainer<MediaLibraryItem>, var sort:Int = Medialibrary.SORT_FILENAME, var asc:Boolean = true) : DiffUtilAdapter<MediaLibraryItem, BaseBrowserAdapter.ViewHolder<ViewDataBinding>>(), MultiSelectAdapter<MediaLibraryItem> {
protected val TAG = "VLC/BaseBrowserAdapter"
@@ -73,6 +74,16 @@ open class BaseBrowserAdapter(val browserContainer: BrowserContainer<MediaLibrar
private var specialIcons = false
private val handler by lazy(LazyThreadSafetyMode.NONE) { Handler() }
+ val diffCallback = BrowserDiffCallback()
+
+ fun changeSort(sort:Int, asc:Boolean) {
+ diffCallback.oldSort = diffCallback.newSort
+ diffCallback.oldAsc = diffCallback.newAsc
+ this.sort = sort
+ this.asc = asc
+ diffCallback.newAsc = asc
+ }
+
init {
val root = browserContainer.isRootDirectory
val fileBrowser = browserContainer.isFile
@@ -91,6 +102,10 @@ open class BaseBrowserAdapter(val browserContainer: BrowserContainer<MediaLibrar
qaMusicDrawable = BitmapDrawable(res, browserContainer.containerActivity().getBitmapFromDrawable(R.drawable.ic_browser_music_normal))
qaPodcastsDrawable = BitmapDrawable(res, browserContainer.containerActivity().getBitmapFromDrawable(R.drawable.ic_browser_podcasts_normal))
qaDownloadDrawable = BitmapDrawable(res, browserContainer.containerActivity().getBitmapFromDrawable(R.drawable.ic_browser_download_normal))
+ diffCallback.oldSort = sort
+ diffCallback.newSort = sort
+ diffCallback.oldAsc = asc
+ diffCallback.newAsc = asc
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder<ViewDataBinding> {
@@ -145,7 +160,7 @@ open class BaseBrowserAdapter(val browserContainer: BrowserContainer<MediaLibrar
&& "content" != scheme
&& "otg" != scheme)
&& !multiSelectHelper.inActionMode)
- vh.bindingContainer.setFileName(if (media.type != MediaWrapper.TYPE_DIR && "file" == scheme) media.fileName else null)
+ vh.bindingContainer.setFileName(if (sort == Medialibrary.SORT_FILENAME && media.type != MediaWrapper.TYPE_DIR && "file" == scheme) media.fileName else null)
if (networkRoot || (isFavorite && getProtocol(media)?.contains("file") == false)) vh.bindingContainer.setProtocol(getProtocol(media))
vh.bindingContainer.setCover(getIcon(media, specialIcons))
vh.selectView(multiSelectHelper.isSelected(position))
@@ -309,5 +324,29 @@ open class BaseBrowserAdapter(val browserContainer: BrowserContainer<MediaLibrar
override fun onUpdateFinished() {
browserContainer.onUpdateFinished(this)
+ diffCallback.oldSort = diffCallback.newSort
+ diffCallback.oldAsc = diffCallback.newAsc
+ }
+
+ override fun createCB() = diffCallback
+
+ class BrowserDiffCallback : DiffUtilAdapter.DiffCallback<MediaLibraryItem>() {
+ var oldSort = -1
+ var newSort = -1
+ var oldAsc = true
+ var newAsc = true
+
+ override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int):Boolean {
+ val result = if (newSort == oldSort && newAsc == oldAsc) true else try {
+ val oldItem = oldList[oldItemPosition] as MediaWrapper
+ val newItem = newList[newItemPosition] as MediaWrapper
+ (oldItem.fileName == newItem.title && newItem.fileName == oldItem.title)
+ } catch (ignored: Exception) {
+ true
+ }
+ return result
+ }
+
+ override fun areItemsTheSame(oldItemPosition : Int, newItemPosition : Int) = oldList[oldItemPosition] == newList[newItemPosition]
}
-}
\ No newline at end of file
+}
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
index 2b94a5522..99462acf7 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
@@ -146,7 +146,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
- if (!this::adapter.isInitialized) adapter = BaseBrowserAdapter(this).apply { stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY }
+ if (!this::adapter.isInitialized) adapter = BaseBrowserAdapter(this, viewModel.sort, !viewModel.desc).apply { stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY }
layoutManager = LinearLayoutManager(activity)
binding.networkList.layoutManager = layoutManager
binding.networkList.adapter = adapter
@@ -162,6 +162,13 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
}
}
+ override fun sortBy(sort: Int) {
+ super.sortBy(sort)
+ adapter.sort = sort
+ adapter.changeSort(sort, !viewModel.desc)
+ UiTools.updateSortTitles(this)
+ }
+
open fun registerSwiperRefreshlayout() = swipeRefreshLayout.setOnRefreshListener(this)
override fun setBreadcrumb() {
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
index f60e7dc4c..04087f418 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
@@ -206,7 +206,7 @@ abstract class MediaBrowserFragment<T : SortableModel> : BaseFragment(), Filtera
}
}
- protected open fun sortBy(sort: Int) {
+ open fun sortBy(sort: Int) {
viewModel.sort(sort)
}
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 33b0b666f..7758788ec 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
@@ -77,8 +77,8 @@ open class BrowserModel(
override fun sort(sort: Int) {
viewModelScope.launch {
this at BrowserModel.sort = sort
+ desc = !desc
provider.sort = sort
- val desc = !desc
provider.desc = desc
val comp = provider.comparator
dataset.value = withContext(coroutineContextProvider.Default) { dataset.value.apply { sortWith(comp) }.also { provider.computeHeaders(dataset.value) } }
More information about the Android
mailing list