-
-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathThemedReactContext.kt
More file actions
69 lines (59 loc) · 2.13 KB
/
ThemedReactContext.kt
File metadata and controls
69 lines (59 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.reactnativekeyboardcontroller.extensions
import android.content.Context
import android.os.Build
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.WritableMap
import com.facebook.react.modules.core.DeviceEventManagerModule
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.Event
import com.facebook.react.uimanager.events.EventDispatcher
import com.reactnativekeyboardcontroller.log.Logger
fun ThemedReactContext?.dispatchEvent(
viewId: Int,
event: Event<*>,
) {
val eventDispatcher: EventDispatcher? =
UIManagerHelper.getEventDispatcherForReactTag(this as ReactContext, viewId)
eventDispatcher?.dispatchEvent(event)
}
fun ThemedReactContext?.emitEvent(
event: String,
params: WritableMap,
) {
this
?.reactApplicationContext
?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
?.emit(event, params)
Logger.i("ThemedReactContext", event)
}
fun ThemedReactContext?.keepShadowNodesInSync(viewId: Int) {
// originally by viewId we should lookup all connected nodes
// and send them to JS
// but at the moment JS side broadcasts events to all ViewType
// instances, so we can send even empty array
val tags = intArrayOf(viewId)
val tagsArray = Arguments.createArray()
for (tag in tags) {
tagsArray.pushInt(tag)
}
// emit the event to JS to re-sync the trees
val onAnimationEndedData = Arguments.createMap()
onAnimationEndedData.putArray("tags", tagsArray)
this?.reactApplicationContext?.emitDeviceEvent("onUserDrivenAnimationEnded", onAnimationEndedData)
}
val ThemedReactContext?.appearance: String
get() =
when {
this == null -> "default"
isSystemDarkMode(this) -> "dark"
else -> "light"
}
private fun isSystemDarkMode(context: Context): Boolean =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
(context.getSystemService(Context.UI_MODE_SERVICE) as? android.app.UiModeManager)
?.nightMode == android.app.UiModeManager.MODE_NIGHT_YES
} else {
false
}