Commit 43d4d0d4 authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Geoffrey Métais

Set providers handle sorting

parent d831fd55
......@@ -49,6 +49,7 @@ import org.videolan.vlc.interfaces.IEventsHandler
import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.media.PlaylistManager
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.MedialibraryViewModel
import org.videolan.vlc.viewmodels.SortableModel
import java.util.*
......@@ -69,10 +70,10 @@ abstract class BaseAudioBrowser<T : SortableModel> : MediaBrowserFragment<T>(),
open fun getCurrentAdapter() = adapter
protected var currentTab
get() = viewPager?.currentItem ?: 0
set(value) {
viewPager?.currentItem = value
}
get() = viewPager?.currentItem ?: 0
set(value) {
viewPager?.currentItem = value
}
private lateinit var layoutOnPageChangeListener: TabLayout.TabLayoutOnPageChangeListener
......@@ -247,8 +248,14 @@ abstract class BaseAudioBrowser<T : SortableModel> : MediaBrowserFragment<T>(),
MediaUtils.openList(activity, Arrays.asList(*item.tracks), 0)
}
override fun sortMenuTitles() {
val model = viewModel
if (model is MedialibraryViewModel) menu?.let { UiTools.updateSortTitles(it, model.providers[currentTab]) }
else super.sortMenuTitles()
}
override fun onUpdateFinished(adapter: RecyclerView.Adapter<*>) {
UiTools.updateSortTitles(this)
sortMenuTitles()
}
override fun onItemFocused(v: View, item: MediaLibraryItem) {}
......
......@@ -212,9 +212,11 @@ abstract class MediaBrowserFragment<T : SortableModel> : Fragment(), ActionMode.
menu.findItem(R.id.ml_menu_sortby_last_modified).isVisible = canSortByLastModified()
menu.findItem(R.id.ml_menu_sortby_number).isVisible = false
}
UiTools.updateSortTitles(this)
sortMenuTitles()
}
open fun sortMenuTitles() = UiTools.updateSortTitles(this)
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.ml_menu_sortby_name -> {
......
......@@ -45,10 +45,7 @@ import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicBlur
import android.text.Html
import android.text.TextUtils
import android.view.DragEvent
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import android.view.*
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.DecelerateInterpolator
......@@ -84,6 +81,7 @@ import androidx.databinding.BindingAdapter
import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
......@@ -313,6 +311,28 @@ object UiTools {
}
fun updateSortTitles(menu: Menu, provider: MedialibraryProvider<*>) {
val sort = provider.sort
val desc = provider.desc
var item: MenuItem? = menu.findItem(R.id.ml_menu_sortby_name)
item?.setTitle(if (sort == Medialibrary.SORT_ALPHA && !desc) R.string.sortby_name_desc else R.string.sortby_name)
item = menu.findItem(R.id.ml_menu_sortby_filename)
item?.setTitle(if (sort == Medialibrary.SORT_FILENAME && !desc) R.string.sortby_filename_desc else R.string.sortby_filename)
item = menu.findItem(R.id.ml_menu_sortby_artist_name)
item?.setTitle(if (sort == Medialibrary.SORT_ARTIST && !desc) R.string.sortby_artist_name_desc else R.string.sortby_artist_name)
item = menu.findItem(R.id.ml_menu_sortby_album_name)
item?.setTitle(if (sort == Medialibrary.SORT_ALBUM && !desc) R.string.sortby_album_name_desc else R.string.sortby_album_name)
item = menu.findItem(R.id.ml_menu_sortby_length)
item?.setTitle(if (sort == Medialibrary.SORT_DURATION && !desc) R.string.sortby_length_desc else R.string.sortby_length)
item = menu.findItem(R.id.ml_menu_sortby_date)
item?.setTitle(if (sort == Medialibrary.SORT_RELEASEDATE && !desc) R.string.sortby_date_desc else R.string.sortby_date)
item = menu.findItem(R.id.ml_menu_sortby_last_modified)
item?.setTitle(if (sort == Medialibrary.SORT_RELEASEDATE && !desc) R.string.sortby_last_modified_date_desc else R.string.sortby_last_modified_date)
// item = menu.findItem(R.id.ml_menu_sortby_number); TODO sort by track number
// if (item != null) item.setTitle(sort == Medialibrary.SORT_ && !desc ? R.string.sortby_number_desc : R.string.sortby_number);
}
fun updateSortTitles(sortable: MediaBrowserFragment<*>) {
val menu = sortable.menu ?: return
val model = sortable.viewModel
......
......@@ -22,19 +22,27 @@ package org.videolan.vlc.providers.medialibrary
import android.content.Context
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.Album
import org.videolan.medialibrary.media.Artist
import org.videolan.medialibrary.media.Genre
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.util.Settings
import org.videolan.vlc.viewmodels.SortableModel
@ExperimentalCoroutinesApi
class AlbumsProvider(val parent : MediaLibraryItem?, context: Context, scope: SortableModel) : MedialibraryProvider<Album>(context, scope) {
override val sortKey = "${super.sortKey}_${parent?.javaClass?.simpleName}"
override fun canSortByDuration() = true
override fun canSortByReleaseDate() = true
init {
sort = Settings.getInstance(context).getInt(sortKey, if (parent is Artist) Medialibrary.SORT_RELEASEDATE else Medialibrary.SORT_DEFAULT)
desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
}
override fun getAll() : Array<Album> = when (parent) {
is Artist -> parent.getAlbums(sort, scope.desc)
is Genre -> parent.getAlbums(sort, scope.desc)
......
......@@ -33,10 +33,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
import org.videolan.vlc.util.ModelsHelper
import org.videolan.vlc.util.canSortBy
import org.videolan.vlc.util.retry
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.SortableModel
import org.videolan.vlc.viewmodels.paged.HeadersIndex
......@@ -47,6 +44,10 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
private val headers = HeadersIndex()
val liveHeaders : LiveData<HeadersIndex> = MutableLiveData<HeadersIndex>()
protected open val sortKey : String = this.javaClass.simpleName
var sort = Medialibrary.SORT_DEFAULT
var desc = false
private val pagingConfig = Config(
pageSize = MEDIALIBRARY_PAGE_SIZE,
prefetchDistance = MEDIALIBRARY_PAGE_SIZE / 5,
......@@ -61,9 +62,6 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
abstract fun getPage(loadSize: Int, startposition: Int): Array<T>
abstract fun getAll(): Array<T>
val sort : Int
get() = if (canSortBy(scope.sort)) scope.sort else Medialibrary.SORT_DEFAULT
open fun canSortByName() = true
open fun canSortByFileNameName() = false
open fun canSortByDuration() = false
......@@ -75,6 +73,22 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
open fun canSortByAlbum ()= false
open fun canSortByPlayCount() = false
open fun sort(sort: Int) {
if (canSortBy(sort)) {
desc = when (this.sort) {
Medialibrary.SORT_DEFAULT -> sort == Medialibrary.SORT_ALPHA
sort -> !desc
else -> false
}
this.sort = sort
refresh()
Settings.getInstance(context).edit()
.putInt(sortKey, sort)
.putBoolean("${sortKey}_desc", desc)
.apply()
}
}
fun refresh(): Boolean {
headers.clear()
if (pagedList.value?.dataSource?.isInvalid == false) {
......
......@@ -22,17 +22,30 @@ package org.videolan.vlc.providers.medialibrary
import android.content.Context
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.*
import org.videolan.vlc.util.Settings
import org.videolan.vlc.viewmodels.SortableModel
@ExperimentalCoroutinesApi
class TracksProvider(val parent : MediaLibraryItem?, context: Context, scope: SortableModel) : MedialibraryProvider<MediaWrapper>(context, scope) {
override val sortKey = "${super.sortKey}_${parent?.javaClass?.simpleName}"
override fun canSortByDuration() = true
override fun canSortByAlbum() = parent !== null
override fun canSortByLastModified() = true
init {
sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_DEFAULT)
desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", parent is Artist)
if (sort == Medialibrary.SORT_DEFAULT) sort = when (parent) {
is Artist -> Medialibrary.SORT_ALBUM
is Album -> Medialibrary.SORT_DEFAULT
else -> Medialibrary.SORT_ALPHA
}
}
override fun getAll(): Array<MediaWrapper> = parent?.tracks ?: medialibrary.getAudio(sort, scope.desc)
override fun getPage(loadSize: Int, startposition: Int) : Array<MediaWrapper> {
......
......@@ -10,9 +10,10 @@ abstract class MedialibraryViewModel(context: Context) : SortableModel(context),
Medialibrary.OnMedialibraryReadyListener, Medialibrary.OnDeviceChangeListener {
val medialibrary = Medialibrary.getInstance().apply {
addOnMedialibraryReadyListener(this@MedialibraryViewModel)
addOnDeviceChangeListener(this@MedialibraryViewModel)
}
addOnMedialibraryReadyListener(this@MedialibraryViewModel)
addOnDeviceChangeListener(this@MedialibraryViewModel)
}
abstract val providers : Array<MedialibraryProvider<out MediaLibraryItem>>
override fun refresh() = providers.forEach { it.refresh() }
......@@ -26,6 +27,8 @@ abstract class MedialibraryViewModel(context: Context) : SortableModel(context),
refresh()
}
override fun sort(sort: Int) { providers.forEach { it.sort(sort) } }
fun isFiltering() = filterQuery != null
override fun onMedialibraryReady() = refresh()
......
......@@ -39,7 +39,6 @@ class PagedTracksModel(context: Context, val parent: MediaLibraryItem? = null):
Medialibrary.PlaylistsCb by EmptyMLCallbacks {
override val provider = TracksProvider(parent, context, this)
override val sortKey = "${super.sortKey}_${parent?.javaClass?.simpleName}"
init {
sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment