diff --git a/android/src/main/java/com/lodev09/truesheet/TrueSheetBottomSheetBehavior.kt b/android/src/main/java/com/lodev09/truesheet/TrueSheetBottomSheetBehavior.kt new file mode 100644 index 0000000..d612542 --- /dev/null +++ b/android/src/main/java/com/lodev09/truesheet/TrueSheetBottomSheetBehavior.kt @@ -0,0 +1,44 @@ +package com.lodev09.truesheet + +import android.content.Context +import android.util.AttributeSet +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.ScrollView +import androidx.coordinatorlayout.widget.CoordinatorLayout +import com.google.android.material.bottomsheet.BottomSheetBehavior + +class TrueSheetBottomSheetBehavior: BottomSheetBehavior { + + constructor() : super() + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) + + override fun onInterceptTouchEvent(parent: CoordinatorLayout, child: T, event: MotionEvent): Boolean { + val isDownEvent = (event.actionMasked == MotionEvent.ACTION_DOWN) + val expanded = (state == BottomSheetBehavior.STATE_EXPANDED) + + if(isDownEvent && expanded){ + val content = child.getChildAt(0) as ViewGroup + for(i in 0 until content.childCount){ + val contentChild = content.getChildAt(i) + val scrolled = (contentChild is ScrollView && contentChild.scrollY > 0) + if(!scrolled) continue + + val inside = isMotionEventInsideView(contentChild, event) + if(inside) return false + } + } + + return super.onInterceptTouchEvent(parent, child, event) + } + + private fun isMotionEventInsideView(view: View, event: MotionEvent): Boolean { + val coords = intArrayOf(0, 0) + view.getLocationInWindow(coords) + return ( + event.rawX >= coords[0] && event.rawX <= (coords[0] + view.width) && + event.rawY >= coords[1] && event.rawY <= (coords[1] + view.height) + ) + } +} diff --git a/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt b/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt index 1c8c7c4..6908e34 100644 --- a/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt +++ b/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt @@ -1,9 +1,32 @@ package com.lodev09.truesheet import android.content.Context +import android.view.View +import android.widget.RelativeLayout import com.google.android.material.bottomsheet.BottomSheetBehavior import androidx.coordinatorlayout.widget.CoordinatorLayout +import com.google.android.material.bottomsheet.BottomSheetDialog class TrueSheetView(context: Context): CoordinatorLayout(context) { + private lateinit var contents: RelativeLayout + private set + private lateinit var behavior: BottomSheetBehavior<*> + private set + + override fun onViewAdded(child: View?) { + super.onViewAdded(child) + + contents = child as RelativeLayout + + behavior = BottomSheetBehavior.from(contents).apply { + // virtually disables 'third' breakpoint + halfExpandedRatio = 0.9999999f + isFitToContents = true + isHideable = true + // default to no collapsed state + skipCollapsed = true + setPeekHeight(Integer.MAX_VALUE) + } + } } diff --git a/android/src/main/java/com/lodev09/truesheet/TrueSheetViewManager.kt b/android/src/main/java/com/lodev09/truesheet/TrueSheetViewManager.kt index 7cbabc9..5061f29 100644 --- a/android/src/main/java/com/lodev09/truesheet/TrueSheetViewManager.kt +++ b/android/src/main/java/com/lodev09/truesheet/TrueSheetViewManager.kt @@ -1,13 +1,16 @@ package com.lodev09.truesheet +import android.annotation.SuppressLint +import android.view.LayoutInflater import com.facebook.react.uimanager.ThemedReactContext import com.facebook.react.uimanager.ViewGroupManager class TrueSheetViewManager : ViewGroupManager() { override fun getName() = NAME + @SuppressLint("InflateParams") override fun createViewInstance(reactContext: ThemedReactContext): TrueSheetView { - return TrueSheetView(reactContext) + return LayoutInflater.from(reactContext).inflate(R.layout.truesheet_layout, null) as TrueSheetView } companion object { diff --git a/android/src/main/res/layout/truesheet_layout.xml b/android/src/main/res/layout/truesheet_layout.xml new file mode 100644 index 0000000..8209418 --- /dev/null +++ b/android/src/main/res/layout/truesheet_layout.xml @@ -0,0 +1,15 @@ + + + + + + diff --git a/example/src/App.tsx b/example/src/App.tsx index 1e85072..dd84f9b 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -13,7 +13,7 @@ import { } from 'react-native' import { TrueSheet } from '@lodev09/react-native-true-sheet' -import { times } from './utils' +// import { times } from './utils' const CONTENT_PADDING = 16 const FOOTER_HEIGHT = 80 @@ -38,11 +38,15 @@ export default function App() { const sheet2 = useRef(null) const sheet3 = useRef(null) - const scrollViewRef = useRef(null) - const flatListRef = useRef(null) + const _scrollViewRef = useRef(null) + const _flatListRef = useRef(null) - const presentSheet1 = (index = 0) => { - sheet1.current?.present(index) + const presentSheet1 = (_index = 0) => { + // sheet1.current?.present(index) + } + + const dismissSheet1 = () => { + // sheet1.current?.dismiss() } return ( @@ -66,10 +70,10 @@ export default function App() {