For compact screens (landscape phones) shrink app bar to compact while searching

Which also fixes recyclers scrolling to the top while searching on a config change
This commit is contained in:
Jays2Kings 2022-04-21 01:47:15 -04:00
parent 1666f8ba4c
commit 4eabe7956e
4 changed files with 57 additions and 15 deletions

View file

@ -49,6 +49,8 @@ class ExpandedAppBarLayout@JvmOverloads constructor(context: Context, attrs: Att
val useLargeToolbar: Boolean
get() = preferences.useLargeToolbar() && !isExtraSmall
var compactSearchMode = false
/** Defines how the toolbar layout should be */
private var toolbarMode = ToolbarState.EXPANDED
set(value) {
@ -104,6 +106,14 @@ class ExpandedAppBarLayout@JvmOverloads constructor(context: Context, attrs: Att
toolbarMode == ToolbarState.EXPANDED
)
val preLayoutHeightWhileSearching: Int
get() = getEstimatedLayout(
cardFrame?.isVisible == true && toolbarMode == ToolbarState.EXPANDED,
useTabsInPreLayout,
toolbarMode == ToolbarState.EXPANDED,
true
)
/** Small toolbar height + top system insets, same size as a collapsed appbar */
private val compactAppBarHeight: Float
get() {
@ -119,7 +129,7 @@ class ExpandedAppBarLayout@JvmOverloads constructor(context: Context, attrs: Att
private val minTabletHeight: Int
get() {
val tabHeight = if (tabsFrameLayout?.isVisible == true) 48.dpToPx else 0
return if (context.isTablet()) {
return if (context.isTablet() || (compactSearchMode && toolbarMode == ToolbarState.EXPANDED)) {
(mainToolbar?.height ?: 0) + paddingTop + tabHeight
} else {
0
@ -185,13 +195,18 @@ class ExpandedAppBarLayout@JvmOverloads constructor(context: Context, attrs: Att
override fun setTranslationY(translationY: Float) {
if (lockYPos) return
val realHeight = (preLayoutHeight + paddingTop).toFloat()
val newY = MathUtils.clamp(translationY, -realHeight + minTabletHeight, 0f)
val realHeight = (preLayoutHeightWhileSearching + paddingTop).toFloat()
val newY = MathUtils.clamp(
translationY,
-realHeight + (if (context.isTablet()) minTabletHeight else 0),
if (compactSearchMode && toolbarMode == ToolbarState.EXPANDED) -realHeight + top + minTabletHeight else 0f
)
super.setTranslationY(newY)
}
fun getEstimatedLayout(includeSearchToolbar: Boolean, includeTabs: Boolean, includeLargeToolbar: Boolean): Int {
val hasLargeToolbar = includeLargeToolbar && useLargeToolbar
fun getEstimatedLayout(includeSearchToolbar: Boolean, includeTabs: Boolean, includeLargeToolbar: Boolean, ignoreSearch: Boolean = false): Int {
val hasLargeToolbar = includeLargeToolbar && useLargeToolbar &&
((!compactSearchMode && toolbarMode == ToolbarState.EXPANDED) || ignoreSearch)
val appBarHeight = attrToolbarHeight * (if (includeSearchToolbar && hasLargeToolbar) 2 else 1)
val widthMeasureSpec = MeasureSpec.makeMeasureSpec(resources.displayMetrics.widthPixels, MeasureSpec.AT_MOST)
val heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
@ -240,11 +255,18 @@ class ExpandedAppBarLayout@JvmOverloads constructor(context: Context, attrs: Att
if (lockYPos) return
val offset = recyclerView?.computeVerticalScrollOffset() ?: 0
val bigHeight = bigView?.height ?: 0
val realHeight = preLayoutHeight + paddingTop
val realHeight = preLayoutHeightWhileSearching + paddingTop
val tabHeight = if (tabsFrameLayout?.isVisible == true) 48.dpToPx else 0
val shortH = if (toolbarMode != ToolbarState.EXPANDED) 0f else compactAppBarHeight
val shortH = if (toolbarMode != ToolbarState.EXPANDED || compactSearchMode) 0f else compactAppBarHeight
val smallHeight = -realHeight + shortH + tabHeight
val newY = when {
toolbarMode == ToolbarState.EXPANDED && compactSearchMode -> {
MathUtils.clamp(
translationY,
-realHeight.toFloat() + top + if (context.isTablet()) minTabletHeight else 0,
-realHeight.toFloat() + top + minTabletHeight
)
}
toolbarMode != ToolbarState.EXPANDED -> {
translationY
}
@ -274,8 +296,15 @@ class ExpandedAppBarLayout@JvmOverloads constructor(context: Context, attrs: Att
else -> bigHeight.toFloat()
}
}
if (toolbarMode != ToolbarState.EXPANDED) {
useSearchToolbarForMenu(offset > realHeight - shortH - tabHeight)
if (toolbarMode != ToolbarState.EXPANDED || compactSearchMode) {
if (compactSearchMode && toolbarMode == ToolbarState.EXPANDED) {
bigView?.alpha = 0f
mainToolbar?.alpha = 0f
cardFrame?.backgroundColor = null
} else {
mainToolbar?.alpha = 1f
}
useSearchToolbarForMenu(compactSearchMode || offset > realHeight - shortH - tabHeight)
return
}
val alpha =
@ -312,10 +341,10 @@ class ExpandedAppBarLayout@JvmOverloads constructor(context: Context, attrs: Att
if (toolbarMode != ToolbarState.EXPANDED) android.R.integer.config_shortAnimTime
else android.R.integer.config_longAnimTime
) ?: 0
val realHeight = preLayoutHeight + paddingTop
val realHeight = preLayoutHeightWhileSearching + paddingTop
val closerToTop = abs(y) > realHeight - halfWay
val atTop = !recyclerView.canScrollVertically(-1)
val shortH = if (toolbarMode != ToolbarState.EXPANDED) 0f else compactAppBarHeight
val shortH = if (toolbarMode != ToolbarState.EXPANDED || compactSearchMode) 0f else compactAppBarHeight
val lastY = if (closerToTop && !atTop) {
-height.toFloat()
} else {

View file

@ -1119,6 +1119,9 @@ class LibraryController(
private fun showCategories(show: Boolean, closeSearch: Boolean = false) {
binding.recyclerCover.isClickable = show
binding.recyclerCover.isFocusable = show
if (show) {
moveRecyclerViewUp()
}
if (closeSearch) {
activityBinding?.cardToolbar?.searchItem?.collapseActionView()
}
@ -1133,6 +1136,7 @@ class LibraryController(
binding.recyclerCover.animate().translationY(translateY).start()
binding.recyclerCover.animate().alpha(if (show) 0.75f else 0f).start()
binding.libraryGridRecycler.recycler.suppressLayout(show)
activityBinding?.appBar?.updateAppBarAfterY(binding.libraryGridRecycler.recycler)
binding.swipeRefresh.isEnabled = !show
setSubtitle()
if (show) {
@ -1232,7 +1236,6 @@ class LibraryController(
viewScope.launchUI {
adapter.performFilterAsync()
}
moveRecyclerViewUp()
return true
}

View file

@ -97,7 +97,7 @@ import eu.kanade.tachiyomi.util.view.blurBehindWindow
import eu.kanade.tachiyomi.util.view.doOnApplyWindowInsetsCompat
import eu.kanade.tachiyomi.util.view.findChild
import eu.kanade.tachiyomi.util.view.getItemView
import eu.kanade.tachiyomi.util.view.moveRecyclerViewUp
import eu.kanade.tachiyomi.util.view.mainRecyclerView
import eu.kanade.tachiyomi.util.view.snack
import eu.kanade.tachiyomi.util.view.withFadeInTransaction
import eu.kanade.tachiyomi.util.view.withFadeTransaction
@ -348,7 +348,13 @@ open class MainActivity : BaseActivity<MainActivityBinding>(), DownloadServiceLi
binding.cardToolbar.searchItem?.setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
val controller = router.backstack.lastOrNull()?.controller
controller?.moveRecyclerViewUp()
binding.appBar.compactSearchMode = binding.appBar.useLargeToolbar && resources.configuration.screenHeightDp < 600
if (binding.appBar.compactSearchMode) {
setFloatingToolbar(true)
controller?.mainRecyclerView?.requestApplyInsets()
binding.appBar.y = 0f
binding.appBar.updateAppBarAfterY(controller?.mainRecyclerView)
}
(controller as? BaseController<*>)?.onActionViewExpand(item)
(controller as? SettingsController)?.onActionViewExpand(item)
binding.cardToolbar.menu.forEach { it.isVisible = false }
@ -357,6 +363,8 @@ open class MainActivity : BaseActivity<MainActivityBinding>(), DownloadServiceLi
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
val controller = router.backstack.lastOrNull()?.controller
binding.appBar.compactSearchMode = false
controller?.mainRecyclerView?.requestApplyInsets()
setupSearchTBMenu(binding.toolbar.menu, true)
(controller as? BaseController<*>)?.onActionViewCollapse(item)
(controller as? SettingsController)?.onActionViewCollapse(item)

View file

@ -600,7 +600,9 @@ fun Controller.setAppBarBG(value: Float, includeTabView: Boolean = false) {
if (!isControllerVisible) return
if (floatingBar) {
(activityBinding?.cardView as? CardView)?.setCardBackgroundColor(context.getResourceColor(R.attr.colorPrimaryVariant))
if (this !is SmallToolbarInterface && activityBinding?.appBar?.useLargeToolbar == true) {
if (this !is SmallToolbarInterface && activityBinding?.appBar?.useLargeToolbar == true &&
activityBinding?.appBar?.compactSearchMode != true
) {
val colorSurface = context.getResourceColor(R.attr.colorSurface)
val color = ColorUtils.blendARGB(
colorSurface,