Skip to content

Commit 3ecf481

Browse files
committed
feat(android): implement initialIndex & onLoad
1 parent 414b6d7 commit 3ecf481

File tree

7 files changed

+98
-21
lines changed

7 files changed

+98
-21
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,12 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
263263
isFitToContents = false
264264

265265
setPeekHeight(getSizeHeight(sizes[0]), isShowing)
266-
halfExpandedRatio = getSizeHeight(sizes[1]).toFloat() / maxScreenHeight.toFloat()
266+
267+
val size2 = getSizeHeight(sizes[1])
268+
if (size2 < maxScreenHeight) {
269+
halfExpandedRatio = size2.toFloat() / maxScreenHeight.toFloat()
270+
}
271+
267272
maxHeight = getSizeHeight(sizes[2])
268273
}
269274
}

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.facebook.react.uimanager.UIManagerHelper
1212
import com.facebook.react.uimanager.events.EventDispatcher
1313
import com.google.android.material.bottomsheet.BottomSheetBehavior
1414
import com.lodev09.truesheet.core.DismissEvent
15+
import com.lodev09.truesheet.core.LoadEvent
1516
import com.lodev09.truesheet.core.PresentEvent
1617
import com.lodev09.truesheet.core.RootSheetView
1718
import com.lodev09.truesheet.core.SizeChangeEvent
@@ -27,6 +28,8 @@ class TrueSheetView(context: Context) :
2728
private val surfaceId: Int
2829
get() = UIManagerHelper.getSurfaceId(this)
2930

31+
private var initialIndex: Int = -1
32+
3033
/**
3134
* Current activeIndex.
3235
*/
@@ -52,11 +55,6 @@ class TrueSheetView(context: Context) :
5255
*/
5356
private val rootSheetView: RootSheetView
5457

55-
/**
56-
* 2nd child of the container view.
57-
*/
58-
private var footerView: ViewGroup? = null
59-
6058
init {
6159
reactContext.addLifecycleEventListener(this)
6260
eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContext, id)
@@ -83,7 +81,7 @@ class TrueSheetView(context: Context) :
8381
presentPromise = null
8482
}
8583

86-
// dispatch onPresent event
84+
// Dispatch onPresent event
8785
eventDispatcher?.dispatchEvent(PresentEvent(surfaceId, id, sheetDialog.getSizeInfoForIndex(currentSizeIndex)))
8886
}
8987

@@ -97,7 +95,7 @@ class TrueSheetView(context: Context) :
9795
dismissPromise = null
9896
}
9997

100-
// dispatch onDismiss event
98+
// Dispatch onDismiss event
10199
eventDispatcher?.dispatchEvent(DismissEvent(surfaceId, id))
102100
}
103101

@@ -132,7 +130,7 @@ class TrueSheetView(context: Context) :
132130
currentSizeIndex = sizeInfo.index
133131
setupDimmedBackground(sizeInfo.index)
134132

135-
// dispatch onSizeChange event
133+
// Dispatch onSizeChange event
136134
eventDispatcher?.dispatchEvent(SizeChangeEvent(surfaceId, id, sizeInfo))
137135
}
138136
}
@@ -164,13 +162,27 @@ class TrueSheetView(context: Context) :
164162
visibility = GONE
165163

166164
(child as ViewGroup).let {
167-
// Container View's first child is the Content View
168-
footerView = it.getChildAt(1) as ViewGroup
169-
170-
sheetDialog.footerView = footerView
171-
172165
// rootView's first child is the Container View
173166
rootSheetView.addView(it, index)
167+
168+
// Initialize content
169+
UiThreadUtil.runOnUiThread {
170+
// 1st child is the content view
171+
val contentView = it.getChildAt(0) as ViewGroup
172+
setContentHeight(contentView.height)
173+
174+
// 2nd child is the footer view
175+
val footerView = it.getChildAt(1) as ViewGroup
176+
sheetDialog.footerView = footerView
177+
setFooterHeight(footerView.height)
178+
179+
if (initialIndex >= 0) {
180+
sheetDialog.present(initialIndex)
181+
}
182+
183+
// Dispatch onLoad event
184+
eventDispatcher?.dispatchEvent(LoadEvent(surfaceId, id))
185+
}
174186
}
175187
}
176188

@@ -224,28 +236,42 @@ class TrueSheetView(context: Context) :
224236
}
225237

226238
fun setMaxHeight(height: Int) {
239+
if (sheetDialog.maxSheetHeight == height) return
240+
227241
sheetDialog.maxSheetHeight = height
228242
configureIfShowing()
229243
}
230244

231245
fun setContentHeight(height: Int) {
246+
if (sheetDialog.contentHeight == height) return
247+
232248
sheetDialog.contentHeight = height
233249
configureIfShowing()
234250
}
235251

236252
fun setFooterHeight(height: Int) {
253+
if (sheetDialog.footerHeight == height) return
254+
237255
sheetDialog.footerHeight = height
238256
configureIfShowing()
239257
}
240258

241259
fun setDimmed(dimmed: Boolean) {
260+
if (sheetDialog.dimmed == dimmed) return
261+
242262
sheetDialog.dimmed = dimmed
243263
if (sheetDialog.isShowing) {
244264
sheetDialog.setupDimmedBackground(currentSizeIndex)
245265
}
246266
}
247267

268+
fun setInitialIndex(index: Int) {
269+
initialIndex = index
270+
}
271+
248272
fun setDimmedIndex(index: Int) {
273+
if (sheetDialog.dimmedIndex == index) return
274+
249275
sheetDialog.dimmedIndex = index
250276
if (sheetDialog.isShowing) {
251277
sheetDialog.setupDimmedBackground(currentSizeIndex)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.facebook.react.uimanager.ThemedReactContext
88
import com.facebook.react.uimanager.ViewGroupManager
99
import com.facebook.react.uimanager.annotations.ReactProp
1010
import com.lodev09.truesheet.core.DismissEvent
11+
import com.lodev09.truesheet.core.LoadEvent
1112
import com.lodev09.truesheet.core.PresentEvent
1213
import com.lodev09.truesheet.core.SizeChangeEvent
1314
import com.lodev09.truesheet.core.Utils
@@ -24,6 +25,7 @@ class TrueSheetViewManager : ViewGroupManager<TrueSheetView>() {
2425

2526
override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any>? =
2627
MapBuilder.builder<String, Any>()
28+
.put(LoadEvent.EVENT_NAME, MapBuilder.of("registrationName", "onLoad"))
2729
.put(PresentEvent.EVENT_NAME, MapBuilder.of("registrationName", "onPresent"))
2830
.put(DismissEvent.EVENT_NAME, MapBuilder.of("registrationName", "onDismiss"))
2931
.put(SizeChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onSizeChange"))
@@ -44,6 +46,11 @@ class TrueSheetViewManager : ViewGroupManager<TrueSheetView>() {
4446
view.setDimmed(dimmed)
4547
}
4648

49+
@ReactProp(name = "initialIndex")
50+
fun setInitialIndex(view: TrueSheetView, index: Int) {
51+
view.setInitialIndex(index)
52+
}
53+
4754
@ReactProp(name = "dimmedIndex")
4855
fun setDimmedIndex(view: TrueSheetView, index: Int) {
4956
view.setDimmedIndex(index)

android/src/main/java/com/lodev09/truesheet/core/Events.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ class PresentEvent(surfaceId: Int, viewId: Int, private val sizeInfo: SizeInfo)
2222
}
2323
}
2424

25+
// onLoad
26+
class LoadEvent(surfaceId: Int, viewId: Int) : Event<PresentEvent>(surfaceId, viewId) {
27+
override fun getEventName() = EVENT_NAME
28+
29+
override fun getEventData(): WritableMap = Arguments.createMap()
30+
31+
companion object {
32+
const val EVENT_NAME = "load"
33+
}
34+
}
35+
2536
// onDismiss
2637
class DismissEvent(surfaceId: Int, viewId: Int) : Event<PresentEvent>(surfaceId, viewId) {
2738
override fun getEventName() = EVENT_NAME

docs/docs/intro.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import preview from '/img/preview.gif'
1010

1111
The **_true_** native bottom sheet experience for your [React Native](https://reactnative.dev) Apps.
1212

13-
<img alt="preview" src={preview} width="600"/>
13+
<img alt="preview" src={preview} width="300"/>
1414

1515
## Features
1616

example/src/App.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { Button } from './components'
88
import { BLUE, DARK, GRAY, SPACING } from './utils'
99

1010
export default function App() {
11+
const sheetRef = useRef<TrueSheet>(null)
12+
1113
const basicSheet = useRef<TrueSheet>(null)
1214
const promptSheet = useRef<TrueSheet>(null)
1315
const scrollViewSheet = useRef<TrueSheet>(null)
@@ -25,7 +27,7 @@ export default function App() {
2527
style={$map}
2628
initialCamera={{
2729
altitude: 18000,
28-
zoom: 11,
30+
zoom: 14,
2931
center: { latitude: 9.306743705457553, longitude: 123.30474002203727 },
3032
pitch: 0,
3133
heading: 0,
@@ -35,14 +37,18 @@ export default function App() {
3537

3638
<TrueSheet
3739
sizes={['15%', 'auto', 'large']}
40+
ref={sheetRef}
3841
blurTint="dark"
3942
backgroundColor={DARK}
4043
contentContainerStyle={{ padding: SPACING, paddingBottom: SPACING * 3 }}
4144
dimmedIndex={2}
4245
dismissible={false}
4346
cornerRadius={12}
4447
initialIndex={1}
45-
onLoad={() => console.log('Sheet has been loaded!')}
48+
onLoad={() => {
49+
// sheetRef.current?.present(1)
50+
console.log('Sheet has been loaded!')
51+
}}
4652
>
4753
<View style={$heading}>
4854
<Text style={$title}>True Sheet 💩</Text>

ios/TrueSheetView.swift

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
176176

177177
@objc
178178
func setMaxHeight(_ height: NSNumber) {
179-
viewController.maxHeight = CGFloat(height.floatValue)
179+
let maxHeight = CGFloat(height.floatValue)
180+
guard viewController.maxHeight != maxHeight else {
181+
return
182+
}
183+
184+
viewController.maxHeight = maxHeight
180185
configurePresentedSheet()
181186
}
182187

@@ -186,17 +191,24 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
186191
let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow })
187192
let bottomInset = window?.safeAreaInsets.bottom ?? 0
188193

189-
viewController.contentHeight = CGFloat(height.floatValue) - bottomInset
194+
let contentHeight = CGFloat(height.floatValue) - bottomInset
195+
guard viewController.contentHeight != contentHeight else {
196+
return
197+
}
198+
199+
viewController.contentHeight = contentHeight
190200
configurePresentedSheet()
191201
}
192202

193203
@objc
194204
func setFooterHeight(_ height: NSNumber) {
195-
guard let footerView, let footerViewHeightConstraint else {
205+
let footerHeight = CGFloat(height.floatValue)
206+
guard let footerView, let footerViewHeightConstraint,
207+
viewController.footerHeight != footerHeight else {
196208
return
197209
}
198210

199-
viewController.footerHeight = CGFloat(height.floatValue)
211+
viewController.footerHeight = footerHeight
200212

201213
if footerView.subviews.first != nil {
202214
containerView?.bringSubviewToFront(footerView)
@@ -228,6 +240,7 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
228240
@objc
229241
func setCornerRadius(_ radius: NSNumber?) {
230242
var cornerRadius: CGFloat?
243+
231244
if let radius {
232245
cornerRadius = CGFloat(radius.floatValue)
233246
}
@@ -243,6 +256,7 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
243256
@objc
244257
func setGrabber(_ visible: Bool) {
245258
viewController.grabber = visible
259+
246260
if #available(iOS 15.0, *) {
247261
withPresentedSheet { sheet in
248262
sheet.prefersGrabberVisible = visible
@@ -252,6 +266,10 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
252266

253267
@objc
254268
func setDimmed(_ dimmed: Bool) {
269+
guard viewController.dimmed != dimmed else {
270+
return
271+
}
272+
255273
viewController.dimmed = dimmed
256274

257275
if #available(iOS 15.0, *) {
@@ -263,6 +281,10 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
263281

264282
@objc
265283
func setDimmedIndex(_ index: NSNumber) {
284+
guard viewController.dimmedIndex != index.intValue else {
285+
return
286+
}
287+
266288
viewController.dimmedIndex = index.intValue
267289

268290
if #available(iOS 15.0, *) {

0 commit comments

Comments
 (0)