From d9d6d4ed2927ea9288119e3a8f9ee769681bd2cf Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Thu, 25 Mar 2021 22:48:17 -0400 Subject: [PATCH] Bottom nav now snaps to bottom or top after scroll --- .../tachiyomi/ui/library/LibraryController.kt | 10 +++--- .../tachiyomi/ui/recents/RecentsController.kt | 14 ++------- .../tachiyomi/ui/source/SourceController.kt | 14 ++------- .../util/view/ControllerExtensions.kt | 31 ++++++++++++++++--- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index e2987bef9a..b9ed58f18d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -235,7 +235,6 @@ class LibraryController( showCategoryText(currentCategory.name) } } - updateFilterSheetY() val savedCurrentCategory = getHeader(true)?.category ?: return if (savedCurrentCategory.order != lastUsedCategory) { lastUsedCategory = savedCurrentCategory.order @@ -252,7 +251,6 @@ class LibraryController( } RecyclerView.SCROLL_STATE_IDLE -> { updateHopperPosition() - updateFilterSheetY() } } } @@ -262,7 +260,6 @@ class LibraryController( val bottomBar = activity?.bottom_nav filter_bottom_sheet ?: return if (bottomBar != null) { - updateHopperY() if (filter_bottom_sheet.sheetBehavior.isHidden()) { val pad = bottomBar.translationY - bottomBar.height filter_bottom_sheet.translationY = pad @@ -283,6 +280,7 @@ class LibraryController( view?.rootWindowInsets?.getBottomGestureInsets() ?: 0 ) filter_bottom_sheet.sheetBehavior?.peekHeight = 60.dpToPx + padding + updateHopperY() fast_scroller.updateLayoutParams { bottomMargin = -pad.toInt() } @@ -387,9 +385,6 @@ class LibraryController( adapter = LibraryCategoryAdapter(this) setRecyclerLayout() - recycler.post { - updateFilterSheetY() - } recycler.manager.spanSizeLookup = ( object : GridLayoutManager.SpanSizeLookup() { @@ -442,6 +437,9 @@ class LibraryController( }, onLeavingController = { header_title?.gone() + }, + onBottomNavUpdate = { + updateFilterSheetY() } ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt index 694c7641ee..0589bbc873 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt @@ -134,6 +134,9 @@ class RecentsController(bundle: Bundle? = null) : afterInsets = { headerHeight = it.systemWindowInsetTop + appBarHeight recycler.updatePaddingRelative(bottom = activity!!.bottom_nav.height) + }, + onBottomNavUpdate = { + setBottomPadding() } ) @@ -144,17 +147,6 @@ class RecentsController(bundle: Bundle? = null) : adapter.addScrollableHeader(presenter.generalHeader) } } - recycler?.post { - setBottomPadding() - } - recycler.addOnScrollListener( - object : RecyclerView.OnScrollListener() { - override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { - super.onScrolled(recyclerView, dx, dy) - setBottomPadding() - } - } - ) dl_bottom_sheet.onCreate(this) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/source/SourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/source/SourceController.kt index 3cbcadd3c7..370a9039ad 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/source/SourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/source/SourceController.kt @@ -13,7 +13,6 @@ import android.view.ViewGroup import androidx.appcompat.widget.SearchView import androidx.core.content.ContextCompat import androidx.core.graphics.ColorUtils -import androidx.recyclerview.widget.RecyclerView import com.bluelinelabs.conductor.ControllerChangeHandler import com.bluelinelabs.conductor.ControllerChangeType import com.bluelinelabs.conductor.RouterTransaction @@ -153,21 +152,15 @@ class SourceController : afterInsets = { headerHeight = it.systemWindowInsetTop + appBarHeight recycler.updatePaddingRelative(bottom = activity?.bottom_nav?.height ?: 0) + }, + onBottomNavUpdate = { + setBottomPadding() } ) recycler?.post { - setBottomPadding() setBottomSheetTabs(if (ext_bottom_sheet?.sheetBehavior.isCollapsed()) 0f else 1f) } - recycler.addOnScrollListener( - object : RecyclerView.OnScrollListener() { - override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { - super.onScrolled(recyclerView, dx, dy) - setBottomPadding() - } - } - ) requestPermissionsSafe(arrayOf(WRITE_EXTERNAL_STORAGE), 301) ext_bottom_sheet.onCreate(this) @@ -230,7 +223,6 @@ class SourceController : fun setBottomSheetTabs(progress: Float) { val bottomSheet = ext_bottom_sheet ?: return - val bottomBar = activity?.bottom_nav ?: return ext_bottom_sheet.tabs.updateLayoutParams { topMargin = ((activity?.appbar?.height?.minus(9f.dpToPx) ?: 0f) * progress).toInt() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt index b43895339d..5e64378591 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt @@ -104,7 +104,8 @@ fun Controller.scrollViewWith( swipeRefreshLayout: SwipeRefreshLayout? = null, afterInsets: ((WindowInsets) -> Unit)? = null, liftOnScroll: ((Boolean) -> Unit)? = null, - onLeavingController: (() -> Unit)? = null + onLeavingController: (() -> Unit)? = null, + onBottomNavUpdate: (() -> Unit)? = null ): ((Boolean) -> Unit) { var statusBarHeight = -1 activity?.appbar?.y = 0f @@ -120,6 +121,9 @@ fun Controller.scrollViewWith( recycler.requestApplyInsets() } } + recycler.post { + onBottomNavUpdate?.invoke() + } val randomTag = Random.nextLong() var lastY = 0f var fakeToolbarView: View? = null @@ -219,10 +223,17 @@ fun Controller.scrollViewWith( val shortAnimationDuration = resources?.getInteger( android.R.integer.config_shortAnimTime ) ?: 0 - activity!!.appbar.animate().y(0f).setDuration(shortAnimationDuration.toLong()) + activity!!.appbar.animate().y(0f) + .setDuration(shortAnimationDuration.toLong()) .start() - activity!!.bottom_nav?.animate()?.translationYBy(0f)?.setDuration(shortAnimationDuration.toLong()) - ?.start() + activity!!.bottom_nav?.let { + val animator = it.animate()?.translationY(0f) + ?.setDuration(shortAnimationDuration.toLong()) + animator?.setUpdateListener { + onBottomNavUpdate?.invoke() + } + animator?.start() + } lastY = 0f if (elevate) elevateFunc(false) } else { @@ -243,6 +254,7 @@ fun Controller.scrollViewWith( tabBar.height.toFloat() ) activity!!.bottom_view?.translationY = tabBar.translationY + onBottomNavUpdate?.invoke() } else if (tabBar.translationY != 0f) { tabBar.translationY = 0f activity!!.bottom_view?.translationY = 0f @@ -279,6 +291,17 @@ fun Controller.scrollViewWith( val atTop = !recycler.canScrollVertically(-1) lastY = if (closerToTop && !atTop) (-activity!!.appbar.height.toFloat()) else 0f activity!!.appbar.animate().y(lastY).setDuration(shortAnimationDuration.toLong()).start() + activity!!.bottom_nav?.let { + val halfWayBottom = abs((it.height.toFloat()) / 2) + val closerToBottom = it.translationY > halfWayBottom + val lastBottomY = if (closerToBottom && !atTop) it.height.toFloat() else 0f + val animator = it.animate()?.translationY(lastBottomY) + ?.setDuration(shortAnimationDuration.toLong()) + animator?.setUpdateListener { + onBottomNavUpdate?.invoke() + } + animator?.start() + } if (recycler.canScrollVertically(-1) && !elevate) elevateFunc(true) else if (!recycler.canScrollVertically(-1) && elevate) elevateFunc(false) }