@@ -15,6 +15,7 @@ import com.lodev09.truesheet.core.RootSheetView
15
15
import com.lodev09.truesheet.core.Utils
16
16
import com.lodev09.truesheet.events.ContainerSizeChangeEvent
17
17
import com.lodev09.truesheet.events.DismissEvent
18
+ import com.lodev09.truesheet.events.DragEvent
18
19
import com.lodev09.truesheet.events.MountEvent
19
20
import com.lodev09.truesheet.events.PresentEvent
20
21
import com.lodev09.truesheet.events.SizeChangeEvent
@@ -33,6 +34,11 @@ class TrueSheetView(context: Context) :
33
34
var initialIndex: Int = - 1
34
35
var initialIndexAnimated: Boolean = true
35
36
37
+ /* *
38
+ * Determines if the sheet is being dragged by the user.
39
+ */
40
+ private var isDragging = false
41
+
36
42
/* *
37
43
* Current activeIndex.
38
44
*/
@@ -113,6 +119,16 @@ class TrueSheetView(context: Context) :
113
119
behavior.addBottomSheetCallback(
114
120
object : BottomSheetBehavior .BottomSheetCallback () {
115
121
override fun onSlide (sheetView : View , slideOffset : Float ) {
122
+ // We only dispatch event if user is actually dragging to be more consistent with IOS
123
+ // i.e. We ignore SETTLING state
124
+ if (isDragging && sheetDialog.behavior.state == BottomSheetBehavior .STATE_DRAGGING ) {
125
+ val height = maxScreenHeight - sheetView.top
126
+ val sizeInfo = SizeInfo (currentSizeIndex, Utils .toDIP(height.toFloat()))
127
+
128
+ // Dispatch drag change event
129
+ eventDispatcher?.dispatchEvent(DragEvent (surfaceId, id, DRAG_STATE_CHANGED , sizeInfo))
130
+ }
131
+
116
132
footerView?.let {
117
133
val y = (maxScreenHeight - sheetView.top - footerHeight).toFloat()
118
134
if (slideOffset >= 0 ) {
@@ -125,23 +141,45 @@ class TrueSheetView(context: Context) :
125
141
}
126
142
}
127
143
128
- override fun onStateChanged (view : View , newState : Int ) {
144
+ override fun onStateChanged (sheetView : View , newState : Int ) {
129
145
if (! isShowing) return
130
146
131
- val sizeInfo = getSizeInfoForState(newState)
132
- if (sizeInfo == null || sizeInfo.index == currentSizeIndex) return
147
+ // When changed to dragging, we know that the drag has started
148
+ if (newState == BottomSheetBehavior .STATE_DRAGGING ) {
149
+ val height = maxScreenHeight - sheetView.top
150
+ val sizeInfo = SizeInfo (currentSizeIndex, Utils .toDIP(height.toFloat()))
151
+
152
+ // Dispatch drag started event
153
+ eventDispatcher?.dispatchEvent(DragEvent (surfaceId, id, DRAG_STATE_BEGAN , sizeInfo))
133
154
134
- // Invoke promise when sheet resized programmatically
135
- presentPromise?.let { promise ->
136
- promise()
137
- presentPromise = null
155
+ // Flag sheet is being dragged
156
+ isDragging = true
157
+ return
138
158
}
139
159
140
- currentSizeIndex = sizeInfo.index
141
- setupDimmedBackground(sizeInfo.index)
160
+ val sizeInfo = getSizeInfoForState(newState)
161
+ sizeInfo?.let {
162
+ // If from drag, dispatch ended event
163
+ // We assume that if sizeInfo is available, it's one of the valid state
164
+ if (isDragging) {
165
+ eventDispatcher?.dispatchEvent(DragEvent (surfaceId, id, DRAG_STATE_ENDED , it))
166
+ isDragging = false
167
+ }
168
+
169
+ if (it.index != currentSizeIndex) {
170
+ // Invoke promise when sheet resized programmatically
171
+ presentPromise?.let { promise ->
172
+ promise()
173
+ presentPromise = null
174
+ }
175
+
176
+ currentSizeIndex = it.index
177
+ setupDimmedBackground(it.index)
142
178
143
- // Dispatch onSizeChange event
144
- eventDispatcher?.dispatchEvent(SizeChangeEvent (surfaceId, id, sizeInfo))
179
+ // Dispatch onSizeChange event
180
+ eventDispatcher?.dispatchEvent(SizeChangeEvent (surfaceId, id, it))
181
+ }
182
+ }
145
183
}
146
184
}
147
185
)
@@ -339,6 +377,10 @@ class TrueSheetView(context: Context) :
339
377
}
340
378
341
379
companion object {
380
+ const val DRAG_STATE_BEGAN = " began"
381
+ const val DRAG_STATE_CHANGED = " changed"
382
+ const val DRAG_STATE_ENDED = " ended"
383
+
342
384
const val TAG = " TrueSheetView"
343
385
}
344
386
}
0 commit comments