[Android] Video ViewModel manages sort & desc values

Geoffrey Métais git at videolan.org
Mon Aug 13 11:27:45 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Aug 13 11:25:38 2018 +0200| [4d9e18354cb711fc39f78938ec7bbf8faf1f051c] | committer: Geoffrey Métais

Video ViewModel manages sort & desc values

Remove this code from View classes

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

 .../src/org/videolan/vlc/gui/tv/MainTvFragment.kt  |  2 +-
 .../vlc/gui/tv/browser/VideoBrowserFragment.kt     |  4 +---
 .../videolan/vlc/gui/tv/browser/VideosFragment.kt  |  4 +---
 .../vlc/gui/tv/preferences/PreferencesVideo.java   |  1 -
 .../videolan/vlc/gui/video/VideoGridFragment.java  |  3 +--
 .../src/org/videolan/vlc/viewmodels/VideosModel.kt | 24 ++++++++++++++--------
 6 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvFragment.kt
index 42f74361c..bfc0fb5a1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvFragment.kt
@@ -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()
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.kt
index 01bd44981..a8a1a3b9e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.kt
@@ -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)
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideosFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideosFragment.kt
index 8b72d5e95..c047ef035 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideosFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideosFragment.kt
@@ -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!!) })
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesVideo.java b/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesVideo.java
index 432c2ece1..ef73acfc6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesVideo.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesVideo.java
@@ -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);
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 39d718bd8..82a7e05e6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -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));
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/VideosModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/VideosModel.kt
index a69e498d8..2ee8f0ca9 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/VideosModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/VideosModel.kt
@@ -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)
         }
     }
 }



More information about the Android mailing list