-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathMentionHandler.kt
More file actions
68 lines (57 loc) · 1.59 KB
/
Copy pathMentionHandler.kt
File metadata and controls
68 lines (57 loc) · 1.59 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
package com.swmansion.enriched.textinput.events
import com.facebook.react.bridge.ReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.swmansion.enriched.textinput.EnrichedTextInputView
class MentionHandler(
private val view: EnrichedTextInputView,
) {
private var previousText: String? = null
private var previousIndicator: String? = null
fun reset() {
endMention()
previousText = null
}
fun endMention() {
val indicator = previousIndicator
if (indicator == null) return
emitEvent(indicator, null)
previousIndicator = null
}
fun onMention(
indicator: String,
text: String?,
) {
var startMention = false
// switching directly to an active mention
if (previousIndicator != indicator) {
startMention = true
endMention()
}
// explicit startMention event before changeMention event
if (startMention && !text.isNullOrEmpty()) {
emitEvent(indicator, "")
}
emitEvent(indicator, text)
}
private fun emitEvent(
indicator: String,
text: String?,
) {
// Do not emit events too often
if (previousIndicator == indicator && previousText == text) return
previousIndicator = indicator
previousText = text
val context = view.context as ReactContext
val surfaceId = UIManagerHelper.getSurfaceId(context)
val dispatcher = UIManagerHelper.getEventDispatcherForReactTag(context, view.id)
dispatcher?.dispatchEvent(
OnMentionEvent(
surfaceId,
view.id,
indicator,
text,
view.experimentalSynchronousEvents,
),
)
}
}