From b2e70e52dd02576d5e5bec5ca08d9edfb22279c8 Mon Sep 17 00:00:00 2001 From: Kirill Zyusko Date: Mon, 3 Apr 2023 19:00:02 +0400 Subject: [PATCH] feat: interactive keyboard (android) (#133) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 📜 Description Added Interactive Keyboard dismissing on Android 11+. ## 💡 Motivation and Context Initially I planned to control the keyboard properties (`opacity` and `position`) via animated properties (`Animated.Value`). But it turned out, that every update of animated value is not scheduled on UI thread and since we need to call `controller.setInsetsAndAlpha` on UI thread we need to wrap it in `UiThreadUtil.runOnUiThread`. It works, but since it creates a new thread too frequently CPU is overused (CPU usage is too high, as a result battery gets drained faster and animation is not very smooth). The second idea was to add JSI `interpolate` function, which can be called directly on UI thread. I thought about usage of worklets (`react-native-worklets`/`react-native-reanimated`) or writing pure JSI function. But in the end I decided to postpone it, since right now I need to support both (paper and fabric) architectures and it seems like JSI is changing time to time (depends on a react-native version) and I thought that it may be complicated to support such code. Taking everything from above into consideration I've decided to implement `interpolate` function in a native language (kotlin). It works well for both architectures, since there is no direct communication between JS and Native thread, though it brings own restrictions, such as limited customization (you have only two pre-defined variants of the function and you can not write your own implementation in JS code). However I think to rewrite it to JSI in the future to make it more customizable. ## 📢 Changelog ### JS - added specs for new `KeyboardGestureArea` component; - added example (paper&fabric) screen; - added `onKeyboardMoveInteractive` view callback and `onInteractive` handler for `useKeyboardHandler` hook; ### Android - added `KeyboardGestureArea` view; - changed target api to 33; - added `androidx.dynamicanimation:dynamicanimation-ktx` library; - added `InteractiveKeyboardProvider` which acts as a singleton to store keyboard state; - added `Interpolator` interface and `IosInterpolator` and `LinearInterpolator` implementation; ## 🤔 How Has This Been Tested? Tested manually on: - Pixel 7 Pro (Android 13, real device); - Pixel 3A (Android 13, emulator); ## 📸 Screenshots (if appropriate): |linear|iOS| |------|---| |