[Android] Optimise custom tabs view

Dmitry git at videolan.org
Thu Mar 16 12:30:17 UTC 2023


vlc-android | branch: master | Dmitry <dgyudin at gmail.com> | Thu Mar 16 12:30:16 2023 +0000| [a35efc263a6254606c082c82fc12e0518e8b8bbf] | committer: Nicolas Pomepuy

Optimise custom tabs view

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

 application/vlc-android/res/layout/audio_tab.xml   | 42 ++++++++--------------
 application/vlc-android/res/values/styles.xml      |  1 -
 .../vlc/gui/audio/AudioAlbumsSongsFragment.kt      |  6 ++--
 .../videolan/vlc/gui/audio/AudioBrowserFragment.kt |  6 ++--
 .../src/org/videolan/vlc/gui/helpers/UiTools.kt    |  9 +++++
 .../videolan/vlc/gui/video/VideoBrowserFragment.kt | 10 +++---
 6 files changed, 33 insertions(+), 41 deletions(-)

diff --git a/application/vlc-android/res/layout/audio_tab.xml b/application/vlc-android/res/layout/audio_tab.xml
index cda710d609..9083cccd9d 100644
--- a/application/vlc-android/res/layout/audio_tab.xml
+++ b/application/vlc-android/res/layout/audio_tab.xml
@@ -22,34 +22,20 @@
   ~
   -->
 
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
+        android:id="@+id/tab_title"
+        android:textAllCaps="true"
         android:layout_width="wrap_content"
-        android:layout_height="wrap_content">
-
-    <TextView
-            android:id="@+id/tab_title"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_marginEnd="8dp"
-            android:fontFamily="sans-serif-medium"
-            android:textAllCaps="true"
-            android:letterSpacing="0.1"
-            android:textColor="@color/standard_selection_control_normal_primary"
-            app:layout_constraintBottom_toBottomOf="parent"
-            app:layout_constraintEnd_toStartOf="@+id/tab_icon"
-            app:layout_constraintStart_toStartOf="parent"
-            app:layout_constraintTop_toTopOf="parent"
-            tools:text="Artists" />
-
-    <ImageView
-            android:id="@+id/tab_icon"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            app:layout_constraintBottom_toBottomOf="parent"
-            app:layout_constraintTop_toTopOf="parent"
-            app:tint="@color/standard_selection_control_normal_primary"
-            app:layout_constraintEnd_toEndOf="parent"
-            app:srcCompat="@drawable/ic_emoji_favorite" />
-</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
+        android:layout_height="wrap_content"
+        android:fontFamily="sans-serif-medium"
+        android:letterSpacing="0.1"
+        android:textColor="@color/standard_selection_control_normal_primary"
+        android:drawablePadding="8dp"
+        app:drawableTint="@color/standard_selection_control_normal_primary"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        tools:text="Artists"
+        tools:drawableRight="@drawable/ic_emoji_favorite" />
\ No newline at end of file
diff --git a/application/vlc-android/res/values/styles.xml b/application/vlc-android/res/values/styles.xml
index 329320e093..104f421a12 100644
--- a/application/vlc-android/res/values/styles.xml
+++ b/application/vlc-android/res/values/styles.xml
@@ -585,7 +585,6 @@
     </style>
 
     <style name="TabLayout.VLC" parent="Widget.MaterialComponents.TabLayout">
-        <item name="tabBackground">?attr/background_default</item>
         <item name="android:background">?attr/background_default</item>
         <item name="tabRippleColor">?attr/colorPrimary</item>
         <item name="tabMode">scrollable</item>
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.kt
index 9385947066..c7f51a25f4 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.kt
@@ -23,7 +23,6 @@ package org.videolan.vlc.gui.audio
 import android.content.Intent
 import android.os.Bundle
 import android.view.*
-import android.widget.ImageView
 import android.widget.TextView
 import androidx.coordinatorlayout.widget.CoordinatorLayout
 import androidx.paging.PagedList
@@ -45,6 +44,8 @@ import org.videolan.vlc.gui.ContentActivity
 import org.videolan.vlc.gui.HeaderMediaListActivity
 import org.videolan.vlc.gui.dialogs.*
 import org.videolan.vlc.gui.helpers.UiTools
+import org.videolan.vlc.gui.helpers.UiTools.addFavoritesIcon
+import org.videolan.vlc.gui.helpers.UiTools.removeDrawables
 import org.videolan.vlc.gui.view.FastScroller
 import org.videolan.vlc.gui.view.RecyclerSectionItemGridDecoration
 import org.videolan.vlc.media.MediaUtils
@@ -244,9 +245,8 @@ class AudioAlbumsSongsFragment : BaseAudioBrowser<AlbumSongsViewModel>(), SwipeR
             val tab = tabLayout!!.getTabAt(i)
             val view = tab?.customView ?: View.inflate(requireActivity(), R.layout.audio_tab, null)
             val title = view.findViewById<TextView>(R.id.tab_title)
-            val icon = view.findViewById<ImageView>(R.id.tab_icon)
             title.text = audioPagerAdapter.getPageTitle(i)
-            if (viewModel.providers[i].onlyFavorites) icon.setVisible() else icon.setGone()
+            if (viewModel.providers[i].onlyFavorites) title.addFavoritesIcon() else title.removeDrawables()
             tab?.customView = view
         }
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
index f8dd531398..5ae8e6947a 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
@@ -26,7 +26,6 @@ import android.content.res.Configuration
 import android.os.Bundle
 import android.util.SparseArray
 import android.view.*
-import android.widget.ImageView
 import android.widget.TextView
 import androidx.coordinatorlayout.widget.CoordinatorLayout
 import androidx.lifecycle.lifecycleScope
@@ -54,6 +53,8 @@ import org.videolan.vlc.gui.HeaderMediaListActivity
 import org.videolan.vlc.gui.SecondaryActivity
 import org.videolan.vlc.gui.dialogs.*
 import org.videolan.vlc.gui.helpers.UiTools
+import org.videolan.vlc.gui.helpers.UiTools.addFavoritesIcon
+import org.videolan.vlc.gui.helpers.UiTools.removeDrawables
 import org.videolan.vlc.gui.view.EmptyLoadingState
 import org.videolan.vlc.gui.view.EmptyLoadingStateView
 import org.videolan.vlc.gui.view.FastScroller
@@ -349,9 +350,8 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>() {
             val tab = tabLayout!!.getTabAt(i)
             val view = tab?.customView ?: View.inflate(requireActivity(), R.layout.audio_tab, null)
             val title = view.findViewById<TextView>(R.id.tab_title)
-            val icon = view.findViewById<ImageView>(R.id.tab_icon)
             title.text = audioPagerAdapter.getPageTitle(i)
-            if (viewModel.providers[i].onlyFavorites) icon.setVisible() else icon.setGone()
+            if (viewModel.providers[i].onlyFavorites) title.addFavoritesIcon() else title.removeDrawables()
             tab?.customView = view
         }
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
index 9259f2d915..b9bc0f3df4 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
@@ -778,6 +778,15 @@ object UiTools {
         DEFAULT_COVER_VIDEO_DRAWABLE = null
     }
 
+    fun TextView.addFavoritesIcon() {
+        val drawable = ContextCompat.getDrawable(context, R.drawable.ic_emoji_favorite)
+        setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null)
+    }
+
+    fun TextView.removeDrawables() {
+        setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
+    }
+
 }
 
 /**
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoBrowserFragment.kt
index 613be1ea56..bf34cc0ae2 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoBrowserFragment.kt
@@ -26,7 +26,6 @@ package org.videolan.vlc.gui.video
 
 import android.os.Bundle
 import android.view.*
-import android.widget.ImageView
 import android.widget.TextView
 import androidx.appcompat.view.ActionMode
 import androidx.fragment.app.Fragment
@@ -37,12 +36,12 @@ import com.google.android.material.tabs.TabLayout
 import com.google.android.material.tabs.TabLayoutMediator
 import org.videolan.medialibrary.interfaces.media.Playlist
 import org.videolan.tools.isStarted
-import org.videolan.tools.setGone
-import org.videolan.tools.setVisible
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.BaseFragment
 import org.videolan.vlc.gui.ContentActivity
 import org.videolan.vlc.gui.PlaylistFragment
+import org.videolan.vlc.gui.helpers.UiTools.addFavoritesIcon
+import org.videolan.vlc.gui.helpers.UiTools.removeDrawables
 import org.videolan.vlc.interfaces.Filterable
 import org.videolan.vlc.util.findCurrentFragment
 
@@ -212,11 +211,10 @@ class VideoBrowserFragment : BaseFragment(), TabLayout.OnTabSelectedListener, Fi
             val tab = tabLayout!!.getTabAt(i)
             val view = tab?.customView ?: View.inflate(requireActivity(), R.layout.audio_tab, null)
             val title = view.findViewById<TextView>(R.id.tab_title)
-            val icon = view.findViewById<ImageView>(R.id.tab_icon)
             title.text = getPageTitle(i)
             when (i) {
-                0 -> if (videoGridOnlyFavorites) icon.setVisible() else icon.setGone()
-                1 -> if (playlistOnlyFavorites) icon.setVisible() else icon.setGone()
+                0 -> if (videoGridOnlyFavorites) title.addFavoritesIcon() else title.removeDrawables()
+                1 -> if (playlistOnlyFavorites) title.addFavoritesIcon() else title.removeDrawables()
             }
             tab?.customView = view
         }



More information about the Android mailing list