[Android] Videos: Group by name by default

Geoffrey Métais git at videolan.org
Wed May 13 06:47:44 CEST 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue May 12 10:00:42 2020 +0200| [68ef2085cc93297fcb8a249bfaf89aa19f33ffa1] | committer: Nicolas Pomepuy

Videos: Group by name by default

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

 .../main/java/org/videolan/resources/Constants.kt  |  6 +++
 .../src/main/java/org/videolan/tools/Settings.kt   |  2 +-
 .../src/org/videolan/vlc/gui/helpers/Navigator.kt  | 19 +-------
 .../videolan/vlc/gui/video/VideoGridFragment.kt    | 57 ++++++++--------------
 4 files changed, 30 insertions(+), 54 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/Constants.kt b/application/resources/src/main/java/org/videolan/resources/Constants.kt
index 1c6cc2fe9..e2b5bf591 100644
--- a/application/resources/src/main/java/org/videolan/resources/Constants.kt
+++ b/application/resources/src/main/java/org/videolan/resources/Constants.kt
@@ -136,6 +136,12 @@ const val KEY_FOLDER = "key_folder"
 const val KEY_GROUPING = "key_grouping"
 const val FAVORITE_TITLE = "favorite_title"
 
+const val KEY_VIDEOS_CARDS = "video_display_in_cards"
+const val KEY_GROUP_VIDEOS = "video_min_group_length"
+const val GROUP_VIDEOS_NONE = "-1"
+const val GROUP_VIDEOS_FOLDER = "0"
+const val GROUP_VIDEOS_NAME = "6"
+
 // Items updates
 const val UPDATE_SELECTION = 0
 const val UPDATE_THUMB = 1
diff --git a/application/tools/src/main/java/org/videolan/tools/Settings.kt b/application/tools/src/main/java/org/videolan/tools/Settings.kt
index 5ececa0ce..81b1a0e0c 100644
--- a/application/tools/src/main/java/org/videolan/tools/Settings.kt
+++ b/application/tools/src/main/java/org/videolan/tools/Settings.kt
@@ -13,7 +13,7 @@ import org.videolan.tools.Settings.init
 
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
-object Settings : SingletonHolder<SharedPreferences, Context>({ init(it) }) {
+object Settings : SingletonHolder<SharedPreferences, Context>({ init(it.applicationContext) }) {
 
     var showVideoThumbs = true
     var tvUI = false
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/Navigator.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/Navigator.kt
index 824e04839..1cd6a3e33 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/Navigator.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/Navigator.kt
@@ -61,7 +61,7 @@ class Navigator : BottomNavigationView.OnNavigationItemSelectedListener, Lifecyc
 
     private val defaultFragmentId= R.id.nav_video
     override var currentFragmentId : Int = 0
-    var currentFragment: Fragment? = null
+    private var currentFragment: Fragment? = null
         private set
     private lateinit var activity: MainActivity
     private lateinit var settings: SharedPreferences
@@ -112,22 +112,7 @@ class Navigator : BottomNavigationView.OnNavigationItemSelectedListener, Lifecyc
             R.id.nav_playlists -> PlaylistFragment()
             R.id.nav_network -> NetworkBrowserFragment()
             R.id.nav_more -> MoreFragment()
-            else -> {
-                val group = Integer.valueOf(Settings.getInstance(activity.applicationContext).getString("video_min_group_length", "1")!!)
-                when {
-                    group > 0 -> VideoGridFragment().apply {
-                        arguments = Bundle(1).apply {
-                            putSerializable(KEY_GROUPING, VideoGroupingType.NAME)
-                        }
-                    }
-                    group == 0 -> VideoGridFragment().apply {
-                        arguments = Bundle(1).apply {
-                            putSerializable(KEY_GROUPING, VideoGroupingType.FOLDER)
-                        }
-                    }
-                    else -> VideoGridFragment()
-                }
-            }
+            else -> VideoGridFragment()
         }
     }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
index 2d8ce82ad..a62f1a3c2 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
@@ -98,10 +98,14 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
             multiSelectHelper = videoListAdapter.multiSelectHelper
             val folder = if (savedInstanceState != null) savedInstanceState.getParcelable<Folder>(KEY_FOLDER)
             else arguments?.getParcelable(KEY_FOLDER)
-            val group = if (savedInstanceState != null) savedInstanceState.getParcelable<VideoGroup>(KEY_GROUP)
-            else arguments?.getParcelable(KEY_GROUP)
-            val grouping = arguments?.getSerializable(KEY_GROUPING) as VideoGroupingType? ?: VideoGroupingType.NONE
-            viewModel = getViewModel(grouping, folder, group)
+            val parentGroup = if (savedInstanceState != null) savedInstanceState.getParcelable<VideoGroup>(KEY_GROUP)
+                    else arguments?.getParcelable(KEY_GROUP)
+            val grouping = when (Settings.getInstance(requireContext()).getString(KEY_GROUP_VIDEOS, null) ?: GROUP_VIDEOS_NAME) {
+                GROUP_VIDEOS_NONE -> VideoGroupingType.NONE
+                GROUP_VIDEOS_FOLDER -> VideoGroupingType.FOLDER
+                else -> VideoGroupingType.NAME
+            }
+            viewModel = getViewModel(grouping, folder, parentGroup)
             setDataObservers()
             Medialibrary.lastThumb.observe(this, thumbObs)
             videoListAdapter.events.onEach { it.process() }.launchWhenStarted(lifecycleScope)
@@ -141,54 +145,36 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
     }
 
     override fun onOptionsItemSelected(item: MenuItem): Boolean {
-        return when (item.itemId) {
+        when (item.itemId) {
             R.id.ml_menu_last_playlist -> {
                 MediaUtils.loadlastPlaylist(activity, PLAYLIST_TYPE_VIDEO)
-                true
             }
             R.id.ml_menu_display_list, R.id.ml_menu_display_grid -> {
-                val displayInCards = settings.getBoolean("video_display_in_cards", true)
-                settings.putSingle("video_display_in_cards", !displayInCards)
+                val displayInCards = settings.getBoolean(KEY_VIDEOS_CARDS, true)
+                settings.putSingle(KEY_VIDEOS_CARDS, !displayInCards)
                 (activity as ContentActivity).forceLoadVideoFragment()
-                true
             }
             R.id.video_min_group_length_disable -> {
-                lifecycleScope.launchWhenStarted {
-                    withContext(Dispatchers.IO) {
-                        settings.edit().putString("video_min_group_length", "-1").commit()
-                    }
-                    changeGroupingType(VideoGroupingType.NONE)
-                }
-                true
+                settings.putSingle(KEY_GROUP_VIDEOS, GROUP_VIDEOS_NONE)
+                changeGroupingType(VideoGroupingType.NONE)
             }
             R.id.video_min_group_length_folder -> {
-                lifecycleScope.launchWhenStarted {
-                    withContext(Dispatchers.IO) {
-                        settings.edit().putString("video_min_group_length", "0").commit()
-                    }
-                    changeGroupingType(VideoGroupingType.FOLDER)
-                }
-                true
+                settings.putSingle(KEY_GROUP_VIDEOS, GROUP_VIDEOS_FOLDER)
+                changeGroupingType(VideoGroupingType.FOLDER)
             }
             R.id.video_min_group_length_name -> {
-                lifecycleScope.launchWhenStarted {
-                    withContext(Dispatchers.IO) {
-                        settings.edit().putString("video_min_group_length", "6").commit()
-                    }
-                    changeGroupingType(VideoGroupingType.NAME)
-                }
-                true
+                settings.putSingle(KEY_GROUP_VIDEOS, GROUP_VIDEOS_NAME)
+                changeGroupingType(VideoGroupingType.NAME)
             }
             R.id.rename_group -> {
                 viewModel.group?.let { renameGroup(it) }
-                true
             }
             R.id.ungroup -> {
                 viewModel.group?.let { viewModel.ungroup(it) }
-                true
             }
-            else -> super.onOptionsItemSelected(item)
+            else -> return super.onOptionsItemSelected(item)
         }
+        return true
     }
 
     override fun sortBy(sort: Int) {
@@ -270,9 +256,8 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
             return
         }
         val res = resources
-        if (gridItemDecoration == null)
-            gridItemDecoration = ItemOffsetDecoration(resources, R.dimen.left_right_1610_margin, R.dimen.top_bottom_1610_margin)
-        val listMode = !settings.getBoolean("video_display_in_cards", true)
+        if (gridItemDecoration == null) gridItemDecoration = ItemOffsetDecoration(resources, R.dimen.left_right_1610_margin, R.dimen.top_bottom_1610_margin)
+        val listMode = !settings.getBoolean(KEY_VIDEOS_CARDS, true)
 
         // Select between grid or list
         binding.videoGrid.removeItemDecoration(gridItemDecoration!!)



More information about the Android mailing list