[Android] Add the favorite actions in the audio ctx

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


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Oct 21 09:07:54 2022 +0200| [30bab02e650c424119dfce5d9249e4009283c1dc] | committer: Nicolas Pomepuy

Add the favorite actions in the audio ctx

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

 .../videolan/vlc/gui/HeaderMediaListActivity.kt    |  9 ++++---
 .../videolan/vlc/gui/audio/AudioBrowserAdapter.kt  |  2 +-
 .../org/videolan/vlc/gui/audio/BaseAudioBrowser.kt | 31 +++++++++++++++++++---
 .../vlc/viewmodels/mobile/PlaylistViewModel.kt     |  7 ++++-
 4 files changed, 40 insertions(+), 9 deletions(-)

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 f619f56dea..166da16990 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/HeaderMediaListActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/HeaderMediaListActivity.kt
@@ -47,9 +47,7 @@ import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 import org.videolan.medialibrary.interfaces.Medialibrary
-import org.videolan.medialibrary.interfaces.media.Album
-import org.videolan.medialibrary.interfaces.media.MediaWrapper
-import org.videolan.medialibrary.interfaces.media.Playlist
+import org.videolan.medialibrary.interfaces.media.*
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.resources.*
 import org.videolan.resources.util.parcelable
@@ -308,6 +306,8 @@ open class HeaderMediaListActivity : AudioPlayerContainerActivity(), IEventsHand
             (item as? MediaWrapper)?.let { media ->
                 if (media.type == MediaWrapper.TYPE_STREAM || (media.type == MediaWrapper.TYPE_ALL && isSchemeHttpOrHttps(media.uri.scheme))) flags = flags or CTX_RENAME or CTX_COPY
                 else  flags = flags or CTX_SHARE
+                flags = flags or if(item.isFavorite) CTX_FAV_REMOVE else CTX_FAV_ADD
+
                 showContext(this, this, position, media, flags)
             }
         }
@@ -441,6 +441,9 @@ open class HeaderMediaListActivity : AudioPlayerContainerActivity(), IEventsHand
                 copy(media.title, media.location)
                 Snackbar.make(window.decorView.findViewById(android.R.id.content), R.string.url_copied_to_clipboard, Snackbar.LENGTH_LONG).show()
             }
+            CTX_FAV_ADD, CTX_FAV_REMOVE -> lifecycleScope.launch {
+                media.isFavorite = option == CTX_FAV_ADD
+            }
         }
 
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.kt
index 3d381eeb77..ab2c8ec4ce 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.kt
@@ -374,7 +374,7 @@ open class AudioBrowserAdapter @JvmOverloads constructor(
                     oldMedia: MediaLibraryItem, newMedia: MediaLibraryItem): Boolean {
                 return if (preventNextAnim) {
                     true
-                } else if (oldMedia is MediaWrapper && newMedia is MediaWrapper && oldMedia.isPresent != newMedia.isPresent) {
+                } else if (oldMedia is MediaWrapper && newMedia is MediaWrapper && (oldMedia.isPresent != newMedia.isPresent || oldMedia.isFavorite != newMedia.isFavorite)) {
                     false
                 } else oldMedia === newMedia || oldMedia.title == newMedia.title && oldMedia.itemType == newMedia.itemType && oldMedia.tracksCount == newMedia.tracksCount && oldMedia.equals(newMedia)
             }
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 083e2888ab..e8607bd2b9 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
@@ -44,6 +44,9 @@ import com.google.android.material.tabs.TabLayout
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
+import org.videolan.medialibrary.interfaces.media.Album
+import org.videolan.medialibrary.interfaces.media.Artist
+import org.videolan.medialibrary.interfaces.media.Genre
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.resources.*
@@ -368,11 +371,23 @@ abstract class BaseAudioBrowser<T : MedialibraryViewModel> : MediaBrowserFragmen
 
     override fun onCtxClick(v: View, position: Int, item: MediaLibraryItem) {
         val flags: Long = when (item.itemType) {
-            MediaLibraryItem.TYPE_MEDIA -> CTX_TRACK_FLAGS
-            MediaLibraryItem.TYPE_ARTIST, MediaLibraryItem.TYPE_GENRE -> {
-                if (item.tracksCount > 2) CTX_AUDIO_FLAGS or CTX_PLAY_SHUFFLE else CTX_AUDIO_FLAGS
+            MediaLibraryItem.TYPE_MEDIA -> {
+                if ((item as? MediaWrapper)?.isFavorite == true) CTX_TRACK_FLAGS or CTX_FAV_REMOVE else CTX_TRACK_FLAGS or CTX_FAV_ADD
             }
-            MediaLibraryItem.TYPE_PLAYLIST, MediaLibraryItem.TYPE_ALBUM -> {
+            MediaLibraryItem.TYPE_ARTIST -> {
+                val flags = if (item.tracksCount > 2) CTX_AUDIO_FLAGS or CTX_PLAY_SHUFFLE else CTX_AUDIO_FLAGS
+                if ((item as? Artist)?.isFavorite == true) flags or CTX_FAV_REMOVE else flags or CTX_FAV_ADD
+
+            }
+            MediaLibraryItem.TYPE_ALBUM -> {
+                val flags = if (item.tracksCount > 2) CTX_PLAYLIST_ALBUM_FLAGS or CTX_PLAY_SHUFFLE else CTX_PLAYLIST_ALBUM_FLAGS
+                if ((item as? Album)?.isFavorite == true) flags or CTX_FAV_REMOVE else flags or CTX_FAV_ADD
+            }
+            MediaLibraryItem.TYPE_GENRE -> {
+                val flags = if (item.tracksCount > 2) CTX_AUDIO_FLAGS or CTX_PLAY_SHUFFLE else CTX_AUDIO_FLAGS
+                if ((item as? Genre)?.isFavorite == true) flags or CTX_FAV_REMOVE else flags or CTX_FAV_ADD
+            }
+            MediaLibraryItem.TYPE_PLAYLIST -> {
                 if (item.tracksCount > 2) CTX_PLAYLIST_ALBUM_FLAGS or CTX_PLAY_SHUFFLE else CTX_PLAYLIST_ALBUM_FLAGS
             }
             else -> CTX_AUDIO_FLAGS
@@ -408,6 +423,14 @@ abstract class BaseAudioBrowser<T : MedialibraryViewModel> : MediaBrowserFragmen
             CTX_SHARE -> lifecycleScope.launch { (requireActivity() as AppCompatActivity).share(media as MediaWrapper) }
             CTX_GO_TO_FOLDER -> showParentFolder(media as MediaWrapper)
             CTX_ADD_SHORTCUT -> lifecycleScope.launch {requireActivity().createShortcut(media)}
+            CTX_FAV_ADD, CTX_FAV_REMOVE -> lifecycleScope.launch {
+                when (media) {
+                    is Album -> media.isFavorite = option == CTX_FAV_ADD
+                    is Artist -> media.isFavorite = option == CTX_FAV_ADD
+                    is Genre -> media.isFavorite = option == CTX_FAV_ADD
+                    is MediaWrapper -> media.isFavorite = option == CTX_FAV_ADD
+                }
+            }
         }
     }
 
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 258c564a4e..a84398798c 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
@@ -23,6 +23,7 @@ package org.videolan.vlc.viewmodels.mobile
 import android.content.Context
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModelProvider
+import androidx.lifecycle.viewModelScope
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
 import org.videolan.medialibrary.interfaces.media.Album
@@ -42,9 +43,13 @@ class PlaylistViewModel(context: Context, val playlist: MediaLibraryItem) : Medi
     init {
         when (playlist) {
             is Playlist -> watchPlaylists()
-            is Album -> watchAlbums()
+            is Album -> {
+                watchAlbums()
+                watchMedia()
+            }
             else -> watchMedia()
         }
+        viewModelScope.registerCallBacks { refresh() }
     }
 
     class Factory(val context: Context, val playlist: MediaLibraryItem): ViewModelProvider.NewInstanceFactory() {



More information about the Android mailing list