Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Android): add notifying for header height change, fix header height values #2075

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions android/src/main/java/com/swmansion/rnscreens/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ class Screen(context: ReactContext?) : FabricEnabledViewGroup(context) {
val width = r - l
val height = b - t

val headerHeight = calculateHeaderHeight().first
val headerHeight = calculateHeaderHeight()
val totalHeight = headerHeight.first + headerHeight.second // action bar height + status bar height
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
updateScreenSizeFabric(width, height, headerHeight)
updateScreenSizeFabric(width, height, totalHeight)
} else {
updateScreenSizePaper(width, height)
}

notifyHeaderHeightChange(totalHeight)
}
}

Expand Down Expand Up @@ -236,18 +239,24 @@ class Screen(context: ReactContext?) : FabricEnabledViewGroup(context) {
// Check if it's possible to get an attribute from theme context and assign a value from it.
// Otherwise, the default value will be returned.
val actionBarHeight = TypedValue.complexToDimensionPixelSize(actionBarTv.data, resources.displayMetrics)
.takeIf { resolvedActionBarSize && headerConfig?.isHeaderHidden != true }
.takeIf { resolvedActionBarSize && headerConfig?.isHeaderHidden != true && headerConfig?.isHeaderTranslucent != true }
?.let { PixelUtil.toDIPFromPixel(it.toFloat()).toDouble() } ?: 0.0

val statusBarHeight = context.resources.getIdentifier("status_bar_height", "dimen", "android")
.takeIf { it > 0 && isStatusBarHidden != true }
// Count only status bar when action bar is visible and status bar is not hidden
.takeIf { it > 0 && isStatusBarHidden != true && actionBarHeight > 0 }
?.let { (context.resources::getDimensionPixelSize)(it) }
?.let { PixelUtil.toDIPFromPixel(it.toFloat()).toDouble() }
?: 0.0

return actionBarHeight to statusBarHeight
}

private fun notifyHeaderHeightChange(headerHeight: Double) {
UIManagerHelper.getEventDispatcherForReactTag(context as ReactContext, id)
?.dispatchEvent(HeaderHeightChangeEvent(id, headerHeight))
}

enum class StackPresentation {
PUSH, MODAL, TRANSPARENT_MODAL
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ScreenStackHeaderConfig(context: Context) : ViewGroup(context) {
private val configSubviews = ArrayList<ScreenStackHeaderSubview>(3)
val toolbar: CustomToolbar
var isHeaderHidden = false // named this way to avoid conflict with platform's isHidden
var isHeaderTranslucent = false // named this way to avoid conflict with platform's isTranslucent
private var headerTopInset: Int? = null
private var title: String? = null
private var titleColor = 0
Expand All @@ -38,7 +39,6 @@ class ScreenStackHeaderConfig(context: Context) : ViewGroup(context) {
private var isDestroyed = false
private var backButtonInCustomView = false
private var isTopInsetEnabled = true
private var isTranslucent = false
private var tintColor = 0
private var isAttachedToWindow = false
private val defaultStartInset: Int
Expand Down Expand Up @@ -195,7 +195,7 @@ class ScreenStackHeaderConfig(context: Context) : ViewGroup(context) {
screenFragment?.setToolbarShadowHidden(isShadowHidden)

// translucent
screenFragment?.setToolbarTranslucent(isTranslucent)
screenFragment?.setToolbarTranslucent(isHeaderTranslucent)

// title
actionBar.title = title
Expand Down Expand Up @@ -363,7 +363,7 @@ class ScreenStackHeaderConfig(context: Context) : ViewGroup(context) {
}

fun setTranslucent(translucent: Boolean) {
isTranslucent = translucent
isHeaderTranslucent = translucent
}

fun setBackButtonInCustomView(backButtonInCustomView: Boolean) {
Expand Down
Loading