Turning MenuSheetItemView into an attributable view

So we can just set this in xml (to be used later)
This commit is contained in:
Jays2Kings 2021-04-17 01:58:05 -04:00
parent ce9372cb75
commit da2cca16a6
4 changed files with 139 additions and 56 deletions

View File

@ -2,21 +2,16 @@ package eu.kanade.tachiyomi.ui.base
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.app.Activity
import android.content.res.ColorStateList
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.widget.TextViewCompat
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.textview.MaterialTextView
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.BottomMenuSheetBinding
import eu.kanade.tachiyomi.util.system.dpToPx
@ -24,14 +19,14 @@ import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.system.hasSideNavBar
import eu.kanade.tachiyomi.util.system.isInNightMode
import eu.kanade.tachiyomi.util.view.expand
import eu.kanade.tachiyomi.util.view.invisible
import eu.kanade.tachiyomi.util.view.isVisible
import eu.kanade.tachiyomi.util.view.setBottomEdge
import eu.kanade.tachiyomi.util.view.setEdgeToEdge
import eu.kanade.tachiyomi.util.view.updateLayoutParams
import eu.kanade.tachiyomi.util.view.visible
import eu.kanade.tachiyomi.util.view.visibleIf
import eu.kanade.tachiyomi.widget.MenuSheetItemView
@SuppressLint("InflateParams")
open class MaterialMenuSheet(
activity: Activity,
items: List<MenuSheetItem>,
@ -72,42 +67,35 @@ open class MaterialMenuSheet(
var currentIndex: Int? = null
items.forEachIndexed { index, item ->
val view =
activity.layoutInflater.inflate(R.layout.menu_sheet_item, null) as ViewGroup
val textView = view.getChildAt(0) as MaterialTextView
activity.layoutInflater.inflate(R.layout.menu_sheet_item, null) as MenuSheetItemView
if (index == 0 && title == null) {
view.setBackgroundResource(R.drawable.rounded_item_background)
}
with(view) {
id = item.id
binding.menuLayout.addView(this)
setOnClickListener {
val shouldDismiss = onMenuItemClicked(this@MaterialMenuSheet, id)
if (shouldDismiss) {
dismiss()
}
}
}
with(textView) {
if (item.text != null) {
text = item.text
} else {
setText(item.textRes)
}
setCompoundDrawablesRelativeWithIntrinsicBounds(item.drawable, 0, 0, 0)
setIcon(item.drawable)
if (item.drawable == 0) {
textSize = 14f
}
if (item.id == selectedId) {
currentIndex = index
setTextColor(context.getResourceColor(R.attr.colorAccent))
TextViewCompat.setCompoundDrawableTintList(
this,
ColorStateList.valueOf(context.getResourceColor(R.attr.colorAccent))
)
setIconColor(activity.getResourceColor(R.attr.colorAccent))
setTextColor(activity.getResourceColor(R.attr.colorAccent))
}
updateLayoutParams<ViewGroup.MarginLayoutParams> {
height = 48.dpToPx
width = MATCH_PARENT
setOnClickListener {
val shouldDismiss = onMenuItemClicked(this@MaterialMenuSheet, id)
if (shouldDismiss) {
dismiss()
}
}
}
}
@ -122,7 +110,7 @@ open class MaterialMenuSheet(
currentIndex?.let {
binding.root.post {
binding.menuScrollView?.scrollTo(0, it * 48.dpToPx - binding.menuScrollView.height / 2)
binding.menuScrollView.scrollTo(0, it * 48.dpToPx - binding.menuScrollView.height / 2)
}
}
@ -154,14 +142,10 @@ open class MaterialMenuSheet(
private fun clearEndDrawables() {
(0 until binding.menuLayout.childCount).forEach {
val textView = (binding.menuLayout.getChildAt(it) as ViewGroup).getChildAt(0) as TextView
val imageView = (binding.menuLayout.getChildAt(it) as ViewGroup).getChildAt(1) as ImageView
textView.setTextColor(primaryColor)
TextViewCompat.setCompoundDrawableTintList(
textView,
ColorStateList.valueOf(primaryColor)
)
imageView.invisible()
val itemView = (binding.menuLayout.getChildAt(it) as MenuSheetItemView)
itemView.setTextColor(primaryColor)
itemView.setIconColor(primaryColor)
itemView.setEndIcon(0)
}
}
@ -169,18 +153,10 @@ open class MaterialMenuSheet(
if (clearAll) {
clearEndDrawables()
}
val layout = binding.menuLayout.findViewById<ViewGroup>(id) ?: return
val textView = layout.getChildAt(0) as? TextView
val imageView = layout.getChildAt(1) as? ImageView
textView?.setTextColor(textView.context.getResourceColor(R.attr.colorAccent))
textView?.let {
TextViewCompat.setCompoundDrawableTintList(
it,
ColorStateList.valueOf(it.context.getResourceColor(R.attr.colorAccent))
)
}
imageView?.visible()
imageView?.setImageResource(drawableRes)
val layout = binding.menuLayout.findViewById<MenuSheetItemView>(id) ?: return
layout.setTextColor(layout.context.getResourceColor(R.attr.colorAccent))
layout.setIconColor(layout.context.getResourceColor(R.attr.colorAccent))
layout.setEndIcon(drawableRes)
}
data class MenuSheetItem(

View File

@ -0,0 +1,97 @@
package eu.kanade.tachiyomi.widget
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.view.isInvisible
import androidx.core.widget.TextViewCompat
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.MenuSheetItemBinding
class MenuSheetItemView @JvmOverloads constructor(context: Context, attrs: AttributeSet?) :
LinearLayout(context, attrs) {
private val mText: String
private val mIconRes: Int
private val mEndIconRes: Int
private var binding: MenuSheetItemBinding? = null
init {
val a = context.obtainStyledAttributes(attrs, R.styleable.MenuSheetItemView, 0, 0)
val str = a.getString(R.styleable.MenuSheetItemView_android_text) ?: ""
mText = str
val d = a.getResourceId(R.styleable.MenuSheetItemView_icon, 0)
mIconRes = d
val e = a.getResourceId(R.styleable.MenuSheetItemView_endIcon, 0)
mEndIconRes = e
a.recycle()
}
override fun onFinishInflate() {
super.onFinishInflate()
binding = try {
MenuSheetItemBinding.bind(this)
} catch (e: Exception) {
MenuSheetItemBinding.inflate(LayoutInflater.from(context), this, true)
}
text = mText
setIcon(mIconRes)
setEndIcon(mEndIconRes)
}
var text: CharSequence?
get() = binding?.itemTextView?.text
set(value) {
binding?.itemTextView?.text = value
}
var textSize: Float
get() = binding?.itemTextView?.textSize ?: 0f
set(value) {
binding?.itemTextView?.textSize = value
}
fun setText(@StringRes res: Int) {
text = context.getString(res)
}
fun setTextColor(@ColorInt color: Int) {
binding?.itemTextView?.setTextColor(color)
}
fun setIconColor(@ColorInt color: Int) = binding?.itemTextView?.let {
TextViewCompat.setCompoundDrawableTintList(
it,
ColorStateList.valueOf(color)
)
}
fun setIcon(@DrawableRes res: Int) {
binding?.itemTextView?.setCompoundDrawablesRelativeWithIntrinsicBounds(
res,
0,
0,
0
)
}
fun setEndIcon(@DrawableRes res: Int) {
binding?.menuEndItem?.isInvisible = res == 0
binding?.menuEndItem?.setImageResource(res)
}
fun setEndIcon(drawable: Drawable?) {
binding?.menuEndItem?.isInvisible = drawable == null
binding?.menuEndItem?.setImageDrawable(drawable)
}
}

View File

@ -1,17 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<eu.kanade.tachiyomi.widget.MenuSheetItemView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_height="48sp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal">
android:orientation="horizontal"
tools:icon="@drawable/ic_share_24dp"
tools:endIcon="@drawable/ic_arrow_downward_24dp"
tools:text="@string/share">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/item_text_view"
android:layout_width="0dp"
android:layout_height="42dp"
android:layout_height="48sp"
android:layout_weight="1"
android:drawablePadding="12dp"
android:drawableTint="?android:textColorPrimary"
@ -20,17 +24,17 @@
android:paddingEnd="16dp"
android:textColor="?android:textColorPrimary"
android:textSize="13sp"
android:tint="@color/md_white_1000_54"
tools:drawableStart="@drawable/ic_share_24dp"
tools:text="@string/share" />
android:tint="@color/md_white_1000_54" />
<ImageView
android:id="@+id/menu_end_item"
android:layout_width="42dp"
android:layout_height="42dp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
app:tint="?colorAccent"
android:layout_gravity="center"
android:visibility="invisible"
tools:visibility="visible"
android:src="@drawable/ic_arrow_upward_24dp" />
</LinearLayout>
</eu.kanade.tachiyomi.widget.MenuSheetItemView>

View File

@ -26,5 +26,11 @@
<attr name="summary" format="reference|string" />
</declare-styleable>
<declare-styleable name="MenuSheetItemView">
<attr name="android:text"/>
<attr name="icon" format="reference"/>
<attr name="endIcon" format="reference"/>
</declare-styleable>
<bool name="isTablet">false</bool>
</resources>