|
1 | 1 | package com.swmansion.rnscreens.gamma.tabs
|
2 | 2 |
|
3 | 3 | import android.util.Log
|
4 |
| -import android.view.View |
5 | 4 | import com.facebook.react.bridge.ReactContext
|
6 |
| -import com.facebook.react.uimanager.UIManagerHelper |
7 |
| -import com.facebook.react.uimanager.events.EventDispatcher |
| 5 | +import com.swmansion.rnscreens.gamma.common.BaseEventEmitter |
8 | 6 | import com.swmansion.rnscreens.gamma.tabs.TabScreenEventEmitter.Companion.TAG
|
9 | 7 | import com.swmansion.rnscreens.gamma.tabs.event.TabScreenDidAppearEvent
|
10 | 8 | import com.swmansion.rnscreens.gamma.tabs.event.TabScreenDidDisappearEvent
|
11 | 9 | import com.swmansion.rnscreens.gamma.tabs.event.TabScreenWillAppearEvent
|
12 | 10 | import com.swmansion.rnscreens.gamma.tabs.event.TabScreenWillDisappearEvent
|
13 | 11 |
|
14 |
| -class TabScreenEventEmitter(val reactContext: ReactContext) { |
15 |
| - var viewTag: Int = View.NO_ID |
16 |
| - |
17 |
| - private val reactEventDispatcher: EventDispatcher? |
18 |
| - get() { |
19 |
| - // Lets assert for now to make sure we won't miss event delivery |
20 |
| - checkValidViewTag() |
21 |
| - return UIManagerHelper.getEventDispatcherForReactTag(reactContext, viewTag) |
22 |
| - } |
23 |
| - |
24 |
| - private val surfaceId: Int |
25 |
| - get() = UIManagerHelper.getSurfaceId(reactContext) |
26 |
| - |
| 12 | +internal class TabScreenEventEmitter( |
| 13 | + reactContext: ReactContext, |
| 14 | + viewTag: Int, |
| 15 | +) : BaseEventEmitter(reactContext, viewTag) { |
27 | 16 | fun emitOnWillAppear() {
|
28 |
| - checkValidViewTag() |
29 | 17 | logEventDispatch(viewTag, TabScreenWillAppearEvent.EVENT_REGISTRATION_NAME)
|
30 |
| - reactEventDispatcher?.dispatchEvent(TabScreenWillAppearEvent(surfaceId, viewTag)) |
| 18 | + reactEventDispatcher.dispatchEvent(TabScreenWillAppearEvent(surfaceId, viewTag)) |
31 | 19 | }
|
32 | 20 |
|
33 | 21 | fun emitOnDidAppear() {
|
34 |
| - checkValidViewTag() |
35 | 22 | logEventDispatch(viewTag, TabScreenDidAppearEvent.EVENT_REGISTRATION_NAME)
|
36 |
| - reactEventDispatcher?.dispatchEvent(TabScreenDidAppearEvent(surfaceId, viewTag)) |
| 23 | + reactEventDispatcher.dispatchEvent(TabScreenDidAppearEvent(surfaceId, viewTag)) |
37 | 24 | }
|
38 | 25 |
|
39 | 26 | fun emitOnWillDisappear() {
|
40 |
| - checkValidViewTag() |
41 | 27 | logEventDispatch(viewTag, TabScreenWillDisappearEvent.EVENT_REGISTRATION_NAME)
|
42 |
| - reactEventDispatcher?.dispatchEvent(TabScreenWillDisappearEvent(surfaceId, viewTag)) |
| 28 | + reactEventDispatcher.dispatchEvent(TabScreenWillDisappearEvent(surfaceId, viewTag)) |
43 | 29 | }
|
44 | 30 |
|
45 | 31 | fun emitOnDidDisappear() {
|
46 |
| - checkValidViewTag() |
47 | 32 | logEventDispatch(viewTag, TabScreenDidDisappearEvent.EVENT_REGISTRATION_NAME)
|
48 |
| - reactEventDispatcher?.dispatchEvent(TabScreenDidDisappearEvent(surfaceId, viewTag)) |
49 |
| - } |
50 |
| - |
51 |
| - private fun checkValidViewTag() { |
52 |
| - check(viewTag != View.NO_ID) { "[RNScreens] Attempt to use viewTag before the value was provided"} |
| 33 | + reactEventDispatcher.dispatchEvent(TabScreenDidDisappearEvent(surfaceId, viewTag)) |
53 | 34 | }
|
54 | 35 |
|
55 | 36 | companion object {
|
56 | 37 | const val TAG = "TabScreenEventEmitter"
|
57 | 38 | }
|
58 | 39 | }
|
59 | 40 |
|
60 |
| -private fun logEventDispatch(viewTag: Int, eventName: String) { |
| 41 | +private fun logEventDispatch( |
| 42 | + viewTag: Int, |
| 43 | + eventName: String, |
| 44 | +) { |
61 | 45 | Log.d(TAG, "TabScreen [$viewTag] emits event: $eventName")
|
62 | 46 | }
|
0 commit comments