Commit 4d9e1835 authored by Geoffrey Métais's avatar Geoffrey Métais

Video ViewModel manages sort & desc values

Remove this code from View classes
parent 8d47c776
......@@ -150,7 +150,7 @@ class MainTvFragment : BrowseSupportFragment(), OnItemViewSelectedListener, OnIt
rowsAdapter.add(miscRow)
adapter = rowsAdapter
videoModel = VideosModel.get(requireContext(), this, null, 0, Medialibrary.SORT_INSERTIONDATE)
videoModel = VideosModel.get(requireContext(), this, null, Medialibrary.SORT_INSERTIONDATE, -1, desc = true)
videoModel.dataset.observe(this, Observer {
updateVideos(it)
(requireActivity() as MainTvActivity).hideLoading()
......
......@@ -27,7 +27,6 @@ import android.annotation.TargetApi
import android.arch.lifecycle.Observer
import android.os.Build
import android.os.Bundle
import org.videolan.medialibrary.Medialibrary
import org.videolan.vlc.util.KEY_GROUP
import org.videolan.vlc.viewmodels.VideosModel
......@@ -35,8 +34,7 @@ import org.videolan.vlc.viewmodels.VideosModel
class VideoBrowserFragment : CategoriesFragment<VideosModel>() {
override fun onCreate(savedInstanceState: Bundle?) {
val minGroupLengthValue = Integer.valueOf(preferences.getString("video_min_group_length", "6"))
viewModel = VideosModel.get(requireContext(), this, arguments?.getString(KEY_GROUP), minGroupLengthValue, Medialibrary.SORT_DEFAULT)
viewModel = VideosModel.get(requireContext(), this, arguments?.getString(KEY_GROUP))
viewModel.categories.observe(this, Observer { update(it) })
super.onCreate(savedInstanceState)
}
......
......@@ -2,7 +2,6 @@ package org.videolan.vlc.gui.tv.browser
import android.arch.lifecycle.Observer
import android.os.Bundle
import org.videolan.medialibrary.Medialibrary
import org.videolan.vlc.R
import org.videolan.vlc.util.KEY_GROUP
import org.videolan.vlc.viewmodels.VideosModel
......@@ -12,10 +11,9 @@ class VideosFragment : MediaLibBrowserFragment<VideosModel>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val minGroupLengthValue = Integer.valueOf(preferences.getString("video_min_group_length", "6"))
val group = arguments?.getString(KEY_GROUP)
title = group ?: getString(R.string.videos)
model = VideosModel.get(requireContext(), this, group, minGroupLengthValue, Medialibrary.SORT_DEFAULT)
model = VideosModel.get(requireContext(), this, group)
model.dataset.observe(this, Observer { update(it!!) })
}
......
......@@ -49,7 +49,6 @@ public class PreferencesVideo extends BasePreferenceFragment {
findPreference("force_list_portrait").setVisible(false);
findPreference("save_brightness").setVisible(false);
findPreference("video_min_group_length").setVisible(false);
findPreference("enable_double_tap_seek").setVisible(false);
findPreference("enable_volume_gesture").setVisible(AndroidDevices.hasTsp);
findPreference("enable_brightness_gesture").setVisible(AndroidDevices.hasTsp);
......
......@@ -84,8 +84,7 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
final SharedPreferences preferences = Settings.INSTANCE.getInstance(requireContext());
final boolean seenMarkVisible = preferences.getBoolean("media_seen", true);
mAdapter = new VideoListAdapter(this, seenMarkVisible);
final int minGroupLengthValue = Integer.valueOf(preferences.getString("video_min_group_length", "6"));
viewModel = ViewModelProviders.of(requireActivity(), new VideosModel.Factory(requireContext(), mGroup, minGroupLengthValue, Medialibrary.SORT_DEFAULT)).get(VideosModel.class);
viewModel = ViewModelProviders.of(requireActivity(), new VideosModel.Factory(requireContext(), mGroup, 0, Medialibrary.SORT_DEFAULT, null)).get(VideosModel.class);
viewModel.getDataset().observe(this, this);
}
if (savedInstanceState != null) setGroup(savedInstanceState.getString(Constants.KEY_GROUP));
......
......@@ -31,26 +31,24 @@ import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.interfaces.MediaAddedCb
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.vlc.R
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.media.MediaGroup
import org.videolan.vlc.util.Settings
import org.videolan.vlc.util.Util
import org.videolan.vlc.util.VLCIO
open class VideosModel(context: Context, private val group: String?, private val minGroupLen: Int, customSort : Int) : MedialibraryModel<MediaWrapper>(context), MediaAddedCb {
open class VideosModel(context: Context, private val group: String?, private val minGroupLen: Int, customSort : Int, customDesc: Boolean?) : MedialibraryModel<MediaWrapper>(context), MediaAddedCb {
override val sortKey = "${super.sortKey}_$group"
override fun canSortByFileNameName() = true
override fun canSortByDuration() = true
override fun canSortByLastModified() = true
private val res by lazy { VLCApplication.getAppResources() }
private val thumbObs = Observer<MediaWrapper> { media -> updateActor.offer(MediaUpdate(listOf(media!!))) }
init {
sort = if (customSort != Medialibrary.SORT_DEFAULT) customSort
else Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = Settings.getInstance(context).getBoolean(sortKey+"_desc", false)
desc = customDesc ?: Settings.getInstance(context).getBoolean(sortKey+"_desc", false)
Medialibrary.lastThumb.observeForever(thumbObs)
}
......@@ -75,7 +73,7 @@ open class VideosModel(context: Context, private val group: String?, private val
}
}
minGroupLen > 0 -> MediaGroup.group(list, minGroupLen).mapTo(displayList) {
if (it.size() > 1 && res !== null) { it.description = res.getQuantityString(R.plurals.videos_quantity, it.size(), it.size()) }
if (it.size() > 1) { it.description = context.resources.getQuantityString(R.plurals.videos_quantity, it.size(), it.size()) }
it.media
}
else -> displayList.addAll(list)
......@@ -97,16 +95,24 @@ open class VideosModel(context: Context, private val group: String?, private val
Medialibrary.lastThumb.removeObserver(thumbObs)
}
class Factory(private val context: Context, val group: String?, private val minGroupLen : Int, private val sort : Int): ViewModelProvider.NewInstanceFactory() {
class Factory(private val context: Context, val group: String?, private val minGroupLen : Int, private val sort : Int, private val desc : Boolean?): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return VideosModel(context.applicationContext, group, minGroupLen, sort) as T
val length = if (minGroupLen == 0) Integer.valueOf(Settings.getInstance(context).getString("video_min_group_length", "6")) else minGroupLen
return VideosModel(context.applicationContext, group, length, sort, desc) as T
}
}
companion object {
fun get(context: Context, fragment: Fragment, group: String?, minGroupLen : Int, sort : Int) : VideosModel {
return ViewModelProviders.of(fragment, Factory(context, group, minGroupLen, sort)).get(VideosModel::class.java)
fun get(
context: Context,
fragment: Fragment,
group: String?,
sort : Int = Medialibrary.SORT_DEFAULT,
minGroupLen : Int = 0,
desc : Boolean? = null
) : VideosModel {
return ViewModelProviders.of(fragment, Factory(context, group, minGroupLen, sort, desc)).get(VideosModel::class.java)
}
}
}
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