From b910db322b2f40cbe6bd89cc7afc1069eadd1a86 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Sun, 20 Oct 2019 10:54:55 +0200 Subject: [PATCH] Fixed snackbar behavior in contrast with bottom navigation --- .../topjohnwu/magisk/redesign/MainActivity.kt | 4 + .../utils/HideBottomViewOnScrollBehavior.kt | 77 +++++++++++++++++++ app/src/main/res/layout/activity_main_md2.xml | 1 - 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/HideBottomViewOnScrollBehavior.kt diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/MainActivity.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/MainActivity.kt index 1a0a43521..6b59776a0 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/MainActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/MainActivity.kt @@ -24,6 +24,7 @@ import com.topjohnwu.magisk.redesign.log.LogFragment import com.topjohnwu.magisk.redesign.module.ModuleFragment import com.topjohnwu.magisk.redesign.settings.SettingsFragment import com.topjohnwu.magisk.redesign.superuser.SuperuserFragment +import com.topjohnwu.magisk.utils.HideBottomViewOnScrollBehavior import com.topjohnwu.magisk.utils.HideTopViewOnScrollBehavior import com.topjohnwu.superuser.Shell import org.koin.androidx.viewmodel.ext.android.viewModel @@ -62,6 +63,9 @@ open class MainActivity : CompatActivity( binding.mainToolbarWrapper.updateLayoutParams { behavior = HideTopViewOnScrollBehavior() } + binding.mainBottomBar.updateLayoutParams { + behavior = HideBottomViewOnScrollBehavior() + } binding.mainNavigation.setOnNavigationItemSelectedListener { when (it.itemId) { R.id.homeFragment -> Navigation.home() diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/HideBottomViewOnScrollBehavior.kt b/app/src/main/java/com/topjohnwu/magisk/utils/HideBottomViewOnScrollBehavior.kt new file mode 100644 index 000000000..521a1fd5d --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/utils/HideBottomViewOnScrollBehavior.kt @@ -0,0 +1,77 @@ +package com.topjohnwu.magisk.utils + +import android.view.View +import android.view.ViewGroup +import androidx.coordinatorlayout.widget.CoordinatorLayout +import androidx.core.view.isVisible +import androidx.core.view.updateLayoutParams +import androidx.interpolator.view.animation.FastOutSlowInInterpolator +import com.google.android.material.behavior.HideBottomViewOnScrollBehavior +import com.google.android.material.snackbar.Snackbar +import com.topjohnwu.magisk.R +import kotlin.math.roundToInt + +class HideBottomViewOnScrollBehavior : HideBottomViewOnScrollBehavior() { + + override fun layoutDependsOn(parent: CoordinatorLayout, child: T, dependency: View) = + super.layoutDependsOn(parent, child, dependency) or (dependency is Snackbar.SnackbarLayout) + + override fun onDependentViewChanged( + parent: CoordinatorLayout, + child: T, + dependency: View + ) = when (dependency) { + is Snackbar.SnackbarLayout -> onDependentViewChanged(parent, child, dependency) + else -> super.onDependentViewChanged(parent, child, dependency) + } + + override fun onDependentViewRemoved( + parent: CoordinatorLayout, + child: T, + dependency: View + ) = when (dependency) { + is Snackbar.SnackbarLayout -> onDependentViewRemoved(parent, child, dependency) + else -> super.onDependentViewRemoved(parent, child, dependency) + } + + //--- + + private fun onDependentViewChanged( + parent: CoordinatorLayout, + child: T, + dependency: Snackbar.SnackbarLayout + ): Boolean { + val viewMargin = (child.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin + val additionalMargin = dependency.resources.getDimension(R.dimen.l1).roundToInt() + val translation = dependency.height + additionalMargin + + dependency.updateLayoutParams { + bottomMargin = viewMargin + } + + // checks whether the navigation is not hidden via scroll + if (child.isVisible && child.translationY <= 0) { + child.translationY(-translation.toFloat()) + } + return false + } + + private fun onDependentViewRemoved( + parent: CoordinatorLayout, + child: T, + dependency: Snackbar.SnackbarLayout + ) { + // checks whether the navigation is not hidden via scroll + if (child.isVisible && child.translationY <= 0) { + child.translationY(0f) + } + } + + //--- + + private fun View.translationY(destination: Float) = animate() + .translationY(destination) + .setInterpolator(FastOutSlowInInterpolator()) + .start() + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main_md2.xml b/app/src/main/res/layout/activity_main_md2.xml index a9873680d..84b29e78c 100644 --- a/app/src/main/res/layout/activity_main_md2.xml +++ b/app/src/main/res/layout/activity_main_md2.xml @@ -56,7 +56,6 @@ style="?styleCardElevated" android:layout_width="match_parent" android:layout_height="wrap_content" - app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior" android:layout_gravity="bottom" android:layout_marginStart="@dimen/l1" android:layout_marginEnd="@dimen/l1"