@@ -20,6 +20,7 @@ import com.facebook.react.views.textinput.ReactEditText
20
20
import com.facebook.react.views.view.ReactViewGroup
21
21
import com.reactnativekeyboardcontroller.InteractiveKeyboardProvider
22
22
import com.reactnativekeyboardcontroller.events.KeyboardTransitionEvent
23
+ import com.reactnativekeyboardcontroller.events.KeyboardTransitionEventData
23
24
import com.reactnativekeyboardcontroller.extensions.dispatchEvent
24
25
import com.reactnativekeyboardcontroller.extensions.dp
25
26
import com.reactnativekeyboardcontroller.extensions.isKeyboardAnimation
@@ -43,6 +44,7 @@ class KeyboardAnimationCallback(
43
44
private var duration = 0
44
45
private var viewTagFocused = - 1
45
46
private var animation: ValueAnimator ? = null
47
+ private var lastEventDispatched: KeyboardTransitionEventData ? = null
46
48
47
49
// listeners
48
50
private val focusListener = OnGlobalFocusChangeListener { oldFocus, newFocus ->
@@ -56,23 +58,17 @@ class KeyboardAnimationCallback(
56
58
// 2. event should be send only when keyboard is visible, since this event arrives earlier -> `tag` will be
57
59
// 100% included in onStart/onMove/onEnd lifecycles, but triggering onStart/onEnd several time
58
60
// can bring breaking changes
59
- context.dispatchEvent(
60
- view.id,
61
- KeyboardTransitionEvent (
62
- surfaceId,
63
- view.id,
61
+ this .dispatchEventToJS(
62
+ KeyboardTransitionEventData (
64
63
" topKeyboardMoveStart" ,
65
64
this .persistentKeyboardHeight,
66
65
1.0 ,
67
66
0 ,
68
67
viewTagFocused,
69
68
),
70
69
)
71
- context.dispatchEvent(
72
- view.id,
73
- KeyboardTransitionEvent (
74
- surfaceId,
75
- view.id,
70
+ this .dispatchEventToJS(
71
+ KeyboardTransitionEventData (
76
72
" topKeyboardMoveEnd" ,
77
73
this .persistentKeyboardHeight,
78
74
1.0 ,
@@ -164,11 +160,8 @@ class KeyboardAnimationCallback(
164
160
)
165
161
166
162
Log .i(TAG , " HEIGHT:: $keyboardHeight TAG:: $viewTagFocused " )
167
- context.dispatchEvent(
168
- view.id,
169
- KeyboardTransitionEvent (
170
- surfaceId,
171
- view.id,
163
+ this .dispatchEventToJS(
164
+ KeyboardTransitionEventData (
172
165
" topKeyboardMoveStart" ,
173
166
keyboardHeight,
174
167
if (! isKeyboardVisible) 0.0 else 1.0 ,
@@ -215,11 +208,8 @@ class KeyboardAnimationCallback(
215
208
)
216
209
217
210
val event = if (InteractiveKeyboardProvider .isInteractive) " topKeyboardMoveInteractive" else " topKeyboardMove"
218
- context.dispatchEvent(
219
- view.id,
220
- KeyboardTransitionEvent (
221
- surfaceId,
222
- view.id,
211
+ this .dispatchEventToJS(
212
+ KeyboardTransitionEventData (
223
213
event,
224
214
height,
225
215
progress,
@@ -259,11 +249,8 @@ class KeyboardAnimationCallback(
259
249
" KeyboardController::" + if (! isKeyboardVisible) " keyboardDidHide" else " keyboardDidShow" ,
260
250
getEventParams(keyboardHeight),
261
251
)
262
- context.dispatchEvent(
263
- view.id,
264
- KeyboardTransitionEvent (
265
- surfaceId,
266
- view.id,
252
+ this .dispatchEventToJS(
253
+ KeyboardTransitionEventData (
267
254
" topKeyboardMoveEnd" ,
268
255
keyboardHeight,
269
256
if (! isKeyboardVisible) 0.0 else 1.0 ,
@@ -300,11 +287,8 @@ class KeyboardAnimationCallback(
300
287
}
301
288
302
289
this .emitEvent(" KeyboardController::keyboardWillShow" , getEventParams(keyboardHeight))
303
- context.dispatchEvent(
304
- view.id,
305
- KeyboardTransitionEvent (
306
- surfaceId,
307
- view.id,
290
+ this .dispatchEventToJS(
291
+ KeyboardTransitionEventData (
308
292
" topKeyboardMoveStart" ,
309
293
keyboardHeight,
310
294
1.0 ,
@@ -317,11 +301,8 @@ class KeyboardAnimationCallback(
317
301
ValueAnimator .ofFloat(this .persistentKeyboardHeight.toFloat(), keyboardHeight.toFloat())
318
302
animation.addUpdateListener { animator ->
319
303
val toValue = animator.animatedValue as Float
320
- context.dispatchEvent(
321
- view.id,
322
- KeyboardTransitionEvent (
323
- surfaceId,
324
- view.id,
304
+ this .dispatchEventToJS(
305
+ KeyboardTransitionEventData (
325
306
" topKeyboardMove" ,
326
307
toValue.toDouble(),
327
308
toValue.toDouble() / keyboardHeight,
@@ -332,11 +313,8 @@ class KeyboardAnimationCallback(
332
313
}
333
314
animation.doOnEnd {
334
315
this .emitEvent(" KeyboardController::keyboardDidShow" , getEventParams(keyboardHeight))
335
- context.dispatchEvent(
336
- view.id,
337
- KeyboardTransitionEvent (
338
- surfaceId,
339
- view.id,
316
+ this .dispatchEventToJS(
317
+ KeyboardTransitionEventData (
340
318
" topKeyboardMoveEnd" ,
341
319
keyboardHeight,
342
320
1.0 ,
@@ -384,6 +362,20 @@ class KeyboardAnimationCallback(
384
362
return params
385
363
}
386
364
365
+ private fun dispatchEventToJS (event : KeyboardTransitionEventData ) {
366
+ if (event != lastEventDispatched) {
367
+ lastEventDispatched = event
368
+ context.dispatchEvent(
369
+ view.id,
370
+ KeyboardTransitionEvent (
371
+ surfaceId,
372
+ view.id,
373
+ data = event,
374
+ ),
375
+ )
376
+ }
377
+ }
378
+
387
379
companion object {
388
380
private const val DEFAULT_ANIMATION_TIME = 250
389
381
}
0 commit comments