use iOS behaviour of expanding sheets when keyboard open
This commit is contained in:
+25
-1
@@ -8,6 +8,8 @@ import android.view.ViewStructure
|
|||||||
import android.view.Window
|
import android.view.Window
|
||||||
import android.view.accessibility.AccessibilityEvent
|
import android.view.accessibility.AccessibilityEvent
|
||||||
import android.widget.FrameLayout
|
import android.widget.FrameLayout
|
||||||
|
import androidx.core.view.ViewCompat
|
||||||
|
import androidx.core.view.WindowInsetsCompat
|
||||||
import androidx.core.view.WindowInsetsControllerCompat
|
import androidx.core.view.WindowInsetsControllerCompat
|
||||||
import androidx.core.view.allViews
|
import androidx.core.view.allViews
|
||||||
import com.facebook.react.bridge.LifecycleEventListener
|
import com.facebook.react.bridge.LifecycleEventListener
|
||||||
@@ -32,6 +34,7 @@ class BottomSheetView(
|
|||||||
|
|
||||||
private lateinit var dialogRootViewGroup: DialogRootViewGroup
|
private lateinit var dialogRootViewGroup: DialogRootViewGroup
|
||||||
private var eventDispatcher: EventDispatcher? = null
|
private var eventDispatcher: EventDispatcher? = null
|
||||||
|
private var isKeyboardVisible: Boolean = false
|
||||||
|
|
||||||
private val screenHeight =
|
private val screenHeight =
|
||||||
context.resources.displayMetrics.heightPixels
|
context.resources.displayMetrics.heightPixels
|
||||||
@@ -238,9 +241,26 @@ class BottomSheetView(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.isOpening = true
|
this.isOpening = true
|
||||||
dialog.show()
|
dialog.show()
|
||||||
this.dialog = dialog
|
this.dialog = dialog
|
||||||
|
|
||||||
|
ViewCompat.setOnApplyWindowInsetsListener(dialogRootViewGroup) { view, insets ->
|
||||||
|
val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
|
||||||
|
val bottomSheet = dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
|
||||||
|
val behavior = bottomSheet?.let { BottomSheetBehavior.from(it) }
|
||||||
|
|
||||||
|
val wasKeyboardVisible = isKeyboardVisible
|
||||||
|
isKeyboardVisible = imeVisible
|
||||||
|
|
||||||
|
if (imeVisible && behavior?.state == BottomSheetBehavior.STATE_HALF_EXPANDED) {
|
||||||
|
behavior.state = BottomSheetBehavior.STATE_EXPANDED
|
||||||
|
} else if (!imeVisible && wasKeyboardVisible) {
|
||||||
|
updateLayout()
|
||||||
|
}
|
||||||
|
insets
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateLayout() {
|
fun updateLayout() {
|
||||||
@@ -264,7 +284,11 @@ class BottomSheetView(
|
|||||||
val availableHeight = screenHeight - getStatusBarHeight() - getNavigationBarHeight()
|
val availableHeight = screenHeight - getStatusBarHeight() - getNavigationBarHeight()
|
||||||
val shouldBeExpanded = targetHeight >= availableHeight
|
val shouldBeExpanded = targetHeight >= availableHeight
|
||||||
|
|
||||||
if (shouldBeExpanded && behavior.state != BottomSheetBehavior.STATE_EXPANDED && !preventExpansion) {
|
if (isKeyboardVisible) {
|
||||||
|
if (behavior.state != BottomSheetBehavior.STATE_EXPANDED) {
|
||||||
|
behavior.state = BottomSheetBehavior.STATE_EXPANDED
|
||||||
|
}
|
||||||
|
} else if (shouldBeExpanded && behavior.state != BottomSheetBehavior.STATE_EXPANDED && !preventExpansion) {
|
||||||
behavior.state = BottomSheetBehavior.STATE_EXPANDED
|
behavior.state = BottomSheetBehavior.STATE_EXPANDED
|
||||||
} else if (!shouldBeExpanded && behavior.state != BottomSheetBehavior.STATE_HALF_EXPANDED) {
|
} else if (!shouldBeExpanded && behavior.state != BottomSheetBehavior.STATE_HALF_EXPANDED) {
|
||||||
behavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
|
behavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
|
||||||
|
|||||||
Reference in New Issue
Block a user