Skip to content

Commit 56e6c11

Browse files
committed
fix: android mods
1 parent 22e4879 commit 56e6c11

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
2525

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

28-
var sheetView: ViewGroup
28+
private var sheetView: ViewGroup
2929

3030
init {
3131
setContentView(rootSheetView)
@@ -39,6 +39,9 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
3939
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
4040
)
4141
}
42+
43+
// Update the usable sheet height
44+
maxScreenHeight = Utils.screenHeight(reactContext)
4245
}
4346

4447
fun show(sizeIndex: Int) {
@@ -52,6 +55,12 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
5255
}
5356
}
5457

58+
fun positionFooter() {
59+
footerView?.apply {
60+
y = (maxScreenHeight - sheetView.top - height).toFloat()
61+
}
62+
}
63+
5564
/**
5665
* Set the state based on the given size index.
5766
*/
@@ -102,7 +111,10 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
102111
else -> (maxScreenHeight * 0.5).toInt()
103112
}
104113

105-
return minOf(height, maxSheetHeight ?: maxScreenHeight)
114+
return when (maxSheetHeight) {
115+
null -> height
116+
else -> minOf(height, maxSheetHeight ?: maxScreenHeight)
117+
}
106118
}
107119

108120
/**
@@ -144,9 +156,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
144156
else -> Utils.screenHeight(reactContext)
145157
}
146158

147-
footerView?.apply {
148-
y = (maxScreenHeight - (sheetView.top ?: 0) - height).toFloat()
149-
}
159+
positionFooter()
150160
}
151161
})
152162
}
@@ -162,9 +172,6 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
162172
* Configure the sheet based on size preferences.
163173
*/
164174
fun configure() {
165-
// Update the usable sheet height
166-
maxScreenHeight = Utils.screenHeight(reactContext)
167-
168175
var contentHeight = 0
169176

170177
contentView?.let { contentHeight = it.height }
@@ -181,20 +188,20 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
181188
when (sizes.size) {
182189
1 -> {
183190
maxHeight = getSizeHeight(sizes[0], contentHeight)
184-
peekHeight = maxHeight
191+
setPeekHeight(maxHeight, isShowing)
185192
skipCollapsed = true
186193
}
187194

188195
2 -> {
189-
peekHeight = getSizeHeight(sizes[0], contentHeight)
196+
setPeekHeight(getSizeHeight(sizes[0], contentHeight), isShowing)
190197
maxHeight = getSizeHeight(sizes[1], contentHeight)
191198
}
192199

193200
3 -> {
194201
// Enables half expanded
195202
isFitToContents = false
196203

197-
peekHeight = getSizeHeight(sizes[0], contentHeight)
204+
setPeekHeight(getSizeHeight(sizes[0], contentHeight), isShowing)
198205
halfExpandedRatio = getSizeHeight(sizes[1], contentHeight).toFloat() / maxScreenHeight.toFloat()
199206
maxHeight = getSizeHeight(sizes[2], contentHeight)
200207
}

android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.lodev09.truesheet
22

33
import android.content.Context
4-
import android.util.Log
54
import android.view.View
65
import android.view.ViewGroup
76
import android.view.ViewStructure
@@ -79,10 +78,8 @@ class TrueSheetView(context: Context) :
7978
registerKeyboardManager()
8079

8180
// Initialize footer y
82-
footerView?.apply {
83-
UiThreadUtil.runOnUiThread {
84-
y = (sheetDialog.maxScreenHeight - sheetView.top - height).toFloat()
85-
}
81+
UiThreadUtil.runOnUiThread {
82+
positionFooter()
8683
}
8784

8885
presentPromise?.let { promise ->
@@ -96,8 +93,8 @@ class TrueSheetView(context: Context) :
9693

9794
// Setup listener when the dialog has been dismissed.
9895
setOnDismissListener {
99-
Log.d(TAG, "dismissed")
10096
unregisterKeyboardManager()
97+
10198
dismissPromise?.let { promise ->
10299
promise()
103100
dismissPromise = null
@@ -221,9 +218,18 @@ class TrueSheetView(context: Context) :
221218
sheetDialog.dismiss()
222219
}
223220

221+
private fun configureIfPresented() {
222+
if (sheetDialog.isShowing) {
223+
UiThreadUtil.runOnUiThread {
224+
sheetDialog.configure()
225+
sheetDialog.positionFooter()
226+
}
227+
}
228+
}
229+
224230
fun setMaxHeight(height: Int) {
225231
sheetDialog.maxSheetHeight = height
226-
sheetDialog.configure()
232+
configureIfPresented()
227233
}
228234

229235
fun setDismissible(dismissible: Boolean) {
@@ -233,7 +239,7 @@ class TrueSheetView(context: Context) :
233239

234240
fun setSizes(newSizes: Array<Any>) {
235241
sheetDialog.sizes = newSizes
236-
sheetDialog.configure()
242+
configureIfPresented()
237243
}
238244

239245
/**

example/src/sheets/PromptSheet.tsx

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import React, { forwardRef, useRef, type Ref, useImperativeHandle } from 'react'
1+
import React, {
2+
forwardRef,
3+
useRef,
4+
type Ref,
5+
useImperativeHandle,
6+
useState,
7+
useEffect,
8+
} from 'react'
29
import { TextInput, View, type TextStyle, type ViewStyle } from 'react-native'
3-
import { TrueSheet, type TrueSheetProps } from '@lodev09/react-native-true-sheet'
10+
import { TrueSheet, type SheetSize, type TrueSheetProps } from '@lodev09/react-native-true-sheet'
411

512
import {
613
BORDER_RADIUS,
714
DARK,
815
DARK_BLUE,
16+
FOOTER_HEIGHT,
917
GRABBER_COLOR,
1018
GRAY,
1119
INPUT_HEIGHT,
@@ -20,35 +28,58 @@ interface PromptSheetProps extends TrueSheetProps {}
2028
export const PromptSheet = forwardRef((props: PromptSheetProps, ref: Ref<TrueSheet>) => {
2129
const sheetRef = useRef<TrueSheet>(null)
2230

31+
const [contentHeight, setContentHeight] = useState<number>()
32+
const [isSubmitted, setIsSubmitted] = useState(false)
33+
const [size, setSize] = useState<SheetSize>('auto')
34+
35+
const handleDismiss = () => {
36+
setIsSubmitted(false)
37+
setSize('auto')
38+
console.log('Sheet prompt dismissed!')
39+
}
40+
2341
const dismiss = async () => {
2442
await sheetRef.current?.dismiss()
2543
console.log('Sheet prompt dismiss asynced')
2644
}
2745

46+
const submit = async () => {
47+
setIsSubmitted(true)
48+
}
49+
2850
useImperativeHandle<TrueSheet | null, TrueSheet | null>(ref, () => sheetRef.current)
2951

52+
useEffect(() => {
53+
if (isSubmitted && contentHeight) {
54+
setSize(contentHeight + FOOTER_HEIGHT)
55+
}
56+
}, [isSubmitted, contentHeight])
57+
3058
return (
3159
<TrueSheet
3260
ref={sheetRef}
3361
name="prompt-sheet"
34-
sizes={['auto', '80%']}
35-
dismissible={false}
62+
sizes={[size, '80%']}
3663
contentContainerStyle={$content}
3764
blurTint="dark"
3865
backgroundColor={DARK}
3966
cornerRadius={12}
4067
grabberProps={{ color: GRABBER_COLOR }}
41-
onDismiss={() => console.log('Sheet prompt dismissed!')}
68+
onDismiss={handleDismiss}
4269
onPresent={({ index, value }) =>
4370
console.log(`Sheet prompt presented with size of ${value} at index: ${index}`)
4471
}
4572
onSizeChange={({ index, value }) => console.log(`Resized to:`, value, 'at index:', index)}
4673
FooterComponent={<Footer />}
4774
{...props}
4875
>
49-
<DemoContent color={DARK_BLUE} text={random(RANDOM_TEXTS)} />
50-
<Input />
51-
<Button text="Dismis" onPress={dismiss} />
76+
<View onLayout={(e) => isSubmitted && setContentHeight(e.nativeEvent.layout.height)}>
77+
<DemoContent color={DARK_BLUE} text={random(RANDOM_TEXTS)} />
78+
<Input />
79+
{isSubmitted && <Input />}
80+
<Button text="Submit" onPress={submit} />
81+
<Button text="Dismis" onPress={dismiss} />
82+
</View>
5283
</TrueSheet>
5384
)
5485
})
@@ -73,7 +104,7 @@ const $inputContainer: ViewStyle = {
73104
height: INPUT_HEIGHT,
74105
borderRadius: BORDER_RADIUS,
75106
justifyContent: 'center',
76-
marginBottom: SPACING * 2,
107+
marginBottom: SPACING,
77108
}
78109

79110
const $input: TextStyle = {

0 commit comments

Comments
 (0)