[Android] Toggle album favorite state in HeaderMediaListActivity

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 07:45:09 2022 +0100| [8ae11490ab41dfec73ea34ca8e314b6fdefb3810] | committer: Nicolas Pomepuy

Toggle album favorite state in HeaderMediaListActivity

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

 .../main/res/drawable/ic_header_media_favorite.xml | 34 ++++++++++++++++++++++
 .../drawable/ic_header_media_favorite_outline.xml  | 34 ++++++++++++++++++++++
 .../res/layout/header_media_list_activity.xml      | 14 +++++++++
 .../videolan/vlc/gui/HeaderMediaListActivity.kt    | 17 +++++++++++
 .../vlc/viewmodels/mobile/PlaylistViewModel.kt     |  7 +++++
 5 files changed, 106 insertions(+)

diff --git a/application/resources/src/main/res/drawable/ic_header_media_favorite.xml b/application/resources/src/main/res/drawable/ic_header_media_favorite.xml
new file mode 100644
index 0000000000..c635964b4a
--- /dev/null
+++ b/application/resources/src/main/res/drawable/ic_header_media_favorite.xml
@@ -0,0 +1,34 @@
+<!--
+  ~ *************************************************************************
+  ~  ic_menu_favorite.xml
+  ~ **************************************************************************
+  ~ Copyright © 2022 VLC authors and VideoLAN
+  ~ Author: Nicolas POMEPUY
+  ~ This program is free software; you can redistribute it and/or modify
+  ~ it under the terms of the GNU General Public License as published by
+  ~ the Free Software Foundation; either version 2 of the License, or
+  ~ (at your option) any later version.
+  ~
+  ~ This program is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  ~ GNU General Public License for more details.
+  ~
+  ~ You should have received a copy of the GNU General Public License
+  ~ along with this program; if not, write to the Free Software
+  ~ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+  ~ ***************************************************************************
+  ~
+  ~
+  -->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="32dp"
+        android:height="32dp"
+        android:viewportWidth="48"
+        android:viewportHeight="48">
+    <path
+            android:fillColor="?attr/colorControlNormal"
+            android:pathData="m26.218,37.652c-1.249,1.134 -3.172,1.134 -4.421,-0.016L21.617,37.471C12.989,29.665 7.352,24.554 7.566,18.178c0.099,-2.794 1.528,-5.472 3.846,-7.05 4.338,-2.958 9.696,-1.578 12.588,1.808 2.892,-3.385 8.25,-4.782 12.588,-1.808 2.317,1.578 3.747,4.256 3.845,7.05 0.23,6.376 -5.423,11.487 -14.051,19.326z"
+            android:strokeWidth="2" />
+</vector>
diff --git a/application/resources/src/main/res/drawable/ic_header_media_favorite_outline.xml b/application/resources/src/main/res/drawable/ic_header_media_favorite_outline.xml
new file mode 100644
index 0000000000..f0f545be6a
--- /dev/null
+++ b/application/resources/src/main/res/drawable/ic_header_media_favorite_outline.xml
@@ -0,0 +1,34 @@
+<!--
+  ~ *************************************************************************
+  ~  ic_menu_favorite_outline.xml
+  ~ **************************************************************************
+  ~ Copyright © 2022 VLC authors and VideoLAN
+  ~ Author: Nicolas POMEPUY
+  ~ This program is free software; you can redistribute it and/or modify
+  ~ it under the terms of the GNU General Public License as published by
+  ~ the Free Software Foundation; either version 2 of the License, or
+  ~ (at your option) any later version.
+  ~
+  ~ This program is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  ~ GNU General Public License for more details.
+  ~
+  ~ You should have received a copy of the GNU General Public License
+  ~ along with this program; if not, write to the Free Software
+  ~ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+  ~ ***************************************************************************
+  ~
+  ~
+  -->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="32dp"
+        android:height="32dp"
+        android:viewportWidth="48"
+        android:viewportHeight="48">
+    <path
+            android:fillColor="?attr/colorControlNormal"
+            android:pathData="M31.461,9.5C28.578,9.481 25.808,10.82 24,12.936 21.108,9.55 15.75,8.171 11.412,11.129 9.095,12.707 7.665,15.384 7.566,18.178 7.353,24.554 12.99,29.665 21.617,37.471l0.18,0.164c1.249,1.15 3.173,1.151 4.422,0.018l0.164,-0.148c8.628,-7.839 14.281,-12.95 14.051,-19.326 -0.099,-2.794 -1.529,-5.471 -3.846,-7.049 -1.627,-1.115 -3.397,-1.617 -5.127,-1.629zM17.041,13.5c2.382,0 4.702,1.523 5.522,3.631h2.891C26.257,15.023 28.577,13.5 30.959,13.5c3.093,0 5.414,2.308 5.414,5.385 0,4.446 -4.857,8.83 -12.219,15.461L24,34.5 23.846,34.346C16.484,27.715 11.627,23.331 11.627,18.885 11.627,15.808 13.948,13.5 17.041,13.5Z"
+            android:strokeWidth="2" />
+</vector>
diff --git a/application/vlc-android/res/layout/header_media_list_activity.xml b/application/vlc-android/res/layout/header_media_list_activity.xml
index d0f1d07b77..f450c15e04 100644
--- a/application/vlc-android/res/layout/header_media_list_activity.xml
+++ b/application/vlc-android/res/layout/header_media_list_activity.xml
@@ -201,6 +201,20 @@
                             vlc:layout_constraintStart_toEndOf="@id/btn_add_playlist"
                             vlc:layout_constraintTop_toTopOf="@id/play_btn" />
 
+                    <ImageView
+                            android:id="@+id/btn_favorite"
+                            android:layout_width="wrap_content"
+                            android:layout_height="wrap_content"
+                            android:layout_marginStart="8dp"
+                            android:clickable="true"
+                            android:foreground="?attr/selectableItemBackgroundBorderless"
+                            android:contentDescription="@string/favorites_add"
+                            android:padding="4dp"
+                            vlc:srcCompat="@drawable/ic_menu_favorite"
+                            vlc:layout_constraintBottom_toBottomOf="@id/play_btn"
+                            vlc:layout_constraintStart_toEndOf="@id/btn_shuffle"
+                            vlc:layout_constraintTop_toTopOf="@id/play_btn" />
+
                 </androidx.constraintlayout.widget.ConstraintLayout>
 
                 <androidx.appcompat.widget.Toolbar
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 166da16990..4fba33ae6b 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/HeaderMediaListActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/HeaderMediaListActivity.kt
@@ -36,6 +36,7 @@ import android.widget.Toast
 import androidx.appcompat.view.ActionMode
 import androidx.appcompat.widget.SearchView
 import androidx.coordinatorlayout.widget.CoordinatorLayout
+import androidx.core.content.ContextCompat
 import androidx.databinding.DataBindingUtil
 import androidx.lifecycle.lifecycleScope
 import androidx.paging.PagedList
@@ -152,6 +153,15 @@ open class HeaderMediaListActivity : AudioPlayerContainerActivity(), IEventsHand
             addToPlaylist(viewModel.playlist.tracks.toList())
         }
 
+        updateFavoriteState()
+
+        binding.btnFavorite.setOnClickListener {
+            lifecycleScope.launch {
+                viewModel.toggleFavorite()
+                updateFavoriteState()
+            }
+        }
+
         binding.headerListArtist.setOnClickListener {
             lifecycleScope.launch {
                 withContext(Dispatchers.IO) {
@@ -190,6 +200,13 @@ open class HeaderMediaListActivity : AudioPlayerContainerActivity(), IEventsHand
         binding.playBtn.setOnClickListener(this)
     }
 
+    private fun updateFavoriteState() {
+        (viewModel.playlist as? Album)?.let {
+            binding.btnFavorite.setVisible()
+            binding.btnFavorite.setImageDrawable(ContextCompat.getDrawable(this, if (it.isFavorite) R.drawable.ic_header_media_favorite else R.drawable.ic_header_media_favorite_outline))
+        } ?: binding.btnFavorite.setGone()
+    }
+
     override fun onStop() {
         super.onStop()
         stopActionMode()
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 a84398798c..1d09431d7d 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
@@ -63,6 +63,13 @@ class PlaylistViewModel(context: Context, val playlist: MediaLibraryItem) : Medi
         withContext(Dispatchers.IO) { (media as? MediaWrapper)?.rename(name) }
         refresh()
     }
+
+    suspend fun toggleFavorite() = withContext(Dispatchers.IO){
+        when (playlist) {
+            is Album -> playlist.setFavorite(!playlist.isFavorite)
+            else ->{}
+        }
+    }
 }
 
 internal fun HeaderMediaListActivity.getViewModel(playlist: MediaLibraryItem) = ViewModelProvider(this, PlaylistViewModel.Factory(this, playlist)).get(PlaylistViewModel::class.java)
\ No newline at end of file



More information about the Android mailing list