Added Bottom sheet for menu popup

For use in future commits
This commit is contained in:
Jay 2020-05-17 02:20:02 -04:00
parent 6c27147166
commit 68cf3cab10
3 changed files with 252 additions and 0 deletions

View File

@ -0,0 +1,147 @@
package eu.kanade.tachiyomi.ui.library
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
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 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.util.system.dpToPx
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.setTextColorRes
import eu.kanade.tachiyomi.util.view.updateLayoutParams
import eu.kanade.tachiyomi.util.view.visible
import eu.kanade.tachiyomi.util.view.visibleIf
import kotlinx.android.synthetic.main.bottom_menu_sheet.*
class MaterialMenuSheet(
activity: Activity,
items: List<MenuSheetItem>,
title: String? = null,
selectedId: Int? = null,
onMenuItemClicked: (MaterialMenuSheet, Int) -> Boolean
) :
BottomSheetDialog
(activity, R.style.BottomSheetDialogTheme) {
val primaryColor = activity.getResourceColor(android.R.attr.textColorPrimary)
private val view = activity.layoutInflater.inflate(R.layout.bottom_menu_sheet, null)
init {
setContentView(view)
setEdgeToEdge(activity, view)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !context.isInNightMode() && !activity.window.decorView.rootWindowInsets.hasSideNavBar()) {
window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
items.forEach {
val view =
activity.layoutInflater.inflate(R.layout.menu_sheet_item, null) as ViewGroup
val textView = view.getChildAt(0) as MaterialTextView
with(view) {
id = it.id
menu_layout.addView(this)
setOnClickListener {
val shouldDismiss = onMenuItemClicked(this@MaterialMenuSheet, id)
if (shouldDismiss) {
dismiss()
}
}
}
with(textView) {
if (it.text != null) {
text = it.text
} else {
setText(it.textRes)
}
setCompoundDrawablesRelativeWithIntrinsicBounds(it.drawable, 0, 0, 0)
if (it.id == selectedId) {
setTextColorRes(R.color.colorAccent)
compoundDrawableTintList =
ColorStateList.valueOf(context.getColor(R.color.colorAccent))
}
updateLayoutParams<ViewGroup.MarginLayoutParams> {
height = 48.dpToPx
width = MATCH_PARENT
}
}
}
BottomSheetBehavior.from(view.parent as ViewGroup).expand()
BottomSheetBehavior.from(view.parent as ViewGroup).skipCollapsed = true
setBottomEdge(menu_layout, activity)
title_layout.visibleIf(title != null)
toolbar_title.text = title
var isNotElevated = false
var elevationAnimator: ValueAnimator? = null
if (title_layout.isVisible()) {
menu_scroll_view.setOnScrollChangeListener { _: View?, _: Int, _: Int, _: Int, _: Int ->
val atTop = !menu_scroll_view.canScrollVertically(-1)
if (atTop != isNotElevated) {
elevationAnimator?.cancel()
isNotElevated = atTop
elevationAnimator?.cancel()
elevationAnimator = ObjectAnimator.ofFloat(
title_layout,
"elevation",
title_layout.elevation,
if (atTop) 0f else 10f.dpToPx
)
elevationAnimator?.duration = 100
elevationAnimator?.start()
}
}
}
}
private fun clearEndDrawables() {
(0 until menu_layout.childCount).forEach {
val textView = (menu_layout.getChildAt(it) as ViewGroup).getChildAt(0) as TextView
val imageView = (menu_layout.getChildAt(it) as ViewGroup).getChildAt(1) as ImageView
textView.setTextColor(primaryColor)
textView.compoundDrawableTintList = ColorStateList.valueOf(primaryColor)
imageView.invisible()
}
}
fun setDrawable(id: Int, @DrawableRes drawableRes: Int, clearAll: Boolean = true) {
if (clearAll) {
clearEndDrawables()
}
val layout = menu_layout.findViewById<ViewGroup>(id) ?: return
val textView = layout.getChildAt(0) as? TextView
val imageView = layout.getChildAt(1) as? ImageView
textView?.setTextColorRes(R.color.colorAccent)
textView?.compoundDrawableTintList =
ColorStateList.valueOf(context.getColor(R.color.colorAccent))
imageView?.visible()
imageView?.setImageResource(drawableRes)
}
data class MenuSheetItem(
val id: Int,
@DrawableRes val drawable: Int = 0,
@StringRes val textRes: Int = 0,
val text: String? = null
)
}

View File

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/source_filter_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_sheet_rounded_background"
app:layout_constraintVertical_chainStyle="packed"
android:backgroundTint="?android:attr/colorBackground">
<androidx.core.widget.NestedScrollView
android:id="@+id/menu_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constrainedHeight="true"
app:layout_constraintTop_toBottomOf="@id/title_layout">
<LinearLayout
android:id="@+id/menu_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</androidx.core.widget.NestedScrollView>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/title_layout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_gravity="top"
android:background="@drawable/bottom_sheet_rounded_background"
android:backgroundTint="?attr/colorSecondary"
android:clickable="true"
android:elevation="0dp"
android:focusable="true"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@id/menu_scroll_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6"
android:textColor="?actionBarTintColor"
android:textSize="17sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Title Text" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/divider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal">
<com.google.android.material.textview.MaterialTextView
android:layout_width="0dp"
android:layout_height="42dp"
android:layout_weight="1"
android:drawablePadding="12dp"
android:drawableTint="?android:textColorPrimary"
android:gravity="center|start"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:textColor="?android:textColorPrimary"
android:textSize="13sp"
android:tint="@color/md_white_1000_54"
tools:drawableStart="@drawable/ic_share_grey_24dp"
tools:text="@string/share" />
<ImageView
android:layout_width="42dp"
android:layout_height="42dp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:tint="@color/colorAccent"
android:visibility="invisible"
tools:visibility="visible"
android:src="@drawable/ic_arrow_up_white_24dp" />
</LinearLayout>