From e584d7bed586ce2e455c9b42e7b3a05424684b34 Mon Sep 17 00:00:00 2001 From: faisalcodes Date: Wed, 1 Feb 2023 22:32:28 +0530 Subject: [PATCH 1/4] Follow-up to #2929. Simplified layouts. --- .../ui/adapters/BottomSheetAdapter.kt | 19 +- .../libretube/ui/adapters/CommentsAdapter.kt | 3 +- .../ui/fragments/CommentsMainFragment.kt | 2 +- .../libretube/ui/views/DrawableTextView.kt | 113 +++++++++++ app/src/main/res/drawable/ic_dislike.xml | 9 + app/src/main/res/layout/bottom_sheet_item.xml | 28 +-- app/src/main/res/layout/comments_row.xml | 182 +++++++++--------- app/src/main/res/layout/fragment_player.xml | 79 +++----- app/src/main/res/layout/searchhistory_row.xml | 39 ++-- .../main/res/layout/searchsuggestion_row.xml | 37 ++-- app/src/main/res/values/attrs.xml | 9 + 11 files changed, 292 insertions(+), 228 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt create mode 100644 app/src/main/res/drawable/ic_dislike.xml diff --git a/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt index f17ca2535..878b06584 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt @@ -1,8 +1,8 @@ package com.github.libretube.ui.adapters import android.view.LayoutInflater -import android.view.View import android.view.ViewGroup +import androidx.appcompat.content.res.AppCompatResources import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.BottomSheetItemBinding import com.github.libretube.obj.BottomSheetItem @@ -23,17 +23,16 @@ class BottomSheetAdapter( override fun onBindViewHolder(holder: BottomSheetViewHolder, position: Int) { val item = items[position] - holder.binding.apply { + holder.binding.sheetItem.apply { val current = item.getCurrent() - title.text = - if (current != null) "${item.title} ($current)" else item.title - if (item.drawable != null) { - drawable.setImageResource(item.drawable) - } else { - drawable.visibility = View.GONE - } + text = if (current != null) "${item.title} ($current)" else item.title + setCompoundDrawablesRelative( + if (item.drawable != null) { + AppCompatResources.getDrawable(context, item.drawable) + } else null, null, null, null + ) - root.setOnClickListener { + setOnClickListener { item.onClick.invoke() listener.invoke(position) } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt index 575bb82a0..38b6df834 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt @@ -65,7 +65,7 @@ class CommentsAdapter( if (comment.verified) verifiedImageView.visibility = View.VISIBLE if (comment.pinned) pinnedImageView.visibility = View.VISIBLE if (comment.hearted) heartedImageView.visibility = View.VISIBLE - if (comment.repliesPage != null) repliesAvailable.visibility = View.VISIBLE + if (comment.repliesPage != null) repliesCount.visibility = View.VISIBLE if (comment.replyCount > 0L) { repliesCount.text = comment.replyCount.formatShort() } @@ -77,7 +77,6 @@ class CommentsAdapter( if (isRepliesAdapter) { repliesCount.visibility = View.GONE - repliesAvailable.visibility = View.GONE // highlight the comment that is being replied to if (comment == comments.firstOrNull()) { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt index bf8c5020d..2e9adacfe 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt @@ -58,7 +58,7 @@ class CommentsMainFragment : Fragment() { viewModel.commentsPage.observe(viewLifecycleOwner) { it ?: return@observe binding.progress.visibility = View.GONE - if (it.disabled == true) { + if (it.disabled) { binding.errorTV.visibility = View.VISIBLE return@observe } diff --git a/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt b/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt new file mode 100644 index 000000000..84c704a5c --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt @@ -0,0 +1,113 @@ +package com.github.libretube.ui.views + +import android.content.Context +import android.content.res.TypedArray +import android.graphics.drawable.Drawable +import android.util.AttributeSet +import android.view.Gravity +import androidx.appcompat.widget.AppCompatTextView +import androidx.core.widget.TextViewCompat +import com.github.libretube.R + +/** + * TextView with custom sizable drawable support. + */ +class DrawableTextView( + context: Context, + attrs: AttributeSet? = null, +) : AppCompatTextView(context, attrs) { + + private var drawableStartDimen = 0F + private var drawableTopDimen = 0F + private var drawableEndDimen = 0F + private var drawableBottomDimen = 0F + + init { + val ta = getContext().obtainStyledAttributes(attrs, R.styleable.DrawableTextView, 0, 0) + try { + drawableStartDimen = getDimen(ta, R.styleable.DrawableTextView_drawableStartDimen) + drawableTopDimen = getDimen(ta, R.styleable.DrawableTextView_drawableTopDimen) + drawableEndDimen = getDimen(ta, R.styleable.DrawableTextView_drawableEndDimen) + drawableBottomDimen = getDimen(ta, R.styleable.DrawableTextView_drawableBottomDimen) + + gravity = ta.getInt( + R.styleable.DrawableTextView_android_gravity, Gravity.CENTER_VERTICAL + ) + compoundDrawablePadding = ta.getDimensionPixelOffset( + R.styleable.DrawableTextView_android_drawablePadding, 20 + ) + } finally { + ta.recycle() + } + } + + private fun getDimen(ta: TypedArray, index: Int): Float { + return ta.getDimensionPixelOffset(index, 0).toFloat() + } + + override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { + super.onLayout(changed, left, top, right, bottom) + val relDrawables = adjust(compoundDrawablesRelative) + setCompoundDrawablesRelative( + relDrawables[0], + relDrawables[1], + relDrawables[2], + relDrawables[3] + ) + } + + private fun adjust(drawables: Array): Array { + val startDimen = drawableStartDimen.toInt() + val start = drawables[0] + if (startDimen > 0 && start != null) { + start.setBounds(0, 0, startDimen, startDimen) + drawables[0] = start + } + val tDimen = drawableTopDimen.toInt() + val top = drawables[1] + if (tDimen > 0 && top != null) { + top.setBounds(0, 0, tDimen, tDimen) + drawables[1] = top + } + val endDimen = drawableEndDimen.toInt() + val end = drawables[2] + if (endDimen > 0 && end != null) { + end.setBounds(0, 0, endDimen, endDimen) + drawables[2] = end + } + val bDimen = drawableBottomDimen.toInt() + val bottom = drawables[3] + if (bDimen > 0 && bottom != null) { + bottom.setBounds(0, 0, bDimen, bDimen) + drawables[3] = bottom + } + return drawables + } + + fun setDrawables( + start: Drawable? = null, + top: Drawable? = null, + end: Drawable? = null, + bottom: Drawable? = null + ) { + TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds( + this, + start, + top, + end, + bottom + ) + } + + fun setDrawableStartDimension( + start: Float = drawableStartDimen, + top: Float = drawableTopDimen, + end: Float = drawableEndDimen, + bottom: Float = drawableBottomDimen + ) { + drawableStartDimen = start + drawableTopDimen = top + drawableEndDimen = end + drawableBottomDimen = bottom + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_dislike.xml b/app/src/main/res/drawable/ic_dislike.xml new file mode 100644 index 000000000..a9d9224ac --- /dev/null +++ b/app/src/main/res/drawable/ic_dislike.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/bottom_sheet_item.xml b/app/src/main/res/layout/bottom_sheet_item.xml index 1a8584f8d..bc6f66de8 100644 --- a/app/src/main/res/layout/bottom_sheet_item.xml +++ b/app/src/main/res/layout/bottom_sheet_item.xml @@ -1,28 +1,14 @@ - - - - - - - \ No newline at end of file + android:drawablePadding="25dp" + android:textSize="16sp" + tools:drawableStart="@drawable/ic_download" + tools:text="Option" /> \ No newline at end of file diff --git a/app/src/main/res/layout/comments_row.xml b/app/src/main/res/layout/comments_row.xml index fb5427e30..6f27c1706 100644 --- a/app/src/main/res/layout/comments_row.xml +++ b/app/src/main/res/layout/comments_row.xml @@ -11,128 +11,118 @@ android:layout_height="wrap_content" android:layout_marginTop="10dp" android:animateLayoutChanges="true" - android:orientation="vertical" + android:layout_marginBottom="16dp" + android:orientation="horizontal" android:paddingStart="15dp" android:paddingEnd="15dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"> + + - - + android:orientation="vertical"> - - - - - - - - - + android:layout_height="match_parent" + android:gravity="center_vertical" + android:orientation="horizontal"> + android:textSize="14sp" + android:textColor="?android:attr/textColorSecondary" + tools:text="Octacat • 10h" /> - + - + + - + - + - + - + - + + diff --git a/app/src/main/res/layout/fragment_player.xml b/app/src/main/res/layout/fragment_player.xml index 15c0083ad..01fb7a38f 100644 --- a/app/src/main/res/layout/fragment_player.xml +++ b/app/src/main/res/layout/fragment_player.xml @@ -29,6 +29,8 @@ android:id="@+id/player_title_layout" android:layout_width="match_parent" android:layout_height="wrap_content" + android:background="?selectableItemBackground" + android:layout_gravity="center_vertical" android:orientation="vertical"> - + android:layout_marginHorizontal="5dp" + android:drawablePadding="5dp" + app:drawableStartCompat="@drawable/ic_like" + app:drawableStartDimen="12dp" + tools:text="4.2K" /> - - - - - - - - - + @@ -339,29 +326,19 @@ app:strokeWidth="1dp" tools:ignore="RtlSymmetry"> - - - - - - - + android:visibility="gone" + android:text="@string/skip_segment" + android:textColor="@android:color/white" + android:textSize="18sp" + android:drawablePadding="20dp" + app:drawableEndCompat="@drawable/ic_next" + app:drawableEndDimen="20dp" + app:drawableTint="@android:color/white" /> diff --git a/app/src/main/res/layout/searchhistory_row.xml b/app/src/main/res/layout/searchhistory_row.xml index f6fbee759..2e4de6833 100644 --- a/app/src/main/res/layout/searchhistory_row.xml +++ b/app/src/main/res/layout/searchhistory_row.xml @@ -1,43 +1,34 @@ - + android:gravity="center_vertical" + android:paddingVertical="8dp" + android:layout_height="wrap_content"> - - - + android:drawablePadding="15dp" + android:layout_marginEnd="10dp" + app:drawableStartCompat="@drawable/ic_history" + tools:text="Suggestion item" /> - \ No newline at end of file + diff --git a/app/src/main/res/layout/searchsuggestion_row.xml b/app/src/main/res/layout/searchsuggestion_row.xml index 912e15718..2ec8814db 100644 --- a/app/src/main/res/layout/searchsuggestion_row.xml +++ b/app/src/main/res/layout/searchsuggestion_row.xml @@ -1,38 +1,29 @@ - + android:gravity="center_vertical" + android:paddingVertical="8dp" + android:layout_height="wrap_content"> - - - + android:drawablePadding="15dp" + android:layout_marginEnd="10dp" + app:drawableStartCompat="@drawable/ic_search" + tools:text="Suggestion item" /> - \ No newline at end of file + diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 58159c406..a5ed73221 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -8,4 +8,13 @@ + + + + + + + + + From 89ac493496527a39721eaaefa37b11983d1fe46a Mon Sep 17 00:00:00 2001 From: faisalcodes Date: Wed, 1 Feb 2023 22:41:04 +0530 Subject: [PATCH 2/4] Fix ktlint --- .../github/libretube/ui/adapters/BottomSheetAdapter.kt | 7 ++++++- .../com/github/libretube/ui/views/DrawableTextView.kt | 10 ++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt index 878b06584..917f4c241 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt @@ -29,7 +29,12 @@ class BottomSheetAdapter( setCompoundDrawablesRelative( if (item.drawable != null) { AppCompatResources.getDrawable(context, item.drawable) - } else null, null, null, null + } else { + null + }, + null, + null, + null ) setOnClickListener { diff --git a/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt b/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt index 84c704a5c..4c4f5e414 100644 --- a/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt @@ -14,7 +14,7 @@ import com.github.libretube.R */ class DrawableTextView( context: Context, - attrs: AttributeSet? = null, + attrs: AttributeSet? = null ) : AppCompatTextView(context, attrs) { private var drawableStartDimen = 0F @@ -31,10 +31,12 @@ class DrawableTextView( drawableBottomDimen = getDimen(ta, R.styleable.DrawableTextView_drawableBottomDimen) gravity = ta.getInt( - R.styleable.DrawableTextView_android_gravity, Gravity.CENTER_VERTICAL + R.styleable.DrawableTextView_android_gravity, + Gravity.CENTER_VERTICAL ) compoundDrawablePadding = ta.getDimensionPixelOffset( - R.styleable.DrawableTextView_android_drawablePadding, 20 + R.styleable.DrawableTextView_android_drawablePadding, + 20 ) } finally { ta.recycle() @@ -110,4 +112,4 @@ class DrawableTextView( drawableEndDimen = end drawableBottomDimen = bottom } -} \ No newline at end of file +} From 1a7ad4fd6897ce60e536e564c6e390c6a1dad4af Mon Sep 17 00:00:00 2001 From: faisalcodes Date: Thu, 2 Feb 2023 20:05:44 +0530 Subject: [PATCH 3/4] Minor clean --- .../ui/adapters/BottomSheetAdapter.kt | 15 ++-------- .../libretube/ui/extensions/SetDrawables.kt | 29 +++++++++++++++++++ .../libretube/ui/views/DrawableTextView.kt | 5 +++- app/src/main/res/layout/bottom_sheet_item.xml | 1 - 4 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/extensions/SetDrawables.kt diff --git a/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt index 917f4c241..b58462ce4 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/BottomSheetAdapter.kt @@ -2,10 +2,10 @@ package com.github.libretube.ui.adapters import android.view.LayoutInflater import android.view.ViewGroup -import androidx.appcompat.content.res.AppCompatResources import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.BottomSheetItemBinding import com.github.libretube.obj.BottomSheetItem +import com.github.libretube.ui.extensions.setDrawables import com.github.libretube.ui.viewholders.BottomSheetViewHolder class BottomSheetAdapter( @@ -23,19 +23,10 @@ class BottomSheetAdapter( override fun onBindViewHolder(holder: BottomSheetViewHolder, position: Int) { val item = items[position] - holder.binding.sheetItem.apply { + holder.binding.root.apply { val current = item.getCurrent() text = if (current != null) "${item.title} ($current)" else item.title - setCompoundDrawablesRelative( - if (item.drawable != null) { - AppCompatResources.getDrawable(context, item.drawable) - } else { - null - }, - null, - null, - null - ) + setDrawables(start = item.drawable) setOnClickListener { item.onClick.invoke() diff --git a/app/src/main/java/com/github/libretube/ui/extensions/SetDrawables.kt b/app/src/main/java/com/github/libretube/ui/extensions/SetDrawables.kt new file mode 100644 index 000000000..d0cc80194 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/extensions/SetDrawables.kt @@ -0,0 +1,29 @@ +package com.github.libretube.ui.extensions + +import android.graphics.drawable.Drawable +import android.widget.TextView +import androidx.appcompat.content.res.AppCompatResources +import androidx.core.widget.TextViewCompat + +fun TextView.setDrawables( + start: Int? = null, + top: Int? = null, + end: Int? = null, + bottom: Int? = null +) { + setDrawables( + start?.let { AppCompatResources.getDrawable(context, it) }, + top?.let { AppCompatResources.getDrawable(context, it) }, + end?.let { AppCompatResources.getDrawable(context, it) }, + bottom?.let { AppCompatResources.getDrawable(context, it) } + ) +} + +fun TextView.setDrawables( + start: Drawable? = null, + top: Drawable? = null, + end: Drawable? = null, + bottom: Drawable? = null +) { + TextViewCompat.setCompoundDrawablesRelative(this, start, top, end, bottom) +} diff --git a/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt b/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt index 4c4f5e414..3f817edf8 100644 --- a/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt @@ -11,6 +11,7 @@ import com.github.libretube.R /** * TextView with custom sizable drawable support. + * It may only be used for icons as it gives same width and height to the drawable. */ class DrawableTextView( context: Context, @@ -101,7 +102,7 @@ class DrawableTextView( ) } - fun setDrawableStartDimension( + fun setDrawablesDimension( start: Float = drawableStartDimen, top: Float = drawableTopDimen, end: Float = drawableEndDimen, @@ -111,5 +112,7 @@ class DrawableTextView( drawableTopDimen = top drawableEndDimen = end drawableBottomDimen = bottom + + invalidate() } } diff --git a/app/src/main/res/layout/bottom_sheet_item.xml b/app/src/main/res/layout/bottom_sheet_item.xml index bc6f66de8..76231da51 100644 --- a/app/src/main/res/layout/bottom_sheet_item.xml +++ b/app/src/main/res/layout/bottom_sheet_item.xml @@ -1,7 +1,6 @@ Date: Thu, 2 Feb 2023 15:45:13 +0100 Subject: [PATCH 4/4] Minor cleanup to the `DrawableTextView` --- .../libretube/ui/views/DrawableTextView.kt | 33 ++++++------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt b/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt index 4c4f5e414..9bcefdada 100644 --- a/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/DrawableTextView.kt @@ -59,33 +59,20 @@ class DrawableTextView( } private fun adjust(drawables: Array): Array { - val startDimen = drawableStartDimen.toInt() - val start = drawables[0] - if (startDimen > 0 && start != null) { - start.setBounds(0, 0, startDimen, startDimen) - drawables[0] = start - } - val tDimen = drawableTopDimen.toInt() - val top = drawables[1] - if (tDimen > 0 && top != null) { - top.setBounds(0, 0, tDimen, tDimen) - drawables[1] = top - } - val endDimen = drawableEndDimen.toInt() - val end = drawables[2] - if (endDimen > 0 && end != null) { - end.setBounds(0, 0, endDimen, endDimen) - drawables[2] = end - } - val bDimen = drawableBottomDimen.toInt() - val bottom = drawables[3] - if (bDimen > 0 && bottom != null) { - bottom.setBounds(0, 0, bDimen, bDimen) - drawables[3] = bottom + listOf(drawableStartDimen, drawableTopDimen, drawableEndDimen, drawableBottomDimen).forEachIndexed { index, dimen -> + drawables[index] = drawables[index].getAdjustedDrawable(dimen) } return drawables } + private fun Drawable?.getAdjustedDrawable(dimen: Float): Drawable? { + this ?: return null + dimen.toInt().let { + if (it > 0) setBounds(0, 0, it, it) + } + return this + } + fun setDrawables( start: Drawable? = null, top: Drawable? = null,