[Android] Allow changing the favorite state in the action mode

Nicolas Pomepuy git at videolan.org
Fri Nov 18 12:29:45 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Nov  3 13:22:30 2022 +0100| [c9db53b4c1979670a2cf589c5b6ee71fbaed3dfd] | committer: Nicolas Pomepuy

Allow changing the favorite state in the action mode

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

 .../vlc-android/res/menu/action_mode_audio_browser.xml        | 11 +++++++++++
 .../src/org/videolan/vlc/gui/HeaderMediaListActivity.kt       |  4 ++++
 .../src/org/videolan/vlc/gui/audio/BaseAudioBrowser.kt        |  4 ++++
 .../src/org/videolan/vlc/viewmodels/MedialibraryViewModel.kt  | 11 +++++++++++
 .../org/videolan/vlc/viewmodels/mobile/PlaylistViewModel.kt   |  3 ++-
 5 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/application/vlc-android/res/menu/action_mode_audio_browser.xml b/application/vlc-android/res/menu/action_mode_audio_browser.xml
index 750e02d004..889ea4ffac 100644
--- a/application/vlc-android/res/menu/action_mode_audio_browser.xml
+++ b/application/vlc-android/res/menu/action_mode_audio_browser.xml
@@ -28,6 +28,17 @@
         android:title="@string/play"
             android:icon="@drawable/ic_am_play"
         app:showAsAction="always"/>
+    <item
+        android:id="@+id/action_mode_favorite_add"
+        android:title="@string/favorites_add"
+            android:icon="@drawable/ic_menu_favorite"
+            app:showAsAction="ifRoom" />
+
+    <item
+        android:id="@+id/action_mode_favorite_remove"
+        android:title="@string/favorites_remove"
+            android:icon="@drawable/ic_menu_favorite_outline"
+            app:showAsAction="ifRoom" />
     <item
         android:id="@+id/action_mode_audio_append"
         android:title="@string/append"
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/HeaderMediaListActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/HeaderMediaListActivity.kt
index 8fd7970834..d849a8addf 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/HeaderMediaListActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/HeaderMediaListActivity.kt
@@ -392,6 +392,8 @@ open class HeaderMediaListActivity : AudioPlayerContainerActivity(), IEventsHand
         menu.findItem(R.id.action_mode_audio_append).isVisible = PlaylistManager.hasMedia()
         menu.findItem(R.id.action_mode_audio_delete).isVisible = true
         menu.findItem(R.id.action_mode_audio_share).isVisible = isSong
+        menu.findItem(R.id.action_mode_favorite_add).isVisible = audioBrowserAdapter.multiSelectHelper.getSelection().none { it.isFavorite }
+        menu.findItem(R.id.action_mode_favorite_remove).isVisible = audioBrowserAdapter.multiSelectHelper.getSelection().none { !it.isFavorite }
         return true
     }
 
@@ -419,6 +421,8 @@ open class HeaderMediaListActivity : AudioPlayerContainerActivity(), IEventsHand
             R.id.action_mode_audio_share -> lifecycleScope.launch { share(list.map { it as MediaWrapper }) }
             R.id.action_mode_audio_set_song -> setRingtone(list[0] as MediaWrapper)
             R.id.action_mode_audio_delete -> if (isPlaylist) removeFromPlaylist(tracks, indexes.toMutableList()) else removeItems(tracks)
+            R.id.action_mode_favorite_add -> lifecycleScope.launch { viewModel.changeFavorite(tracks, true) }
+            R.id.action_mode_favorite_remove -> lifecycleScope.launch { viewModel.changeFavorite(tracks, false) }
             else -> return false
         }
         stopActionMode()
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/BaseAudioBrowser.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/BaseAudioBrowser.kt
index 088f57d7ac..db8999bca2 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/BaseAudioBrowser.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/BaseAudioBrowser.kt
@@ -291,6 +291,8 @@ abstract class BaseAudioBrowser<T : MedialibraryViewModel> : MediaBrowserFragmen
         menu.findItem(R.id.action_mode_audio_delete).isVisible = isMedia
         menu.findItem(R.id.action_mode_audio_share).isVisible = isMedia
         menu.findItem(R.id.action_mode_audio_share).isVisible = isMedia
+        menu.findItem(R.id.action_mode_favorite_add).isVisible = getCurrentAdapter()?.multiSelectHelper?.getSelection()?.none { it.isFavorite } ?: false
+        menu.findItem(R.id.action_mode_favorite_remove).isVisible = getCurrentAdapter()?.multiSelectHelper?.getSelection()?.none { !it.isFavorite } ?: false
         menu.findItem(R.id.action_mode_go_to_folder).isVisible = if (count == 1) getCurrentAdapter()?.multiSelectHelper?.let { selectHelper ->
             (selectHelper.getSelection().first() as? MediaWrapper)?.let {
                 it.uri.retrieveParent() != null
@@ -323,6 +325,8 @@ abstract class BaseAudioBrowser<T : MedialibraryViewModel> : MediaBrowserFragmen
                 R.id.action_mode_audio_set_song -> activity?.setRingtone(list.first() as MediaWrapper)
                 R.id.action_mode_audio_delete -> removeItems(list)
                 R.id.action_mode_go_to_folder -> (list.first() as? MediaWrapper)?.let { showParentFolder(it) }
+                R.id.action_mode_favorite_add -> lifecycleScope.launch { viewModel.changeFavorite(list, true) }
+                R.id.action_mode_favorite_remove -> lifecycleScope.launch { viewModel.changeFavorite(list, false) }
             }
         }
         return true
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryViewModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryViewModel.kt
index 47f2c22d3b..6ce1e98e81 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryViewModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryViewModel.kt
@@ -3,10 +3,14 @@ package org.videolan.vlc.viewmodels
 import android.content.Context
 import android.view.Menu
 import androidx.lifecycle.viewModelScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
+import java.util.ArrayList
 
 
 abstract class MedialibraryViewModel(context: Context) : SortableModel(context),
@@ -52,6 +56,13 @@ abstract class MedialibraryViewModel(context: Context) : SortableModel(context),
     override fun canSortByAlbum () = providers.any { it.canSortByAlbum () }
     override fun canSortByPlayCount() = providers.any { it.canSortByPlayCount() }
     override fun canSortByMediaNumber() = providers.any { it.canSortByMediaNumber() }
+
+
+    suspend fun changeFavorite(tracks: List<MediaLibraryItem>, favorite: Boolean) = withContext(Dispatchers.IO) {
+        tracks.forEach {
+            it.isFavorite = favorite
+        }
+    }
 }
 
 fun MedialibraryViewModel.prepareOptionsMenu(menu: Menu) {
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/PlaylistViewModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/PlaylistViewModel.kt
index 3d657683c5..8a240c4b38 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/PlaylistViewModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/PlaylistViewModel.kt
@@ -36,6 +36,7 @@ import org.videolan.vlc.gui.HeaderMediaListActivity
 import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
 import org.videolan.vlc.providers.medialibrary.TracksProvider
 import org.videolan.vlc.viewmodels.MedialibraryViewModel
+import java.util.ArrayList
 
 class PlaylistViewModel(context: Context, private val initialPlaylist: MediaLibraryItem) : MedialibraryViewModel(context) {
 
@@ -89,7 +90,7 @@ class PlaylistViewModel(context: Context, private val initialPlaylist: MediaLibr
         refresh()
     }
 
-    suspend fun toggleFavorite() = withContext(Dispatchers.IO){
+    suspend fun toggleFavorite() = withContext(Dispatchers.IO) {
         playlist?.let { it.setFavorite(!it.isFavorite) }
     }
 }



More information about the Android mailing list