Skip to content

fix: double synchronization #1017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ class KeyboardAnimationCallback(
val view: View,
val context: ThemedReactContext?,
private val config: KeyboardAnimationCallbackConfig,
private val source: KeyboardAnimationCallback? = null,
) : WindowInsetsAnimationCompat.Callback(config.dispatchMode),
OnApplyWindowInsetsListener,
Suspendable {
private val surfaceId = UIManagerHelper.getSurfaceId(eventPropagationView)

// state variables
private var persistentKeyboardHeight = 0.0
private var prevKeyboardHeight = 0.0
private var persistentKeyboardHeight = getCurrentKeyboardHeight()
private var prevKeyboardHeight = getCurrentKeyboardHeight()
private var isKeyboardVisible = false
private var isTransitioning = false
private var duration = 0
Expand Down Expand Up @@ -161,11 +162,36 @@ class KeyboardAnimationCallback(
Logger.i(TAG, "onApplyWindowInsets: ${this.persistentKeyboardHeight} -> $keyboardHeight")
layoutObserver?.syncUpLayout()
this.onKeyboardResized(keyboardHeight)

return insets
}

// always verify insets, because sometimes default lifecycle methods may not be invoked
// (when we press "Share" on Android 16, when Modal closes keyboard, etc.)
val newHeight = getCurrentKeyboardHeight(insets)
if (prevKeyboardHeight != newHeight && !isMoving && !isSuspended) {
Logger.w(
TAG,
"detected desynchronized state - force updating it. $prevKeyboardHeight -> $newHeight. Attached: ${view.isAttachedToWindow} EVA: ${this.eventPropagationView.isAttachedToWindow} Modal ${this.source}",
)
this.syncKeyboardPosition(newHeight, newHeight > 0)
}

return insets
}

override fun onPrepare(animation: WindowInsetsAnimationCompat) {
super.onPrepare(animation)

println("desynchronized - onPrepare")

if (!animation.isKeyboardAnimation || isSuspended) {
return
}

isTransitioning = true
}

@Suppress("detekt:ReturnCount")
override fun onStart(
animation: WindowInsetsAnimationCompat,
Expand All @@ -175,7 +201,6 @@ class KeyboardAnimationCallback(
return bounds
}

isTransitioning = true
isKeyboardVisible = isKeyboardVisible()
duration = animation.durationMillis.toInt()
val keyboardHeight = getCurrentKeyboardHeight()
Expand Down Expand Up @@ -415,14 +440,15 @@ class KeyboardAnimationCallback(
return insets?.isVisible(WindowInsetsCompat.Type.ime()) ?: false
}

private fun getCurrentKeyboardHeight(): Double {
val insets = ViewCompat.getRootWindowInsets(view)
val keyboardHeight = insets?.getInsets(WindowInsetsCompat.Type.ime())?.bottom ?: 0
private fun getCurrentKeyboardHeight(insets: WindowInsetsCompat? = null): Double {
val root = ViewCompat.getRootWindowInsets(view)
val final = insets ?: root
val keyboardHeight = final?.getInsets(WindowInsetsCompat.Type.ime())?.bottom ?: 0
val navigationBar =
if (config.hasTranslucentNavigationBar) {
0
} else {
insets?.getInsets(WindowInsetsCompat.Type.navigationBars())?.bottom ?: 0
root?.getInsets(WindowInsetsCompat.Type.navigationBars())?.bottom ?: 0
}

// on hide it will be negative value, so we are using max function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ModalAttachedWatcher(
return
}

val cb = this.callback()
val modal =
try {
uiManager?.resolveView(event.viewTag) as? ReactModalHostView
Expand All @@ -46,6 +47,9 @@ class ModalAttachedWatcher(
if (modal == null) {
return
}
if (cb == null) {
return
}

val dialog = modal.dialog
val window = dialog?.window
Expand All @@ -62,6 +66,7 @@ class ModalAttachedWatcher(
eventPropagationView = view,
context = reactContext,
config = config,
source = cb,
)

rootView.addView(eventView)
Expand All @@ -70,28 +75,22 @@ class ModalAttachedWatcher(
// on Android < 12 all events for `WindowInsetsAnimationCallback`
// go through main `rootView`, so we don't need to stop main
// callback - otherwise keyboard transitions will not be animated
this.callback()?.suspend(true)
cb.suspend(true)
// attaching callback to Modal on Android < 12 can cause ghost animations, see: https://github.com/kirillzyusko/react-native-keyboard-controller/pull/718
// and overall attaching additional callbacks (if animation events go through the main window) is not necessary
// and overall attaching additional callbacks (if animation events go through the main window)
// is not necessary
ViewCompat.setWindowInsetsAnimationCallback(rootView, callback)
ViewCompat.setOnApplyWindowInsetsListener(eventView, callback)

// when modal is shown then keyboard will be hidden by default
//
// - if events are coming from main window - then keyboard position
// will be synchronized from main window callback
// - if events are coming from modal window - then we need to update
// position ourself, because callback can be attached after keyboard
// auto-dismissal and we may miss some events and keyboard position
// will be outdated
callback.syncKeyboardPosition(0.0, false)
}

dialog?.setOnDismissListener {
callback.syncKeyboardPosition()
callback.destroy()
eventView.removeSelf()
this.callback()?.suspend(false)
// un-pause it in next frame because straight away `onApplyWindowInsets` will be called
view.post {
this.callback()?.suspend(false)
}
}

// imitating edge-to-edge mode behavior
Expand Down
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@
"Pixelfed",
"Kwai",
"Kwibo",
"revolut"
"revolut",
"desynchronized"
],
"ignorePaths": [
"node_modules",
Expand Down
Loading