Skip to content

Commit

Permalink
fix(android): container size change
Browse files Browse the repository at this point in the history
  • Loading branch information
lodev09 committed Dec 7, 2024
1 parent 6b2ce51 commit cdfde64
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
})
}

fun setOnSizeChangeListener(listener: RootSheetView.OnSizeChangeListener) {
rootSheetView.setOnSizeChangeListener(listener)
fun setOnSizeChangeListener(listener: (w: Int, h: Int) -> Unit) {
rootSheetView.sizeChangeListener = listener
}

/**
Expand Down
6 changes: 6 additions & 0 deletions android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.EventDispatcher
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.lodev09.truesheet.core.RootSheetView
import com.lodev09.truesheet.core.Utils
import com.lodev09.truesheet.events.ContainerSizeChangeEvent
import com.lodev09.truesheet.events.DismissEvent
import com.lodev09.truesheet.events.MountEvent
import com.lodev09.truesheet.events.PresentEvent
Expand Down Expand Up @@ -67,6 +69,10 @@ class TrueSheetView(context: Context) :

// Configure Sheet Dialog
sheetDialog.apply {
setOnSizeChangeListener { w, h ->
eventDispatcher?.dispatchEvent(ContainerSizeChangeEvent(surfaceId, id, Utils.toDIP(w), Utils.toDIP(h)))
}

// Setup listener when the dialog has been presented.
setOnShowListener {
registerKeyboardManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.annotations.ReactProp
import com.lodev09.truesheet.core.Utils
import com.lodev09.truesheet.events.ContainerSizeChangeEvent
import com.lodev09.truesheet.events.DismissEvent
import com.lodev09.truesheet.events.MountEvent
import com.lodev09.truesheet.events.PresentEvent
Expand All @@ -30,6 +31,7 @@ class TrueSheetViewManager : ViewGroupManager<TrueSheetView>() {
.put(PresentEvent.EVENT_NAME, MapBuilder.of("registrationName", "onPresent"))
.put(DismissEvent.EVENT_NAME, MapBuilder.of("registrationName", "onDismiss"))
.put(SizeChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onSizeChange"))
.put(ContainerSizeChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onContainerSizeChange"))
.build()

@ReactProp(name = "edgeToEdge")
Expand Down
28 changes: 2 additions & 26 deletions android/src/main/java/com/lodev09/truesheet/core/RootSheetView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import android.annotation.SuppressLint
import android.content.Context
import android.view.MotionEvent
import android.view.View
import com.facebook.react.bridge.GuardedRunnable
import com.facebook.react.config.ReactFeatureFlags
import com.facebook.react.uimanager.JSPointerDispatcher
import com.facebook.react.uimanager.JSTouchDispatcher
import com.facebook.react.uimanager.RootView
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerModule
import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.react.views.view.ReactViewGroup

Expand All @@ -35,14 +33,10 @@ class RootSheetView(private val context: Context?) :

private val jSTouchDispatcher = JSTouchDispatcher(this)
private var jSPointerDispatcher: JSPointerDispatcher? = null
private var sizeChangeListener: OnSizeChangeListener? = null
public var sizeChangeListener: ((w: Int, h: Int) -> Unit)? = null

var eventDispatcher: EventDispatcher? = null

interface OnSizeChangeListener {
fun onSizeChange(width: Int, height: Int)
}

init {
if (ReactFeatureFlags.dispatchPointerEvents) {
jSPointerDispatcher = JSPointerDispatcher(this)
Expand All @@ -55,30 +49,12 @@ class RootSheetView(private val context: Context?) :
viewWidth = w
viewHeight = h
updateFirstChildView()

sizeChangeListener?.onSizeChange(w, h)
}

fun setOnSizeChangeListener(listener: OnSizeChangeListener) {
sizeChangeListener = listener
}

private fun updateFirstChildView() {
if (childCount > 0) {
hasAdjustedSize = false
val viewTag = getChildAt(0).id
reactContext.runOnNativeModulesQueueThread(
object : GuardedRunnable(reactContext) {
override fun runGuarded() {
val uiManager: UIManagerModule =
reactContext
.reactApplicationContext
.getNativeModule(UIManagerModule::class.java) ?: return

uiManager.updateNodeSize(viewTag, viewWidth, viewHeight)
}
}
)
sizeChangeListener?.let { it(viewWidth, viewHeight) }
} else {
hasAdjustedSize = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.lodev09.truesheet.events

import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.events.Event

// onContainerSizeChange
class ContainerSizeChangeEvent(surfaceId: Int, viewId: Int, private val width: Float, private val height: Float) :
Event<ContainerSizeChangeEvent>(surfaceId, viewId) {
override fun getEventName() = EVENT_NAME

override fun getEventData(): WritableMap {
val data = Arguments.createMap()
data.putDouble("width", width.toDouble())
data.putDouble("height", height.toDouble())

return data
}

companion object {
const val EVENT_NAME = "containerSizeChange"
}
}
14 changes: 9 additions & 5 deletions src/TrueSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const LINKING_ERROR =
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n'

type WidthChangeEvent = NativeSyntheticEvent<{ width: number }>
type ContainerSizeChangeEvent = NativeSyntheticEvent<{ width: number; height: number }>
type SizeChangeEvent = NativeSyntheticEvent<SizeInfo>

interface TrueSheetNativeViewProps extends Omit<TrueSheetProps, 'onPresent' | 'onSizeChange'> {
Expand All @@ -31,13 +31,14 @@ interface TrueSheetNativeViewProps extends Omit<TrueSheetProps, 'onPresent' | 'o
scrollableHandle: number | null
onPresent: (event: SizeChangeEvent) => void
onSizeChange: (event: SizeChangeEvent) => void
onWidthChange: (event: WidthChangeEvent) => void
onContainerSizeChange: (event: ContainerSizeChangeEvent) => void
}

type NativeRef = Component<TrueSheetNativeViewProps> & Readonly<NativeMethods>

interface TrueSheetState {
containerWidth?: number
containerHeight?: number
contentHeight?: number
footerHeight?: number
scrollableHandle: number | null
Expand Down Expand Up @@ -70,10 +71,11 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
this.onSizeChange = this.onSizeChange.bind(this)
this.onContentLayout = this.onContentLayout.bind(this)
this.onFooterLayout = this.onFooterLayout.bind(this)
this.onWidthChange = this.onWidthChange.bind(this)
this.onContainerSizeChange = this.onContainerSizeChange.bind(this)

this.state = {
containerWidth: undefined,
containerHeight: undefined,
contentHeight: undefined,
footerHeight: undefined,
scrollableHandle: null,
Expand Down Expand Up @@ -147,9 +149,10 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
this.props.onSizeChange?.(event.nativeEvent)
}

private onWidthChange(event: WidthChangeEvent): void {
private onContainerSizeChange(event: ContainerSizeChangeEvent): void {
this.setState({
containerWidth: event.nativeEvent.width,
containerHeight: event.nativeEvent.height,
})
}

Expand Down Expand Up @@ -260,7 +263,7 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
onPresent={this.onPresent}
onDismiss={this.onDismiss}
onSizeChange={this.onSizeChange}
onWidthChange={this.onWidthChange}
onContainerSizeChange={this.onContainerSizeChange}
>
<View
collapsable={false}
Expand All @@ -273,6 +276,7 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
// Update the width on JS side.
// New Arch interop does not support updating it in native :/
width: this.state.containerWidth,
height: this.state.containerHeight,

// Remove backgroundColor if `blurTint` is set on iOS
backgroundColor: Platform.select({
Expand Down

0 comments on commit cdfde64

Please sign in to comment.