Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
14 changes: 9 additions & 5 deletions android/src/main/java/com/swmansion/rnscreens/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.swmansion.rnscreens.events.SheetDetentChangedEvent
import com.swmansion.rnscreens.ext.asScreenStackFragment
import com.swmansion.rnscreens.ext.parentAsViewGroup
import com.swmansion.rnscreens.gamma.common.FragmentProviding
import com.swmansion.rnscreens.utils.pxToDp
import kotlin.math.max

@SuppressLint("ViewConstructor") // Only we construct this view, it is never inflated.
Expand Down Expand Up @@ -185,9 +186,6 @@ class Screen(
val height = b - t

dispatchShadowStateUpdate(width, height, t)

// FormSheet has no header in current model.
notifyHeaderHeightChange(t)
}
}

Expand Down Expand Up @@ -456,12 +454,18 @@ class Screen(
super.onTouchEvent(event)
}

private fun notifyHeaderHeightChange(headerHeight: Int) {
fun notifyHeaderHeightChange(headerHeight: Int) {
val screenContext = context as ReactContext
val surfaceId = UIManagerHelper.getSurfaceId(screenContext)
UIManagerHelper
.getEventDispatcherForReactTag(screenContext, id)
?.dispatchEvent(HeaderHeightChangeEvent(surfaceId, id, headerHeight))
?.dispatchEvent(
HeaderHeightChangeEvent(
surfaceId,
id,
pxToDp(screenContext, headerHeight.toFloat()).toDouble(),
),
)
}

internal fun onSheetDetentChanged(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class ScreenStackHeaderConfig(
defaultStartInsetWithNavigation
}

val headerHeightUpdateProxy = ScreenStackHeaderHeightUpdateProxy(this)

fun destroy() {
isDestroyed = true
}
Expand Down Expand Up @@ -126,6 +128,8 @@ class ScreenStackHeaderConfig(

val contentInsetEnd = toolbar.currentContentInsetEnd + toolbar.paddingEnd

headerHeightUpdateProxy.updateHeaderHeightIfNeeded()

// Note that implementation of the callee differs between architectures.
updateHeaderConfigState(
toolbar.width,
Expand Down Expand Up @@ -162,7 +166,7 @@ class ScreenStackHeaderConfig(
?.dispatchEvent(HeaderDetachedEvent(surfaceId, id))
}

private val screen: Screen?
val screen: Screen?
get() = parent as? Screen

private val screenStack: ScreenStack?
Expand Down Expand Up @@ -216,6 +220,7 @@ class ScreenStackHeaderConfig(
if (toolbar.parent != null) {
screenFragment?.removeToolbar()
}
headerHeightUpdateProxy.updateHeaderHeightIfNeeded()
return
}

Expand Down Expand Up @@ -336,6 +341,8 @@ class ScreenStackHeaderConfig(
toolbar.addView(view)
i++
}

headerHeightUpdateProxy.updateHeaderHeightIfNeeded()
}

private fun maybeUpdate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.swmansion.rnscreens

class ScreenStackHeaderHeightUpdateProxy(
val config: ScreenStackHeaderConfig,
) {
var previousHeaderHeightInPx: Int? = null

fun updateHeaderHeightIfNeeded() {
val currentHeaderHeightInPx = if (config.isHeaderHidden) 0 else config.toolbar.height

if (currentHeaderHeightInPx != previousHeaderHeightInPx) {
previousHeaderHeightInPx = currentHeaderHeightInPx
config.screen?.notifyHeaderHeightChange(currentHeaderHeightInPx)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import com.facebook.react.uimanager.events.Event
class HeaderHeightChangeEvent(
surfaceId: Int,
viewId: Int,
private val headerHeight: Int,
private val headerHeightInDp: Double,
) : Event<HeaderHeightChangeEvent>(surfaceId, viewId) {
override fun getEventName() = EVENT_NAME

// As the same header height could appear twice, use header height as a coalescing key.
override fun getCoalescingKey(): Short = headerHeight.toShort()
override fun getCoalescingKey(): Short = headerHeightInDp.toInt().toShort()

override fun getEventData(): WritableMap? =
Arguments.createMap().apply {
putDouble("headerHeight", headerHeight.toDouble())
putDouble("headerHeight", headerHeightInDp)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.swmansion.rnscreens.utils

import android.content.Context
import android.os.Build
import android.util.TypedValue

fun pxToDp(
context: Context,
px: Float,
): Float {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
return TypedValue.deriveDimension(
TypedValue.COMPLEX_UNIT_DIP,
px,
context.resources.displayMetrics,
)
} else {
val density = context.resources.displayMetrics.density
return px / density
}
}
Loading
Loading