Skip to content

Commit

Permalink
WIP: Add reloadable toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
tboba committed Aug 24, 2023
1 parent cf32cbc commit b4b31b9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 24 deletions.
17 changes: 16 additions & 1 deletion android/src/main/java/com/swmansion/rnscreens/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Screen constructor(context: ReactContext?) : FabricEnabledViewGroup(contex
var isGestureEnabled = true
var screenOrientation: Int? = null
private set
var headerType = HeaderType.Small
private var mHeaderType: HeaderType = HeaderType.Small
private var mStatusBarStyle: String? = null
private var mStatusBarHidden: Boolean? = null
private var mStatusBarTranslucent: Boolean? = null
Expand Down Expand Up @@ -160,6 +160,21 @@ class Screen constructor(context: ReactContext?) : FabricEnabledViewGroup(contex
this.headerConfig?.toolbar?.importantForAccessibility = mode
}

var headerType: HeaderType
get() = mHeaderType
set(headerType) {
val previousHeaderType = mHeaderType
mHeaderType = headerType

if (previousHeaderType != headerType) {
(fragment as ScreenStackFragment).reloadToolbar()
}

if (previousHeaderType.isCollapsing != headerType.isCollapsing) {
(fragment as ScreenStackFragment).rebuildViewWithToolbar()
}
}

var statusBarStyle: String?
get() = mStatusBarStyle
set(statusBarStyle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,39 @@ class ScreenStackFragment : ScreenFragment {
fun removeToolbar() {
isToolbarHidden = true
mAppBarLayout?.let {
// For small headers, removing `toolbar` is sufficient.
// For small headers, removing `toolbar` from its parent is sufficient.
// Thus, we can simply remove toolbar from AppBarLayout.
mToolbar?.let { toolbar ->
if (toolbar.parent === it) {
it.removeView(toolbar)
}
val toolbarParent = mToolbar?.parent
if (toolbarParent is ViewGroup) {
toolbarParent.removeView(mToolbar)
}
// mToolbar?.let { toolbar ->
// if (toolbar.parent === it) {
// it.removeView(toolbar)
// }
// }

val ctlParent = mCollapsingToolbarLayout?.parent
if (ctlParent is ViewGroup) {
ctlParent.removeView(mCollapsingToolbarLayout)
}

// 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)
}
}
// mCollapsingToolbarLayout?.let { collapsingToolbar ->
// mToolbar?.let { toolbar ->
// if (toolbar.parent === collapsingToolbar) {
// toolbar.removeView(collapsingToolbar)
// }
// }
//
// if (collapsingToolbar.parent === it) {
// it.removeView(collapsingToolbar)
// }
// }

it.removeAllViews()

// As AppBarLayout may have dimensions of expanded medium / large header,
// We need to change its layout params to `WRAP_CONTENT`.
Expand All @@ -78,7 +89,7 @@ class ScreenStackFragment : ScreenFragment {
)
}

mToolbar = null
// mToolbar = null
mCollapsingToolbarLayout = null
}

Expand All @@ -102,13 +113,20 @@ 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)
)
}

// 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)
)
}

fun reloadToolbar() {
val previousToolbar = mToolbar
removeToolbar()
previousToolbar?.let { setToolbar(it) }
println(getViewTree(mAppBarLayout!!))
}

fun setToolbarShadowHidden(hidden: Boolean) {
Expand Down Expand Up @@ -195,6 +213,25 @@ class ScreenStackFragment : ScreenFragment {
return view
}

fun rebuildViewWithToolbar() {
if (mNestedScrollView == null) {
val parent = screen.parent
if (parent is ViewGroup) {
val nestedScrollView = createNestedScrollViewFromScreen()
mNestedScrollView = nestedScrollView
parent.addView(nestedScrollView)
}
} else {
mNestedScrollView?.removeView(screen)
val nsvParent = mNestedScrollView?.parent
if (nsvParent is ViewGroup) {
nsvParent.removeView(mNestedScrollView)
nsvParent.addView(screen)
}
mNestedScrollView = null
}
}

private fun getViewTree(root: ViewGroup): String {
fun getViewDesc(v: View): String {
val res = v.resources
Expand Down Expand Up @@ -244,6 +281,7 @@ class ScreenStackFragment : ScreenFragment {
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 }

// Apply the same styling from toolbar
mToolbar?.let {
title = it.title
background = it.background
Expand Down

0 comments on commit b4b31b9

Please sign in to comment.