Skip to content

Commit

Permalink
Refactorize way how collapsingToolbarLayout is being created
Browse files Browse the repository at this point in the history
  • Loading branch information
tboba committed Aug 23, 2023
1 parent bae6092 commit 390344c
Showing 1 changed file with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.swmansion.rnscreens

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.content.res.TypedArray
import android.graphics.Color
import android.os.Bundle
Expand Down Expand Up @@ -57,6 +56,8 @@ class ScreenStackFragment : ScreenFragment {
}

fun setToolbar(toolbar: Toolbar) {
mToolbar = toolbar

if (!screen.headerType.isCollapsing) {
mAppBarLayout?.addView(toolbar)
toolbar.layoutParams = AppBarLayout.LayoutParams(
Expand All @@ -68,22 +69,11 @@ class ScreenStackFragment : ScreenFragment {
R.attr.actionBarSize.resolveAttribute(toolbar.context)
)

mCollapsingToolbarLayout?.addView(toolbar)
}

mToolbar = toolbar
propagatePropsFromToolbarToViews(toolbar)
}

private fun propagatePropsFromToolbarToViews(toolbar: Toolbar) {
mAppBarLayout?.apply {
background = toolbar.background
}
mCollapsingToolbarLayout = createCollapsingToolbarLayout()
updatePropsFromToolbarToViews(toolbar)

mCollapsingToolbarLayout?.apply {
title = toolbar.title
background = toolbar.background
contentScrim = toolbar.background
mCollapsingToolbarLayout?.addView(toolbar)
mAppBarLayout?.addView(mCollapsingToolbarLayout)
}
}

Expand Down Expand Up @@ -135,11 +125,6 @@ class ScreenStackFragment : ScreenFragment {
view?.addView(recycleView(screen))
}

if (mCollapsingToolbarLayout == null) {
val collapsingToolbarLayout = if (!screen.headerType.isCollapsing) null else createCollapsingToolbarLayout()
mCollapsingToolbarLayout = collapsingToolbarLayout
}

mAppBarLayout = context?.let { AppBarLayout(it) }?.apply {
// By default AppBarLayout will have a background color set but since we cover the whole layout
// with toolbar (that can be semi-transparent) the bar layout background color does not pay a
Expand Down Expand Up @@ -224,6 +209,11 @@ class ScreenStackFragment : ScreenFragment {
val collapsingToolbarLayout = context?.let { CollapsingToolbarLayout(it, null, toolbarStyle) }?.apply {
layoutParams = AppBarLayout.LayoutParams(AppBarLayout.LayoutParams.MATCH_PARENT, AppBarLayout.LayoutParams.MATCH_PARENT)
.apply { scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL or AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED }

mToolbar?.let {
title = it.title
background = it.background
}
}

return collapsingToolbarLayout
Expand All @@ -245,6 +235,16 @@ class ScreenStackFragment : ScreenFragment {
return nestedScrollView
}

private fun updatePropsFromToolbarToViews(toolbar: Toolbar) {
mAppBarLayout?.apply {
background = toolbar.background
}

mNestedScrollView?.apply {
background = toolbar.background
}
}

private fun shouldShowSearchBar(): Boolean {
val config = screen.headerConfig
val numberOfSubViews = config?.configSubviewsCount ?: 0
Expand Down

0 comments on commit 390344c

Please sign in to comment.