[Android] Use the new Favorite API

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 11:42:03 2022 +0100| [9be077a7a709a7e7ec099d9f1d9b00c4a55a79bc] | committer: Nicolas Pomepuy

Use the new Favorite API

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

 .../org/videolan/vlc/gui/audio/AudioBrowserAdapter.kt    | 16 ++++++++--------
 .../src/org/videolan/vlc/gui/audio/BaseAudioBrowser.kt   |  7 +------
 .../videolan/vlc/viewmodels/mobile/PlaylistViewModel.kt  |  5 +----
 3 files changed, 10 insertions(+), 18 deletions(-)

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 c032410eef..7a050f77b4 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
@@ -150,12 +150,7 @@ open class AudioBrowserAdapter @JvmOverloads constructor(
             holder.binding.setVariable(BR.isSD, item.uri.isSD())
             holder.binding.setVariable(BR.isPresent, item.isPresent)
         } else holder.binding.setVariable(BR.isPresent, true)
-        when (item) {
-            is MediaWrapper -> holder.binding.setVariable(BR.isFavorite, item.isFavorite)
-            is Album -> holder.binding.setVariable(BR.isFavorite, item.isFavorite)
-            is Artist -> holder.binding.setVariable(BR.isFavorite, item.isFavorite)
-            is Genre -> holder.binding.setVariable(BR.isFavorite, item.isFavorite)
-        }
+        item?.let { holder.binding.setVariable(BR.isFavorite, it.isFavorite) }
         holder.binding.setVariable(BR.inSelection,multiSelectHelper.inActionMode)
         holder.binding.invalidateAll()
         holder.binding.executePendingBindings()
@@ -182,6 +177,7 @@ open class AudioBrowserAdapter @JvmOverloads constructor(
                     UPDATE_REORDER -> {
                        holder.binding.invalidateAll()
                     }
+                    UPDATE_FAVORITE_STATE -> getItem(position)?.let { holder.binding.setVariable(BR.isFavorite, it.isFavorite) }
                 }
             }
         }
@@ -371,6 +367,7 @@ open class AudioBrowserAdapter @JvmOverloads constructor(
 
         private const val TAG = "VLC/AudioBrowserAdapter"
         private const val UPDATE_PAYLOAD = 1
+        private const val UPDATE_FAVORITE_STATE = 2
         /**
          * Awful hack to workaround the [PagedListAdapter] not keeping track of notifyItemMoved operations
          */
@@ -381,7 +378,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 || oldMedia.isFavorite != newMedia.isFavorite)) {
+                } else if (oldMedia is MediaWrapper && newMedia is MediaWrapper && oldMedia.isPresent != newMedia.isPresent) {
                     false
                 } else oldMedia === newMedia || oldMedia.title == newMedia.title && oldMedia.itemType == newMedia.itemType && oldMedia.tracksCount == newMedia.tracksCount && oldMedia.equals(newMedia)
             }
@@ -391,8 +388,11 @@ open class AudioBrowserAdapter @JvmOverloads constructor(
                 return false
             }
 
-            override fun getChangePayload(oldItem: MediaLibraryItem, newItem: MediaLibraryItem): Any? {
+            override fun getChangePayload(oldItem: MediaLibraryItem, newItem: MediaLibraryItem): Any {
                 preventNextAnim = false
+                when {
+                    oldItem.isFavorite != newItem.isFavorite  -> return UPDATE_FAVORITE_STATE
+                }
                 return UPDATE_PAYLOAD
             }
         }
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 e8607bd2b9..088f57d7ac 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
@@ -424,12 +424,7 @@ abstract class BaseAudioBrowser<T : MedialibraryViewModel> : MediaBrowserFragmen
             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
-                }
+                withContext(Dispatchers.IO) { 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 37eda77218..3d657683c5 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
@@ -90,10 +90,7 @@ class PlaylistViewModel(context: Context, private val initialPlaylist: MediaLibr
     }
 
     suspend fun toggleFavorite() = withContext(Dispatchers.IO){
-        when (playlist) {
-            is Album -> (playlist as Album).setFavorite(!(playlist as Album).isFavorite)
-            else ->{}
-        }
+        playlist?.let { it.setFavorite(!it.isFavorite) }
     }
 }
 



More information about the Android mailing list