Skip to content

Commit bb5a2d7

Browse files
committed
refactor(android): normalize event dispatcher
1 parent 199c0d8 commit bb5a2d7

11 files changed

+57
-206
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lodev09.truesheet
2+
3+
import com.facebook.react.bridge.Arguments
4+
import com.facebook.react.bridge.WritableMap
5+
import com.facebook.react.uimanager.events.Event
6+
7+
// onDragBegin
8+
class TrueSheetEvent(surfaceId: Int, viewId: Int, private val name: String, private val data: WritableMap?) :
9+
Event<TrueSheetEvent>(surfaceId, viewId) {
10+
override fun getEventName() = name
11+
override fun getEventData(): WritableMap = data ?: Arguments.createMap()
12+
13+
companion object {
14+
const val MOUNT = "mount"
15+
const val PRESENT = "present"
16+
const val DISMISS = "dismiss"
17+
const val SIZE_CHANGE = "sizeChange"
18+
const val DRAG_BEGIN = "dragBegin"
19+
const val DRAG_CHANGE = "dragChange"
20+
const val DRAG_END = "dragEnd"
21+
const val CONTAINER_SIZE_CHANGE = "containerSizeChange"
22+
}
23+
}

β€Žandroid/src/main/java/com/lodev09/truesheet/TrueSheetView.kt

+26-17
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@ import android.view.View
55
import android.view.ViewGroup
66
import android.view.ViewStructure
77
import android.view.accessibility.AccessibilityEvent
8+
import com.facebook.react.bridge.Arguments
89
import com.facebook.react.bridge.LifecycleEventListener
910
import com.facebook.react.bridge.UiThreadUtil
11+
import com.facebook.react.bridge.WritableMap
1012
import com.facebook.react.uimanager.ThemedReactContext
1113
import com.facebook.react.uimanager.UIManagerHelper
1214
import com.facebook.react.uimanager.events.EventDispatcher
1315
import com.google.android.material.bottomsheet.BottomSheetBehavior
1416
import com.lodev09.truesheet.core.RootSheetView
1517
import com.lodev09.truesheet.core.Utils
16-
import com.lodev09.truesheet.events.ContainerSizeChangeEvent
17-
import com.lodev09.truesheet.events.DismissEvent
18-
import com.lodev09.truesheet.events.DragBeginEvent
19-
import com.lodev09.truesheet.events.DragChangeEvent
20-
import com.lodev09.truesheet.events.DragEndEvent
21-
import com.lodev09.truesheet.events.MountEvent
22-
import com.lodev09.truesheet.events.PresentEvent
23-
import com.lodev09.truesheet.events.SizeChangeEvent
2418

2519
class TrueSheetView(context: Context) :
2620
ViewGroup(context),
@@ -78,7 +72,10 @@ class TrueSheetView(context: Context) :
7872
// Configure Sheet Dialog
7973
sheetDialog.apply {
8074
setOnSizeChangeListener { w, h ->
81-
eventDispatcher?.dispatchEvent(ContainerSizeChangeEvent(surfaceId, id, Utils.toDIP(w.toFloat()), Utils.toDIP(h.toFloat())))
75+
val data = Arguments.createMap()
76+
data.putDouble("width", Utils.toDIP(w.toFloat()).toDouble())
77+
data.putDouble("height", Utils.toDIP(h.toFloat()).toDouble())
78+
dispatchEvent(TrueSheetEvent.CONTAINER_SIZE_CHANGE, data)
8279
}
8380

8481
// Setup listener when the dialog has been presented.
@@ -100,7 +97,7 @@ class TrueSheetView(context: Context) :
10097
}
10198

10299
// Dispatch onPresent event
103-
eventDispatcher?.dispatchEvent(PresentEvent(surfaceId, id, sheetDialog.getSizeInfoForIndex(currentSizeIndex)))
100+
dispatchEvent(TrueSheetEvent.PRESENT, sizeInfoData(sheetDialog.getSizeInfoForIndex(currentSizeIndex)))
104101
}
105102

106103
// Setup listener when the dialog has been dismissed.
@@ -114,7 +111,7 @@ class TrueSheetView(context: Context) :
114111
}
115112

116113
// Dispatch onDismiss event
117-
eventDispatcher?.dispatchEvent(DismissEvent(surfaceId, id))
114+
dispatchEvent(TrueSheetEvent.DISMISS)
118115
}
119116

120117
// Configure sheet behavior events
@@ -130,7 +127,7 @@ class TrueSheetView(context: Context) :
130127
val sizeInfo = SizeInfo(currentSizeIndex, Utils.toDIP(height.toFloat()))
131128

132129
// Dispatch drag change event
133-
eventDispatcher?.dispatchEvent(DragChangeEvent(surfaceId, id, sizeInfo))
130+
dispatchEvent(TrueSheetEvent.DRAG_CHANGE, sizeInfoData(sizeInfo))
134131
}
135132

136133
else -> { }
@@ -159,7 +156,7 @@ class TrueSheetView(context: Context) :
159156
// When changed to dragging, we know that the drag has started
160157
BottomSheetBehavior.STATE_DRAGGING -> {
161158
// Dispatch drag started event
162-
eventDispatcher?.dispatchEvent(DragBeginEvent(surfaceId, id, currentSizeInfo))
159+
dispatchEvent(TrueSheetEvent.DRAG_BEGIN, sizeInfoData(currentSizeInfo))
163160

164161
// Flag sheet is being dragged
165162
isDragging = true
@@ -176,7 +173,7 @@ class TrueSheetView(context: Context) :
176173
val sizeInfo = getSizeInfoForState(newState)
177174
sizeInfo?.let {
178175
// Dispatch drag ended after dragging
179-
eventDispatcher?.dispatchEvent(DragEndEvent(surfaceId, id, it))
176+
dispatchEvent(TrueSheetEvent.DRAG_END, sizeInfoData(it))
180177
if (it.index != currentSizeIndex) {
181178
// Invoke promise when sheet resized programmatically
182179
presentPromise?.let { promise ->
@@ -188,7 +185,7 @@ class TrueSheetView(context: Context) :
188185
setupDimmedBackground(it.index)
189186

190187
// Dispatch onSizeChange event
191-
eventDispatcher?.dispatchEvent(SizeChangeEvent(surfaceId, id, it))
188+
dispatchEvent(TrueSheetEvent.SIZE_CHANGE, sizeInfoData(it))
192189
}
193190
}
194191

@@ -248,7 +245,7 @@ class TrueSheetView(context: Context) :
248245
}
249246

250247
// Dispatch onMount event
251-
eventDispatcher?.dispatchEvent(MountEvent(surfaceId, id))
248+
dispatchEvent(TrueSheetEvent.MOUNT)
252249
}
253250
}
254251
}
@@ -295,6 +292,18 @@ class TrueSheetView(context: Context) :
295292
sheetDialog.dismiss()
296293
}
297294

295+
private fun sizeInfoData(sizeInfo: SizeInfo): WritableMap {
296+
val data = Arguments.createMap()
297+
data.putInt("index", sizeInfo.index)
298+
data.putDouble("value", sizeInfo.value.toDouble())
299+
300+
return data
301+
}
302+
303+
private fun dispatchEvent(name: String, data: WritableMap? = null) {
304+
eventDispatcher?.dispatchEvent(TrueSheetEvent(surfaceId, id, name, data))
305+
}
306+
298307
private fun configureIfShowing() {
299308
if (sheetDialog.isShowing) {
300309
sheetDialog.configure()
@@ -384,7 +393,7 @@ class TrueSheetView(context: Context) :
384393
// For consistency with IOS, we are not waiting
385394
// for the state to change before dispatching onSizeChange event.
386395
val sizeInfo = sheetDialog.getSizeInfoForIndex(sizeIndex)
387-
eventDispatcher?.dispatchEvent(SizeChangeEvent(surfaceId, id, sizeInfo))
396+
dispatchEvent(TrueSheetEvent.SIZE_CHANGE, sizeInfoData(sizeInfo))
388397

389398
promiseCallback()
390399
} else {

β€Žandroid/src/main/java/com/lodev09/truesheet/TrueSheetViewManager.kt

+8-16
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ import com.facebook.react.uimanager.ThemedReactContext
1111
import com.facebook.react.uimanager.ViewGroupManager
1212
import com.facebook.react.uimanager.annotations.ReactProp
1313
import com.lodev09.truesheet.core.Utils
14-
import com.lodev09.truesheet.events.ContainerSizeChangeEvent
15-
import com.lodev09.truesheet.events.DismissEvent
16-
import com.lodev09.truesheet.events.DragBeginEvent
17-
import com.lodev09.truesheet.events.DragChangeEvent
18-
import com.lodev09.truesheet.events.DragEndEvent
19-
import com.lodev09.truesheet.events.MountEvent
20-
import com.lodev09.truesheet.events.PresentEvent
21-
import com.lodev09.truesheet.events.SizeChangeEvent
2214

2315
class TrueSheetViewManager : ViewGroupManager<TrueSheetView>() {
2416
override fun getName() = TAG
@@ -32,14 +24,14 @@ class TrueSheetViewManager : ViewGroupManager<TrueSheetView>() {
3224

3325
override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any>? =
3426
MapBuilder.builder<String, Any>()
35-
.put(MountEvent.EVENT_NAME, MapBuilder.of("registrationName", "onMount"))
36-
.put(PresentEvent.EVENT_NAME, MapBuilder.of("registrationName", "onPresent"))
37-
.put(DismissEvent.EVENT_NAME, MapBuilder.of("registrationName", "onDismiss"))
38-
.put(SizeChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onSizeChange"))
39-
.put(DragBeginEvent.EVENT_NAME, MapBuilder.of("registrationName", "onDragBegin"))
40-
.put(DragChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onDragChange"))
41-
.put(DragEndEvent.EVENT_NAME, MapBuilder.of("registrationName", "onDragEnd"))
42-
.put(ContainerSizeChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onContainerSizeChange"))
27+
.put(TrueSheetEvent.MOUNT, MapBuilder.of("registrationName", "onMount"))
28+
.put(TrueSheetEvent.PRESENT, MapBuilder.of("registrationName", "onPresent"))
29+
.put(TrueSheetEvent.DISMISS, MapBuilder.of("registrationName", "onDismiss"))
30+
.put(TrueSheetEvent.SIZE_CHANGE, MapBuilder.of("registrationName", "onSizeChange"))
31+
.put(TrueSheetEvent.DRAG_BEGIN, MapBuilder.of("registrationName", "onDragBegin"))
32+
.put(TrueSheetEvent.DRAG_CHANGE, MapBuilder.of("registrationName", "onDragChange"))
33+
.put(TrueSheetEvent.DRAG_END, MapBuilder.of("registrationName", "onDragEnd"))
34+
.put(TrueSheetEvent.CONTAINER_SIZE_CHANGE, MapBuilder.of("registrationName", "onContainerSizeChange"))
4335
.build()
4436

4537
@ReactProp(name = "edgeToEdge")

β€Žandroid/src/main/java/com/lodev09/truesheet/events/ContainerSizeChangeEvent.kt

-23
This file was deleted.

β€Žandroid/src/main/java/com/lodev09/truesheet/events/DismissEvent.kt

-16
This file was deleted.

β€Žandroid/src/main/java/com/lodev09/truesheet/events/DragBeginEvent.kt

-24
This file was deleted.

β€Žandroid/src/main/java/com/lodev09/truesheet/events/DragChangeEvent.kt

-24
This file was deleted.

β€Žandroid/src/main/java/com/lodev09/truesheet/events/DragEndEvent.kt

-24
This file was deleted.

β€Žandroid/src/main/java/com/lodev09/truesheet/events/MountEvent.kt

-16
This file was deleted.

β€Žandroid/src/main/java/com/lodev09/truesheet/events/PresentEvent.kt

-23
This file was deleted.

β€Žandroid/src/main/java/com/lodev09/truesheet/events/SizeChangeEvent.kt

-23
This file was deleted.

0 commit comments

Comments
Β (0)