Change how the bottom navigation is hidden (#5823)

* Change how the bottom navigation is hidden

Modifies the translationY instead of the height.

* Cleanups
This commit is contained in:
Ivan Iskandar
2021-09-17 04:37:17 +07:00
committed by GitHub
parent be001d090c
commit f125ab01ee
7 changed files with 193 additions and 150 deletions

View File

@@ -383,7 +383,7 @@ class LibraryController(
actionMode!!,
R.menu.library_selection
) { onActionItemClicked(it!!) }
(activity as? MainActivity)?.showBottomNav(visible = false, expand = true)
(activity as? MainActivity)?.showBottomNav(false)
}
}
@@ -492,7 +492,7 @@ class LibraryController(
selectionRelay.call(LibrarySelectionEvent.Cleared())
binding.actionToolbar.hide()
(activity as? MainActivity)?.showBottomNav(visible = true, expand = true)
(activity as? MainActivity)?.showBottomNav(true)
actionMode = null
}

View File

@@ -10,7 +10,6 @@ import android.view.Gravity
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.view.ActionMode
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.animation.doOnEnd
import androidx.core.splashscreen.SplashScreen
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
@@ -64,7 +63,6 @@ import eu.kanade.tachiyomi.util.system.dpToPx
import eu.kanade.tachiyomi.util.system.isTablet
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.view.setNavigationBarTransparentCompat
import eu.kanade.tachiyomi.widget.HideBottomNavigationOnScrollBehavior
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn
@@ -86,8 +84,6 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
}
}
private var bottomNavAnimator: ViewHeightAnimator? = null
private var isConfirmingExit: Boolean = false
private var isHandlingShortcut: Boolean = false
@@ -138,15 +134,6 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
}
setSplashScreenExitAnimation(splashScreen)
if (binding.bottomNav != null) {
bottomNavAnimator = ViewHeightAnimator(binding.bottomNav!!)
// Set behavior of bottom nav
preferences.hideBottomBarOnScroll()
.asImmediateFlow { setBottomNavBehaviorOnScroll() }
.launchIn(lifecycleScope)
}
if (binding.sideNav != null) {
preferences.sideNavIconAlignment()
.asImmediateFlow {
@@ -532,11 +519,11 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
binding.appbar.setExpanded(true)
if ((from == null || from is RootController) && to !is RootController) {
showNav(visible = false, expand = true)
showNav(false)
}
if (to is RootController) {
// Always show bottom nav again when returning to a RootController
showNav(visible = true, expand = from !is RootController)
showNav(true)
}
if (from is TabbedController) {
@@ -587,27 +574,22 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
}
}
private fun showNav(visible: Boolean, expand: Boolean = false) {
showBottomNav(visible, expand)
private fun showNav(visible: Boolean) {
showBottomNav(visible)
showSideNav(visible)
}
// Also used from some controllers to swap bottom nav with action toolbar
fun showBottomNav(visible: Boolean, expand: Boolean = false) {
fun showBottomNav(visible: Boolean) {
if (visible) {
binding.bottomNav?.translationY = 0F
if (expand) {
bottomNavAnimator?.expand()
}
binding.bottomNav?.slideUp()
} else {
bottomNavAnimator?.collapse()
binding.bottomNav?.slideDown()
}
}
private fun showSideNav(visible: Boolean) {
binding.sideNav?.let {
it.isVisible = visible
}
binding.sideNav?.isVisible = visible
}
/**
@@ -622,18 +604,6 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
}
}
private fun setBottomNavBehaviorOnScroll() {
showNav(visible = true)
binding.bottomNav?.updateLayoutParams<CoordinatorLayout.LayoutParams> {
behavior = when {
preferences.hideBottomBarOnScroll().get() -> HideBottomNavigationOnScrollBehavior()
else -> null
}
}
binding.bottomNav?.translationY = 0F
}
private val nav: NavigationBarView
get() = binding.bottomNav ?: binding.sideNav!!

View File

@@ -1,107 +0,0 @@
package eu.kanade.tachiyomi.ui.main
import android.animation.ObjectAnimator
import android.view.View
import android.view.ViewTreeObserver
import android.view.animation.DecelerateInterpolator
import androidx.annotation.Keep
class ViewHeightAnimator(val view: View, val duration: Long = 250L) {
/**
* The default height of the view. It's unknown until the view is layout.
*/
private var height = 0
/**
* Whether the last state of the view is shown or hidden.
*/
private var isLastStateShown = true
/**
* Animation used to expand and collapse the view.
*/
private val animation by lazy {
ObjectAnimator.ofInt(this, "height", height).apply {
duration = this@ViewHeightAnimator.duration
interpolator = DecelerateInterpolator()
}
}
init {
view.viewTreeObserver.addOnGlobalLayoutListener(
object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (view.height > 0) {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
// Save the tabs default height.
height = view.height
// Now that we know the height, set the initial height.
if (isLastStateShown) {
setHeight(height)
} else {
setHeight(0)
}
}
}
}
)
}
/**
* Sets the height of the tab layout.
*
* @param newHeight The new height of the tab layout.
*/
@Keep
fun setHeight(newHeight: Int) {
view.layoutParams.height = newHeight
view.requestLayout()
}
/**
* Returns the height of the tab layout. This method is also called from the animator through
* reflection.
*/
fun getHeight(): Int {
return view.layoutParams.height
}
/**
* Expands the tab layout with an animation.
*/
fun expand() {
if (isMeasured) {
if (getHeight() != height) {
animation.setIntValues(height)
animation.start()
} else {
animation.cancel()
}
}
isLastStateShown = true
}
/**
* Collapse the tab layout with an animation.
*/
fun collapse() {
if (isMeasured) {
if (getHeight() != 0) {
animation.setIntValues(0)
animation.start()
} else {
animation.cancel()
}
}
isLastStateShown = false
}
/**
* Returns whether the tab layout has a known height.
*/
private val isMeasured: Boolean
get() = height > 0
}

View File

@@ -180,7 +180,7 @@ class UpdatesController :
actionMode!!,
R.menu.updates_chapter_selection
) { onActionItemClicked(it!!) }
(activity as? MainActivity)?.showBottomNav(visible = false, expand = true)
(activity as? MainActivity)?.showBottomNav(false)
}
toggleSelection(position)
@@ -386,7 +386,7 @@ class UpdatesController :
adapter?.clearSelection()
binding.actionToolbar.hide()
(activity as? MainActivity)?.showBottomNav(visible = true, expand = true)
(activity as? MainActivity)?.showBottomNav(true)
actionMode = null
}