|
1 | 1 | package com.lodev09.truesheet.utils |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import android.os.Build |
4 | 5 | import android.view.View |
5 | 6 | import android.view.inputmethod.InputMethodManager |
6 | 7 | import androidx.core.view.ViewCompat |
| 8 | +import androidx.core.view.WindowInsetsAnimationCompat |
7 | 9 | import androidx.core.view.WindowInsetsCompat |
8 | 10 | import com.facebook.react.uimanager.ThemedReactContext |
9 | 11 |
|
10 | 12 | object KeyboardUtils { |
11 | 13 |
|
| 14 | + /** |
| 15 | + * Checks if the soft keyboard is currently visible. |
| 16 | + */ |
| 17 | + fun isKeyboardVisible(reactContext: ThemedReactContext): Boolean { |
| 18 | + val rootView = reactContext.currentActivity?.window?.decorView?.rootView ?: return false |
| 19 | + return isKeyboardVisible(rootView) |
| 20 | + } |
| 21 | + |
| 22 | + private fun isKeyboardVisible(view: View): Boolean { |
| 23 | + val insets = ViewCompat.getRootWindowInsets(view) ?: return false |
| 24 | + return insets.isVisible(WindowInsetsCompat.Type.ime()) |
| 25 | + } |
| 26 | + |
12 | 27 | /** |
13 | 28 | * Dismisses the soft keyboard if currently shown. |
14 | 29 | */ |
15 | 30 | fun dismiss(reactContext: ThemedReactContext) { |
16 | | - val imm = reactContext.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager |
17 | | - reactContext.currentActivity?.currentFocus?.let { focusedView -> |
18 | | - imm?.hideSoftInputFromWindow(focusedView.windowToken, 0) |
| 31 | + val rootView = reactContext.currentActivity?.window?.decorView?.rootView ?: return |
| 32 | + dismiss(rootView, null) |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Dismisses the soft keyboard with an optional callback when the animation completes. |
| 37 | + */ |
| 38 | + fun dismiss(view: View, onComplete: (() -> Unit)?) { |
| 39 | + val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager |
| 40 | + val focusedView = view.rootView.findFocus() |
| 41 | + |
| 42 | + if (focusedView == null || !isKeyboardVisible(view)) { |
| 43 | + onComplete?.invoke() |
| 44 | + return |
19 | 45 | } |
| 46 | + |
| 47 | + if (onComplete != null) { |
| 48 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 49 | + ViewCompat.setWindowInsetsAnimationCallback( |
| 50 | + view, |
| 51 | + object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE) { |
| 52 | + override fun onProgress( |
| 53 | + insets: WindowInsetsCompat, |
| 54 | + runningAnimations: List<WindowInsetsAnimationCompat> |
| 55 | + ): WindowInsetsCompat = insets |
| 56 | + |
| 57 | + override fun onEnd(animation: WindowInsetsAnimationCompat) { |
| 58 | + ViewCompat.setWindowInsetsAnimationCallback(view, null) |
| 59 | + onComplete() |
| 60 | + } |
| 61 | + } |
| 62 | + ) |
| 63 | + } else { |
| 64 | + view.postDelayed({ onComplete() }, 120) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + imm?.hideSoftInputFromWindow(focusedView.windowToken, 0) |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Shows the soft keyboard for the given view. |
| 73 | + */ |
| 74 | + fun show(view: View) { |
| 75 | + val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager |
| 76 | + imm?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) |
20 | 77 | } |
21 | 78 |
|
22 | 79 | /** |
|
0 commit comments