Skip to content

Commit

Permalink
chore(android): layout
Browse files Browse the repository at this point in the history
  • Loading branch information
lodev09 committed Mar 25, 2024
1 parent f45f617 commit 2c5b135
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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<T : ViewGroup>: BottomSheetBehavior<T> {

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)
)
}
}
23 changes: 23 additions & 0 deletions android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<TrueSheetView>() {
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 {
Expand Down
15 changes: 15 additions & 0 deletions android/src/main/res/layout/truesheet_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<com.lodev09.truesheet.TrueSheetView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:id="@+id/contents"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.lodev09.truesheet.TrueSheetBottomSheetBehavior"/>

</com.lodev09.truesheet.TrueSheetView>
20 changes: 12 additions & 8 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,11 +38,15 @@ export default function App() {
const sheet2 = useRef<TrueSheet>(null)
const sheet3 = useRef<TrueSheet>(null)

const scrollViewRef = useRef<ScrollView>(null)
const flatListRef = useRef<FlatList>(null)
const _scrollViewRef = useRef<ScrollView>(null)
const _flatListRef = useRef<FlatList>(null)

const presentSheet1 = (index = 0) => {
sheet1.current?.present(index)
const presentSheet1 = (_index = 0) => {
// sheet1.current?.present(index)
}

const dismissSheet1 = () => {
// sheet1.current?.dismiss()
}

return (
Expand All @@ -66,10 +70,10 @@ export default function App() {
<Button text="Present Large" onPress={() => presentSheet1(2)} />
<Button text="Present 80%" onPress={() => presentSheet1(1)} />
<Button text="Present Auto" onPress={() => presentSheet1(0)} />
<Button text="Dismis" onPress={() => sheet1.current?.dismiss()} />
<Button text="Dismis" onPress={dismissSheet1} />
</TrueSheet>

<TrueSheet
{/* <TrueSheet
ref={sheet2}
scrollRef={scrollViewRef}
onDismiss={() => console.log('Sheet 2 dismissed!')}
Expand Down Expand Up @@ -97,7 +101,7 @@ export default function App() {
indicatorStyle="black"
renderItem={({ item }) => <DemoContent text={String(item + 1)} />}
/>
</TrueSheet>
</TrueSheet> */}
</View>
)
}
Expand Down
3 changes: 0 additions & 3 deletions src/TrueSheet.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@ export class TrueSheet extends PureComponent<TrueSheetProps> {
}

render(): ReactNode {
const FooterComponent = this.props.FooterComponent

return (
<TrueSheetNativeView ref={this.ref} sizes={this.props.sizes ?? ['medium', 'large']}>
<View style={{ backgroundColor: this.props.backgroundColor ?? 'white' }}>
<View style={this.props.style}>{this.props.children}</View>
<View>{!!FooterComponent && <FooterComponent />}</View>
</View>
</TrueSheetNativeView>
)
Expand Down

0 comments on commit 2c5b135

Please sign in to comment.