11package com.applozic.mobicomkit.uiwidgets.utils
22
3+ import android.animation.ValueAnimator
34import android.view.View
45import android.view.ViewGroup
6+ import android.view.animation.DecelerateInterpolator
7+ import androidx.core.animation.doOnEnd
58import androidx.core.view.ViewCompat
69import androidx.core.view.WindowInsetsCompat
710import androidx.core.view.updateLayoutParams
811import androidx.core.view.updatePadding
912
1013/* *
11- * A utility object that helps manage system insets (like status bar, navigation bar, etc.)
14+ * A utility object that helps manage system insets (like status bar, navigation bar, keyboard, etc.)
1215 * by configuring padding or margin adjustments based on the system window insets.
16+ * Includes support for keyboard animations and edge-to-edge display.
1317 */
1418object InsetHelper {
1519
@@ -31,6 +35,12 @@ object InsetHelper {
3135 @JvmField
3236 val navigationTypeMask = WindowInsetsCompat .Type .navigationBars()
3337
38+ /* *
39+ * [imeTypeMask] is a type mask for the software keyboard.
40+ */
41+ @JvmField
42+ val imeTypeMask = WindowInsetsCompat .Type .ime()
43+
3444 /* *
3545 * Configures the system insets with status bar to update either
3646 * padding or margin of the target view.
@@ -53,7 +63,18 @@ object InsetHelper {
5363 bottom : Int = -1,
5464 isPadding : Boolean = true
5565 ) {
56- configureInset(view, systemTypeMask, 0 , 0 , top, bottom, isPadding)
66+ configureInset(view, systemTypeMask, 0 , 0 , top, bottom, isPadding)
67+ }
68+
69+ @JvmStatic
70+ fun configureSystemInsetsWithKeyboard (
71+ view : View ,
72+ top : Int = -1,
73+ bottom : Int = -1,
74+ isPadding : Boolean = true,
75+ adjustForKeyboard : (isVisible: Boolean , keyboardHeight: Int ) -> Unit
76+ ) {
77+ configureInset(view, systemTypeMask, 0 , 0 , top, bottom, isPadding, adjustForKeyboard)
5778 }
5879
5980 /* *
@@ -73,6 +94,7 @@ object InsetHelper {
7394 * @param top The padding or margin to apply on the top side. Default is -1 (which means apply the inset value).
7495 * @param bottom The padding or margin to apply on the bottom side. Default is -1 (which means apply the inset value).
7596 * @param isPadding If true, the function adjusts the padding of the view. If false, it adjusts the margins of the view. Default is true.
97+ * @param adjustForKeyboard the function will adjust the view when the keyboard appears/disappears.
7698 */
7799 @JvmStatic
78100 fun configureInset (
@@ -82,23 +104,43 @@ object InsetHelper {
82104 right : Int = -1,
83105 top : Int = -1,
84106 bottom : Int = -1,
85- isPadding : Boolean = true
107+ isPadding : Boolean = true,
108+ adjustForKeyboard : ((isVisible: Boolean , keyboardHeight: Int ) -> Unit )? = null
86109 ) {
110+ var lastKeyboardHeight = 0
111+ var wasKeyboardVisible = false
112+
87113 ViewCompat .setOnApplyWindowInsetsListener(view) { targetView, windowInsets ->
88114 val insets = windowInsets.getInsets(typeMask)
115+ val imeInsets = windowInsets.getInsets(imeTypeMask)
116+ val isKeyboardVisible = windowInsets.isVisible(WindowInsetsCompat .Type .ime())
117+
118+ // Combine insets if adjusting for keyboard
119+ val bottomInset = if (adjustForKeyboard != null && imeInsets.bottom > 0 ) {
120+ imeInsets.bottom
121+ } else {
122+ insets.bottom
123+ }
124+
125+ if (isKeyboardVisible != wasKeyboardVisible) {
126+ adjustForKeyboard?.invoke(isKeyboardVisible, lastKeyboardHeight)
127+ wasKeyboardVisible = isKeyboardVisible
128+ lastKeyboardHeight = resolveInset(bottom, bottomInset)
129+ }
130+
89131 if (isPadding) {
90132 targetView.updatePadding(
91133 left = resolveInset(left, insets.left),
92134 right = resolveInset(right, insets.right),
93135 top = resolveInset(top, insets.top),
94- bottom = resolveInset(bottom, insets.bottom )
136+ bottom = resolveInset(bottom, bottomInset )
95137 )
96138 } else {
97139 targetView.updateLayoutParams<ViewGroup .MarginLayoutParams > {
98140 leftMargin = resolveInset(left, insets.left)
99141 rightMargin = resolveInset(right, insets.right)
100- bottomMargin = resolveInset(bottom, insets.bottom)
101142 topMargin = resolveInset(top, insets.top)
143+ bottomMargin = resolveInset(bottom, bottomInset)
102144 }
103145 }
104146 // Return CONSUMED to prevent further insets handling by the system
0 commit comments