Skip to content

Commit

Permalink
Fix not working headerShown prop for medium / large headers
Browse files Browse the repository at this point in the history
  • Loading branch information
tboba committed Aug 23, 2023
1 parent 390344c commit cf32cbc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ScreenStackFragment : ScreenFragment {
private var mNestedScrollView: NestedScrollView? = null
private var mShadowHidden = false
private var mIsTranslucent = false
private var isToolbarHidden = false

var searchView: CustomSearchView? = null
var onSearchViewCreate: ((searchView: CustomSearchView) -> Unit)? = null
Expand All @@ -45,18 +46,45 @@ class ScreenStackFragment : ScreenFragment {
}

fun removeToolbar() {
isToolbarHidden = true
mAppBarLayout?.let {
// For small headers, removing `toolbar` is sufficient.
// Thus, we can simply remove toolbar from AppBarLayout.
mToolbar?.let { toolbar ->
if (toolbar.parent === it) {
it.removeView(toolbar)
}
}

// For medium and large headers, we need to check if toolbar's parent is
// collapsingToolbarLayout and if collapsingToolbarLayout's parent is AppBarLayout.
// we're removing those views from their parents here.
mCollapsingToolbarLayout?.let { collapsingToolbar ->
mToolbar?.let { toolbar ->
if (toolbar.parent === collapsingToolbar) {
toolbar.removeView(collapsingToolbar)
}
}

if (collapsingToolbar.parent === it) {
it.removeView(collapsingToolbar)
}
}

// As AppBarLayout may have dimensions of expanded medium / large header,
// We need to change its layout params to `WRAP_CONTENT`.
it.layoutParams = CoordinatorLayout.LayoutParams(
CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT
)
}

mToolbar = null
mCollapsingToolbarLayout = null
}

fun setToolbar(toolbar: Toolbar) {
mToolbar = toolbar
isToolbarHidden = false

if (!screen.headerType.isCollapsing) {
mAppBarLayout?.addView(toolbar)
Expand All @@ -74,6 +102,12 @@ class ScreenStackFragment : ScreenFragment {

mCollapsingToolbarLayout?.addView(toolbar)
mAppBarLayout?.addView(mCollapsingToolbarLayout)

// As `setToolbar` may be called after changing header's visibility,
// we need to apply correction to layoutParams with proper dimensions.
mAppBarLayout?.layoutParams = CoordinatorLayout.LayoutParams(
CoordinatorLayout.LayoutParams.MATCH_PARENT, getHeightOfToolbar(toolbar.context)
)
}
}

Expand Down Expand Up @@ -298,10 +332,16 @@ class ScreenStackFragment : ScreenFragment {
container.dismiss(this)
}

private fun getHeightOfToolbar(context: Context) = when (screen.headerType) {
HeaderType.Medium -> R.attr.collapsingToolbarLayoutMediumSize.resolveAttribute(context)
HeaderType.Large -> R.attr.collapsingToolbarLayoutLargeSize.resolveAttribute(context)
else -> CoordinatorLayout.LayoutParams.WRAP_CONTENT
private fun getHeightOfToolbar(context: Context): Int {
if (isToolbarHidden) {
return CoordinatorLayout.LayoutParams.WRAP_CONTENT
}

return when (screen.headerType) {
HeaderType.Medium -> R.attr.collapsingToolbarLayoutMediumSize.resolveAttribute(context)
HeaderType.Large -> R.attr.collapsingToolbarLayoutLargeSize.resolveAttribute(context)
else -> CoordinatorLayout.LayoutParams.WRAP_CONTENT
}
}

private fun Int.resolveAttribute(context: Context): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ class ScreenStackHeaderConfig(context: Context) : ViewGroup(context) {
}

if (mIsHidden) {
if (toolbar.parent != null) {
screenFragment?.removeToolbar()
}
screenFragment?.removeToolbar()
return
}

Expand Down

0 comments on commit cf32cbc

Please sign in to comment.