Skip to content

Commit fe47069

Browse files
committed
fix: double synchronization
1 parent 93d38c6 commit fe47069

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

android/src/main/java/com/reactnativekeyboardcontroller/listeners/KeyboardAnimationCallback.kt

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,34 @@ class KeyboardAnimationCallback(
161161
Logger.i(TAG, "onApplyWindowInsets: ${this.persistentKeyboardHeight} -> $keyboardHeight")
162162
layoutObserver?.syncUpLayout()
163163
this.onKeyboardResized(keyboardHeight)
164+
165+
return insets
166+
}
167+
168+
// always verify insets, because sometimes default lifecycles may not be invoked
169+
// (when we press "Share" on Android 16, for example)
170+
val newHeight = getCurrentKeyboardHeight(insets)
171+
if (prevKeyboardHeight != newHeight && !isTransitioning) {
172+
Logger.w(
173+
TAG,
174+
"detected desynchronized state - force updating it. $prevKeyboardHeight -> $newHeight"
175+
)
176+
this.syncKeyboardPosition(newHeight, newHeight > 0)
164177
}
165178

166179
return insets
167180
}
168181

182+
override fun onPrepare(animation: WindowInsetsAnimationCompat) {
183+
super.onPrepare(animation)
184+
185+
if (!animation.isKeyboardAnimation || isSuspended) {
186+
return
187+
}
188+
189+
isTransitioning = true
190+
}
191+
169192
@Suppress("detekt:ReturnCount")
170193
override fun onStart(
171194
animation: WindowInsetsAnimationCompat,
@@ -175,7 +198,6 @@ class KeyboardAnimationCallback(
175198
return bounds
176199
}
177200

178-
isTransitioning = true
179201
isKeyboardVisible = isKeyboardVisible()
180202
duration = animation.durationMillis.toInt()
181203
val keyboardHeight = getCurrentKeyboardHeight()
@@ -415,14 +437,15 @@ class KeyboardAnimationCallback(
415437
return insets?.isVisible(WindowInsetsCompat.Type.ime()) ?: false
416438
}
417439

418-
private fun getCurrentKeyboardHeight(): Double {
419-
val insets = ViewCompat.getRootWindowInsets(view)
420-
val keyboardHeight = insets?.getInsets(WindowInsetsCompat.Type.ime())?.bottom ?: 0
440+
private fun getCurrentKeyboardHeight(insets: WindowInsetsCompat? = null): Double {
441+
val root = ViewCompat.getRootWindowInsets(view)
442+
val final = insets ?: root
443+
val keyboardHeight = final?.getInsets(WindowInsetsCompat.Type.ime())?.bottom ?: 0
421444
val navigationBar =
422445
if (config.hasTranslucentNavigationBar) {
423446
0
424447
} else {
425-
insets?.getInsets(WindowInsetsCompat.Type.navigationBars())?.bottom ?: 0
448+
root?.getInsets(WindowInsetsCompat.Type.navigationBars())?.bottom ?: 0
426449
}
427450

428451
// on hide it will be negative value, so we are using max function

0 commit comments

Comments
 (0)