From fef2b207314c88903d30a0c8cf3ad0c5f0dbdf5c Mon Sep 17 00:00:00 2001 From: lodev09 Date: Wed, 27 Mar 2024 09:41:21 +0800 Subject: [PATCH] feat: implement auto height --- .../com/lodev09/truesheet/TrueSheetView.kt | 44 +++++++++++++++++-- .../src/main/res/layout/truesheet_layout.xml | 15 ------- 2 files changed, 40 insertions(+), 19 deletions(-) delete mode 100644 android/src/main/res/layout/truesheet_layout.xml diff --git a/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt b/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt index e1bd1ff..099622b 100644 --- a/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt +++ b/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt @@ -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 @@ -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 { @@ -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() } diff --git a/android/src/main/res/layout/truesheet_layout.xml b/android/src/main/res/layout/truesheet_layout.xml deleted file mode 100644 index 1216573..0000000 --- a/android/src/main/res/layout/truesheet_layout.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - -