Skip to content

Commit

Permalink
feat: implement auto height
Browse files Browse the repository at this point in the history
  • Loading branch information
lodev09 committed Mar 27, 2024
1 parent 9510421 commit fef2b20
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
44 changes: 40 additions & 4 deletions android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.lodev09.truesheet

import android.content.Context
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.ViewStructure
import android.view.accessibility.AccessibilityEvent
import android.widget.FrameLayout
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.uimanager.PixelUtil
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.EventDispatcher
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog

class TrueSheetView(context: Context) : ViewGroup(context), LifecycleEventListener {
private val sheetRootView: TrueSheetRootViewGroup?
private var sheetDialog: BottomSheetDialog?

private lateinit var sheetBehavior: BottomSheetBehavior<*>

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

Expand Down Expand Up @@ -43,10 +50,17 @@ class TrueSheetView(context: Context) : ViewGroup(context), LifecycleEventListen

sheetRootView?.addView(child, index)

val frameLayout = FrameLayout(context)
frameLayout.addView(sheetRootView)

sheetDialog?.setContentView(frameLayout)
UiThreadUtil.runOnUiThread {
try {
val manager = UIManagerHelper.getUIManagerForReactTag(reactContext, child.id)
val view = manager?.resolveView(child.id)
if (view != null) {
setupSheetDialog(view.height)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}

override fun getChildCount(): Int {
Expand Down Expand Up @@ -101,6 +115,28 @@ class TrueSheetView(context: Context) : ViewGroup(context), LifecycleEventListen
onDropInstance()
}

private fun setupSheetDialog(height: Int) {
val frameLayout = FrameLayout(context)
frameLayout.addView(sheetRootView)

sheetDialog?.setContentView(frameLayout)

sheetBehavior = BottomSheetBehavior.from(frameLayout.parent as View).apply {
addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit
override fun onStateChanged(bottomSheet: View, newState: Int) {
// TODO
}
})

// virtually disables 'third' breakpoint
isFitToContents = true
isHideable = true
// skipCollapsed = true
peekHeight = height
}
}

fun present() {
sheetDialog?.show()
}
Expand Down
15 changes: 0 additions & 15 deletions android/src/main/res/layout/truesheet_layout.xml

This file was deleted.

0 comments on commit fef2b20

Please sign in to comment.