Skip to content

Commit

Permalink
refactor(android): cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
lodev09 committed Feb 24, 2025
1 parent c87a6ef commit 2b40742
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 45 deletions.
29 changes: 20 additions & 9 deletions android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
BottomSheetDialog(reactContext) {

private var keyboardManager = KeyboardManager(reactContext)
private var sheetView: ViewGroup
private var windowAnimation: Int = 0

// First child of the rootSheetView
private val containerView: ViewGroup
get() = rootSheetView.getChildAt(0) as ViewGroup

private val sheetContainerView: ViewGroup
get() = rootSheetView.parent as ViewGroup

/**
* Specify whether the sheet background is dimmed.
* Set to `false` to allow interaction with the background components.
Expand Down Expand Up @@ -62,17 +68,22 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val

var cornerRadius: Float = 0f
var backgroundColor: Int = Color.WHITE
var footerView: ViewGroup? = null

// 1st child is the content view
val contentView: ViewGroup
get() = containerView.getChildAt(0) as ViewGroup

// 2nd child is the footer view
val footerView: ViewGroup
get() = containerView.getChildAt(1) as ViewGroup

var sizes: Array<Any> = arrayOf("medium", "large")

init {
setContentView(rootSheetView)

sheetView = rootSheetView.parent as ViewGroup

sheetView.setBackgroundColor(backgroundColor)
sheetView.clipToOutline = true
sheetContainerView.setBackgroundColor(backgroundColor)
sheetContainerView.clipToOutline = true

// Setup window params to adjust layout based on Keyboard state
window?.apply {
Expand Down Expand Up @@ -120,7 +131,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val

// Use current background color
background.paint.color = backgroundColor
sheetView.background = background
sheetContainerView.background = background
}

/**
Expand Down Expand Up @@ -185,8 +196,8 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
}

fun positionFooter() {
footerView?.apply {
y = (maxScreenHeight - sheetView.top - footerHeight).toFloat()
footerView.apply {
y = (maxScreenHeight - sheetContainerView.top - footerHeight).toFloat()
}
}

Expand Down
58 changes: 31 additions & 27 deletions android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class TrueSheetView(context: Context) :
val data = Arguments.createMap()
data.putDouble("width", Utils.toDIP(w.toFloat()).toDouble())
data.putDouble("height", Utils.toDIP(h.toFloat()).toDouble())

dispatchEvent(TrueSheetEvent.CONTAINER_SIZE_CHANGE, data)
}

Expand All @@ -97,7 +98,7 @@ class TrueSheetView(context: Context) :
}

// Dispatch onPresent event
dispatchEvent(TrueSheetEvent.PRESENT, sizeInfoData(sheetDialog.getSizeInfoForIndex(currentSizeIndex)))
dispatchEvent(TrueSheetEvent.PRESENT, sizeInfoData(getSizeInfoForIndex(currentSizeIndex)))
}

// Setup listener when the dialog has been dismissed.
Expand All @@ -118,7 +119,7 @@ class TrueSheetView(context: Context) :
behavior.addBottomSheetCallback(
object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(sheetView: View, slideOffset: Float) {
when (sheetDialog.behavior.state) {
when (behavior.state) {
// For consistency with IOS, we consider SETTLING as dragging change.
BottomSheetBehavior.STATE_DRAGGING,
BottomSheetBehavior.STATE_SETTLING -> handleDragChange(sheetView)
Expand Down Expand Up @@ -172,39 +173,35 @@ class TrueSheetView(context: Context) :
// Do nothing as we are laid out by UIManager
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()

// Initialize content
UiThreadUtil.runOnUiThread {
setContentHeight(sheetDialog.contentView.height)
setFooterHeight(sheetDialog.footerView.height)

if (initialIndex >= 0) {
currentSizeIndex = initialIndex
sheetDialog.present(initialIndex, initialIndexAnimated)
}

// Dispatch onMount event
dispatchEvent(TrueSheetEvent.MOUNT)
}
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
sheetDialog.dismiss()
}

override fun addView(child: View, index: Int) {
UiThreadUtil.assertOnUiThread()
rootSheetView.addView(child, index)

// Hide this host view
visibility = GONE

(child as ViewGroup).let {
// rootView's first child is the Container View
rootSheetView.addView(it, index)

// Initialize content
UiThreadUtil.runOnUiThread {
// 1st child is the content view
val contentView = it.getChildAt(0) as ViewGroup?
setContentHeight(contentView?.height ?: 0)

// 2nd child is the footer view
val footerView = it.getChildAt(1) as ViewGroup?
sheetDialog.footerView = footerView
setFooterHeight(footerView?.height ?: 0)

if (initialIndex >= 0) {
currentSizeIndex = initialIndex
sheetDialog.present(initialIndex, initialIndexAnimated)
}

// Dispatch onMount event
dispatchEvent(TrueSheetEvent.MOUNT)
}
}
}

override fun getChildCount(): Int {
Expand All @@ -216,10 +213,13 @@ class TrueSheetView(context: Context) :
override fun getChildAt(index: Int): View = rootSheetView.getChildAt(index)

override fun removeView(child: View) {
UiThreadUtil.assertOnUiThread()
rootSheetView.removeView(child)
}

override fun removeViewAt(index: Int) {
UiThreadUtil.assertOnUiThread()

val child = getChildAt(index)
rootSheetView.removeView(child)
}
Expand Down Expand Up @@ -394,6 +394,8 @@ class TrueSheetView(context: Context) :
* Present the sheet at given size index.
*/
fun present(sizeIndex: Int, promiseCallback: () -> Unit) {
UiThreadUtil.assertOnUiThread()

currentSizeIndex = sizeIndex

if (sheetDialog.isShowing) {
Expand All @@ -414,6 +416,8 @@ class TrueSheetView(context: Context) :
* Dismisses the sheet.
*/
fun dismiss(promiseCallback: () -> Unit) {
UiThreadUtil.assertOnUiThread()

dismissPromise = promiseCallback
sheetDialog.dismiss()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class RootSheetView(private val context: Context?) :

var eventDispatcher: EventDispatcher? = null

private val reactContext: ThemedReactContext
get() = context as ThemedReactContext

init {
if (ReactFeatureFlags.dispatchPointerEvents) {
jSPointerDispatcher = JSPointerDispatcher(this)
}
}

private val reactContext: ThemedReactContext
get() = context as ThemedReactContext

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)

Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"react": "18.3.1",
"react-native": "0.77.1",
"react-native-edge-to-edge": "^1.4.3",
"react-native-gesture-handler": "^2.23.1",
"react-native-gesture-handler": "^2.24.0",
"react-native-maps": "^1.20.1",
"react-native-reanimated": "^3.16.7",
"react-native-safe-area-context": "^5.2.0",
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17327,17 +17327,17 @@ __metadata:
languageName: node
linkType: hard

"react-native-gesture-handler@npm:^2.23.1":
version: 2.23.1
resolution: "react-native-gesture-handler@npm:2.23.1"
"react-native-gesture-handler@npm:^2.24.0":
version: 2.24.0
resolution: "react-native-gesture-handler@npm:2.24.0"
dependencies:
"@egjs/hammerjs": ^2.0.17
hoist-non-react-statics: ^3.3.0
invariant: ^2.2.4
peerDependencies:
react: "*"
react-native: "*"
checksum: f7afb2fdd370fdac52ef1ba37495ef598569df666a25f9a5f5825436861c90933face6b7ba8cb551fd90ba5438a1754495cf5690d9b663d4ab7a24c75d5abfc1
checksum: 65abaeef68180fee2811d01d88ff50c231a91faca05279222fcaaa55349e758b68a7d6a9ac3eddfb1887f6b4c4790ac195b99989f1ad8a2a1f3f3bdff3ba0a76
languageName: node
linkType: hard

Expand Down Expand Up @@ -17422,7 +17422,7 @@ __metadata:
react-native: 0.77.1
react-native-builder-bob: ^0.37.0
react-native-edge-to-edge: ^1.4.3
react-native-gesture-handler: ^2.23.1
react-native-gesture-handler: ^2.24.0
react-native-maps: ^1.20.1
react-native-reanimated: ^3.16.7
react-native-safe-area-context: ^5.2.0
Expand Down

0 comments on commit 2b40742

Please sign in to comment.