From f78ae4a81872e2e22447467e1d6d40d1725f171f Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 18 Aug 2022 11:35:21 +0300 Subject: [PATCH] Make BottomSheet`s handle fit top window inset --- .../base/ui/widgets/BottomSheetHeaderBar.kt | 45 ++++++++++++------- .../main/res/layout/layout_sheet_header.xml | 33 ++++++-------- app/src/main/res/values/dimens.xml | 2 + 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/base/ui/widgets/BottomSheetHeaderBar.kt b/app/src/main/java/org/koitharu/kotatsu/base/ui/widgets/BottomSheetHeaderBar.kt index 3aaa7270d..19757304e 100644 --- a/app/src/main/java/org/koitharu/kotatsu/base/ui/widgets/BottomSheetHeaderBar.kt +++ b/app/src/main/java/org/koitharu/kotatsu/base/ui/widgets/BottomSheetHeaderBar.kt @@ -2,15 +2,11 @@ package org.koitharu.kotatsu.base.ui.widgets import android.animation.LayoutTransition import android.content.Context -import android.transition.AutoTransition -import android.transition.TransitionManager import android.util.AttributeSet import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.WindowInsets -import android.view.animation.AccelerateDecelerateInterpolator -import android.view.animation.DecelerateInterpolator import androidx.annotation.AttrRes import androidx.annotation.StringRes import androidx.appcompat.widget.Toolbar @@ -43,7 +39,10 @@ class BottomSheetHeaderBar @JvmOverloads constructor( private val locationBuffer = IntArray(2) private val expansionListeners = LinkedList() private var fitStatusBar = false - private var transition: AutoTransition? = null + private val minHandleHeight = context.resources.getDimensionPixelSize(R.dimen.bottom_sheet_handle_size_min) + private val maxHandleHeight = context.resources.getDimensionPixelSize(R.dimen.bottom_sheet_handle_size_max) + private var isLayoutSuppressedCompat = false + private var isLayoutCalledWhileSuppressed = false @Deprecated("") val toolbar: MaterialToolbar @@ -156,6 +155,14 @@ class BottomSheetHeaderBar @JvmOverloads constructor( binding.toolbar.setSubtitle(resId) } + override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { + if (isLayoutSuppressedCompat) { + isLayoutCalledWhileSuppressed = true + } else { + super.onLayout(changed, l, t, r, b) + } + } + private fun setBottomSheetBehavior(behavior: BottomSheetBehavior<*>?) { bottomSheetBehavior?.removeBottomSheetCallback(bottomSheetCallback) bottomSheetBehavior = behavior @@ -170,11 +177,21 @@ class BottomSheetHeaderBar @JvmOverloads constructor( if (isExpanded == binding.dragHandle.isGone) { return } - TransitionManager.beginDelayedTransition(this, getTransition()) + suppressLayoutCompat(true) binding.toolbar.navigationIcon = (if (isExpanded) closeDrawable else null) binding.dragHandle.isGone = isExpanded expansionListeners.forEach { it.onExpansionStateChanged(this, isExpanded) } dispatchInsets(ViewCompat.getRootWindowInsets(this)) + suppressLayoutCompat(false) + } + + private fun suppressLayoutCompat(suppress: Boolean) { + if (suppress == isLayoutSuppressedCompat) return + isLayoutSuppressedCompat = suppress + if (!suppress && isLayoutCalledWhileSuppressed) { + requestLayout() + } + isLayoutCalledWhileSuppressed = false } private fun dispatchInsets(insets: WindowInsetsCompat?) { @@ -182,11 +199,14 @@ class BottomSheetHeaderBar @JvmOverloads constructor( return } val isExpanded = binding.dragHandle.isGone + val topInset = insets?.getInsets(WindowInsetsCompat.Type.systemBars())?.top ?: 0 if (isExpanded) { - val topInset = insets?.getInsets(WindowInsetsCompat.Type.systemBars())?.top ?: 0 updatePadding(top = topInset) } else { updatePadding(top = 0) + binding.dragHandle.updateLayoutParams { + height = topInset.coerceIn(minHandleHeight, maxHandleHeight) + } } } @@ -225,7 +245,7 @@ class BottomSheetHeaderBar @JvmOverloads constructor( return true } val viewId = child.id - return viewId == R.id.dragHandle || viewId == R.id.toolbar || viewId == R.id.frame + return viewId == R.id.dragHandle || viewId == R.id.toolbar } private fun convertLayoutParams(params: ViewGroup.LayoutParams?): Toolbar.LayoutParams? { @@ -242,15 +262,6 @@ class BottomSheetHeaderBar @JvmOverloads constructor( } } - private fun getTransition(): AutoTransition { - transition?.let { return it } - val t = AutoTransition() - t.duration = context.getAnimationDuration(android.R.integer.config_shortAnimTime) - t.addTarget(binding.dragHandle) - transition = t - return t - } - private inner class Callback : BottomSheetBehavior.BottomSheetCallback(), View.OnClickListener { override fun onStateChanged(bottomSheet: View, newState: Int) { diff --git a/app/src/main/res/layout/layout_sheet_header.xml b/app/src/main/res/layout/layout_sheet_header.xml index 47ea53857..d0ec2a0e3 100644 --- a/app/src/main/res/layout/layout_sheet_header.xml +++ b/app/src/main/res/layout/layout_sheet_header.xml @@ -6,27 +6,20 @@ android:layout_height="wrap_content" tools:parentTag="com.google.android.material.appbar.AppBarLayout"> - + android:layout_height="@dimen/bottom_sheet_handle_size_min" + android:minHeight="0dp" + android:paddingTop="12dp" + android:paddingBottom="0dp" /> - - - - - + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index a9459a31a..f68a30c84 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -35,6 +35,8 @@ 4dp 0dp + 16dp + 24dp 8dp 8dp