Skip to content

Commit

Permalink
Remove fullscreen control on BottomSheetExposer.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
abalta committed Apr 3, 2023
1 parent c710ad1 commit 749f575
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class ViewExtDemoActivity : AppCompatActivity() {
btnBottomSheet.click {
bottomSheetOf(
supportFragmentManager,
false,
30,
40,
dialogTheme = R.style.FullScreenDialog,
viewHolderCreator = { layoutInflater, sheet ->
val binding = DialogBottomSheetBinding.inflate(layoutInflater)
binding.root
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/res/layout/dialog_bottom_sheet.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">

<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
Expand All @@ -13,4 +18,13 @@
app:layout_constraintBottom_toBottomOf="parent"
android:text="This is Bottom Sheet Dialog Fragment"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/btn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,38 @@ import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.core.view.ViewCompat
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import com.mobven.extension.R
import com.mobven.extension.heightPixels

class BottomSheetExposer(
private val isFullScreen: Boolean = true,
private val heightMultiplier: Int = 50,
private val dialogTheme: Int,
private val viewHolderCreator: (inflater: LayoutInflater, sheet: BottomSheetExposer) -> View?
) : BottomSheetDialogFragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, dialogTheme)
}

private fun setupFullHeight(bottomSheetDialog: BottomSheetDialog) {
val bottomSheet =
bottomSheetDialog.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?
bottomSheet?.let { frame ->
val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(frame)
frame.layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.skipCollapsed = true
}
}

private fun skipCollapsed(bottomSheetDialog: BottomSheetDialog) {
val bottomSheet =
bottomSheetDialog.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?
bottomSheet?.let {
val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(it)
it.minimumHeight = requireActivity().heightPixels() * heightMultiplier / 100
frame.layoutParams.height = requireActivity().heightPixels() * heightMultiplier / 100
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.skipCollapsed = true
}
}

override fun onStart() {
super.onStart()
if (isFullScreen) {
val sheetContainer = requireView().parent as ViewGroup
sheetContainer.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
}
val sheetContainer = requireView().parent as ViewGroup
sheetContainer.layoutParams.height = requireActivity().heightPixels() * heightMultiplier / 100

}

override fun onCreateView(
Expand All @@ -65,32 +50,22 @@ class BottomSheetExposer(
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), theme)
if (isFullScreen) {
dialog.setOnShowListener {
val bottomSheetDialog = it as BottomSheetDialog
setupFullHeight(bottomSheetDialog)
}
} else {
dialog.setOnShowListener {
val bottomSheetDialog = it as BottomSheetDialog
skipCollapsed(bottomSheetDialog)
}
val dialog = BottomSheetDialog(requireContext(), dialogTheme)
dialog.setOnShowListener {
val bottomSheetDialog = it as BottomSheetDialog
setupFullHeight(bottomSheetDialog)
}

return dialog
}
}

fun bottomSheetOf(
fragmentManager: FragmentManager,
isFullScreen: Boolean = true,
heightMultiplier: Int = 50,
dialogTheme: Int = android.R.style.Theme_Material_NoActionBar_TranslucentDecor,
viewHolderCreator: (inflater: LayoutInflater, sheet: BottomSheetExposer) -> View?
) {
BottomSheetExposer(
isFullScreen,
heightMultiplier,
dialogTheme,
viewHolderCreator
Expand Down
9 changes: 9 additions & 0 deletions extension/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="RoundedShape" parent="ShapeAppearance.Material3.LargeComponent">
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerFamilyTopRight">rounded</item>
<item name="cornerSizeTopLeft">10dp</item>
<item name="cornerSizeTopRight">10dp</item>
</style>
</resources>

0 comments on commit 749f575

Please sign in to comment.