mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-11-10 01:35:09 +01:00
Turning MenuSheetItemView into an attributable view
So we can just set this in xml (to be used later)
This commit is contained in:
parent
ce9372cb75
commit
da2cca16a6
@ -2,21 +2,16 @@ package eu.kanade.tachiyomi.ui.base
|
|||||||
|
|
||||||
import android.animation.ObjectAnimator
|
import android.animation.ObjectAnimator
|
||||||
import android.animation.ValueAnimator
|
import android.animation.ValueAnimator
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.res.ColorStateList
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
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.DrawableRes
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
import androidx.constraintlayout.widget.ConstraintLayout
|
import androidx.constraintlayout.widget.ConstraintLayout
|
||||||
import androidx.core.widget.TextViewCompat
|
|
||||||
import com.google.android.material.bottomsheet.BottomSheetBehavior
|
import com.google.android.material.bottomsheet.BottomSheetBehavior
|
||||||
import com.google.android.material.bottomsheet.BottomSheetDialog
|
import com.google.android.material.bottomsheet.BottomSheetDialog
|
||||||
import com.google.android.material.textview.MaterialTextView
|
|
||||||
import eu.kanade.tachiyomi.R
|
import eu.kanade.tachiyomi.R
|
||||||
import eu.kanade.tachiyomi.databinding.BottomMenuSheetBinding
|
import eu.kanade.tachiyomi.databinding.BottomMenuSheetBinding
|
||||||
import eu.kanade.tachiyomi.util.system.dpToPx
|
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.hasSideNavBar
|
||||||
import eu.kanade.tachiyomi.util.system.isInNightMode
|
import eu.kanade.tachiyomi.util.system.isInNightMode
|
||||||
import eu.kanade.tachiyomi.util.view.expand
|
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.isVisible
|
||||||
import eu.kanade.tachiyomi.util.view.setBottomEdge
|
import eu.kanade.tachiyomi.util.view.setBottomEdge
|
||||||
import eu.kanade.tachiyomi.util.view.setEdgeToEdge
|
import eu.kanade.tachiyomi.util.view.setEdgeToEdge
|
||||||
import eu.kanade.tachiyomi.util.view.updateLayoutParams
|
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.util.view.visibleIf
|
||||||
|
import eu.kanade.tachiyomi.widget.MenuSheetItemView
|
||||||
|
|
||||||
|
@SuppressLint("InflateParams")
|
||||||
open class MaterialMenuSheet(
|
open class MaterialMenuSheet(
|
||||||
activity: Activity,
|
activity: Activity,
|
||||||
items: List<MenuSheetItem>,
|
items: List<MenuSheetItem>,
|
||||||
@ -72,42 +67,35 @@ open class MaterialMenuSheet(
|
|||||||
var currentIndex: Int? = null
|
var currentIndex: Int? = null
|
||||||
items.forEachIndexed { index, item ->
|
items.forEachIndexed { index, item ->
|
||||||
val view =
|
val view =
|
||||||
activity.layoutInflater.inflate(R.layout.menu_sheet_item, null) as ViewGroup
|
activity.layoutInflater.inflate(R.layout.menu_sheet_item, null) as MenuSheetItemView
|
||||||
val textView = view.getChildAt(0) as MaterialTextView
|
|
||||||
if (index == 0 && title == null) {
|
if (index == 0 && title == null) {
|
||||||
view.setBackgroundResource(R.drawable.rounded_item_background)
|
view.setBackgroundResource(R.drawable.rounded_item_background)
|
||||||
}
|
}
|
||||||
with(view) {
|
with(view) {
|
||||||
id = item.id
|
id = item.id
|
||||||
binding.menuLayout.addView(this)
|
binding.menuLayout.addView(this)
|
||||||
setOnClickListener {
|
|
||||||
val shouldDismiss = onMenuItemClicked(this@MaterialMenuSheet, id)
|
|
||||||
if (shouldDismiss) {
|
|
||||||
dismiss()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
with(textView) {
|
|
||||||
if (item.text != null) {
|
if (item.text != null) {
|
||||||
text = item.text
|
text = item.text
|
||||||
} else {
|
} else {
|
||||||
setText(item.textRes)
|
setText(item.textRes)
|
||||||
}
|
}
|
||||||
setCompoundDrawablesRelativeWithIntrinsicBounds(item.drawable, 0, 0, 0)
|
setIcon(item.drawable)
|
||||||
if (item.drawable == 0) {
|
if (item.drawable == 0) {
|
||||||
textSize = 14f
|
textSize = 14f
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.id == selectedId) {
|
if (item.id == selectedId) {
|
||||||
currentIndex = index
|
currentIndex = index
|
||||||
setTextColor(context.getResourceColor(R.attr.colorAccent))
|
setIconColor(activity.getResourceColor(R.attr.colorAccent))
|
||||||
TextViewCompat.setCompoundDrawableTintList(
|
setTextColor(activity.getResourceColor(R.attr.colorAccent))
|
||||||
this,
|
}
|
||||||
ColorStateList.valueOf(context.getResourceColor(R.attr.colorAccent))
|
|
||||||
)
|
setOnClickListener {
|
||||||
|
val shouldDismiss = onMenuItemClicked(this@MaterialMenuSheet, id)
|
||||||
|
if (shouldDismiss) {
|
||||||
|
dismiss()
|
||||||
}
|
}
|
||||||
updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
|
||||||
height = 48.dpToPx
|
|
||||||
width = MATCH_PARENT
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,7 +110,7 @@ open class MaterialMenuSheet(
|
|||||||
|
|
||||||
currentIndex?.let {
|
currentIndex?.let {
|
||||||
binding.root.post {
|
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() {
|
private fun clearEndDrawables() {
|
||||||
(0 until binding.menuLayout.childCount).forEach {
|
(0 until binding.menuLayout.childCount).forEach {
|
||||||
val textView = (binding.menuLayout.getChildAt(it) as ViewGroup).getChildAt(0) as TextView
|
val itemView = (binding.menuLayout.getChildAt(it) as MenuSheetItemView)
|
||||||
val imageView = (binding.menuLayout.getChildAt(it) as ViewGroup).getChildAt(1) as ImageView
|
itemView.setTextColor(primaryColor)
|
||||||
textView.setTextColor(primaryColor)
|
itemView.setIconColor(primaryColor)
|
||||||
TextViewCompat.setCompoundDrawableTintList(
|
itemView.setEndIcon(0)
|
||||||
textView,
|
|
||||||
ColorStateList.valueOf(primaryColor)
|
|
||||||
)
|
|
||||||
imageView.invisible()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,18 +153,10 @@ open class MaterialMenuSheet(
|
|||||||
if (clearAll) {
|
if (clearAll) {
|
||||||
clearEndDrawables()
|
clearEndDrawables()
|
||||||
}
|
}
|
||||||
val layout = binding.menuLayout.findViewById<ViewGroup>(id) ?: return
|
val layout = binding.menuLayout.findViewById<MenuSheetItemView>(id) ?: return
|
||||||
val textView = layout.getChildAt(0) as? TextView
|
layout.setTextColor(layout.context.getResourceColor(R.attr.colorAccent))
|
||||||
val imageView = layout.getChildAt(1) as? ImageView
|
layout.setIconColor(layout.context.getResourceColor(R.attr.colorAccent))
|
||||||
textView?.setTextColor(textView.context.getResourceColor(R.attr.colorAccent))
|
layout.setEndIcon(drawableRes)
|
||||||
textView?.let {
|
|
||||||
TextViewCompat.setCompoundDrawableTintList(
|
|
||||||
it,
|
|
||||||
ColorStateList.valueOf(it.context.getResourceColor(R.attr.colorAccent))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
imageView?.visible()
|
|
||||||
imageView?.setImageResource(drawableRes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data class MenuSheetItem(
|
data class MenuSheetItem(
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,21 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?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"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="42dp"
|
android:layout_height="48sp"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:background="?attr/selectableItemBackground"
|
android:background="?attr/selectableItemBackground"
|
||||||
android:clickable="true"
|
android:clickable="true"
|
||||||
android:focusable="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
|
<com.google.android.material.textview.MaterialTextView
|
||||||
|
android:id="@+id/item_text_view"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="42dp"
|
android:layout_height="48sp"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:drawablePadding="12dp"
|
android:drawablePadding="12dp"
|
||||||
android:drawableTint="?android:textColorPrimary"
|
android:drawableTint="?android:textColorPrimary"
|
||||||
@ -20,17 +24,17 @@
|
|||||||
android:paddingEnd="16dp"
|
android:paddingEnd="16dp"
|
||||||
android:textColor="?android:textColorPrimary"
|
android:textColor="?android:textColorPrimary"
|
||||||
android:textSize="13sp"
|
android:textSize="13sp"
|
||||||
android:tint="@color/md_white_1000_54"
|
android:tint="@color/md_white_1000_54" />
|
||||||
tools:drawableStart="@drawable/ic_share_24dp"
|
|
||||||
tools:text="@string/share" />
|
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
|
android:id="@+id/menu_end_item"
|
||||||
android:layout_width="42dp"
|
android:layout_width="42dp"
|
||||||
android:layout_height="42dp"
|
android:layout_height="42dp"
|
||||||
android:paddingStart="8dp"
|
android:paddingStart="8dp"
|
||||||
android:paddingEnd="8dp"
|
android:paddingEnd="8dp"
|
||||||
app:tint="?colorAccent"
|
app:tint="?colorAccent"
|
||||||
|
android:layout_gravity="center"
|
||||||
android:visibility="invisible"
|
android:visibility="invisible"
|
||||||
tools:visibility="visible"
|
tools:visibility="visible"
|
||||||
android:src="@drawable/ic_arrow_upward_24dp" />
|
android:src="@drawable/ic_arrow_upward_24dp" />
|
||||||
</LinearLayout>
|
</eu.kanade.tachiyomi.widget.MenuSheetItemView>
|
@ -26,5 +26,11 @@
|
|||||||
<attr name="summary" format="reference|string" />
|
<attr name="summary" format="reference|string" />
|
||||||
</declare-styleable>
|
</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>
|
<bool name="isTablet">false</bool>
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user