Skip to content

Commit cd0ad67

Browse files
committed
add dismissWithAnimation parameter to BottomSheetDialogProperties
1 parent 3420e66 commit cd0ad67

File tree

1 file changed

+42
-22
lines changed
  • bottomsheetdialog-compose/src/main/kotlin/com/holix/android/bottomsheetdialog/compose

1 file changed

+42
-22
lines changed

bottomsheetdialog-compose/src/main/kotlin/com/holix/android/bottomsheetdialog/compose/BottomSheetDialog.kt

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,29 @@ import androidx.lifecycle.ViewTreeViewModelStoreOwner
2424
import androidx.savedstate.findViewTreeSavedStateRegistryOwner
2525
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
2626
import com.google.android.material.bottomsheet.BottomSheetBehavior
27+
import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback
2728
import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_HIDDEN
2829
import com.google.android.material.bottomsheet.BottomSheetDialog
2930
import java.util.*
3031

3132

3233
/**
33-
* Properties used to customize the behavior of a [Dialog].
34+
* Properties used to customize the behavior of a [BottomSheetDialog].
3435
*
3536
* @property dismissOnBackPress whether the dialog can be dismissed by pressing the back button.
3637
* If true, pressing the back button will call onDismissRequest.
3738
* @property dismissOnClickOutside whether the dialog can be dismissed by clicking outside the
3839
* dialog's bounds. If true, clicking outside the dialog will call onDismissRequest.
40+
* @property dismissWithAnimation [BottomSheetDialog.setDismissWithAnimation]
3941
* @property securePolicy Policy for setting [WindowManager.LayoutParams.FLAG_SECURE] on the
4042
* dialog's window.
41-
* @property usePlatformDefaultWidth Whether the width of the dialog's content should be limited to
42-
* the platform default, which is smaller than the screen width.
43+
* @property navigationBarColor Color to apply to the navigationBar.
4344
*/
4445
@Immutable
4546
class BottomSheetDialogProperties constructor(
4647
val dismissOnBackPress: Boolean = true,
4748
val dismissOnClickOutside: Boolean = true,
49+
val dismissWithAnimation: Boolean = false,
4850
val securePolicy: SecureFlagPolicy = SecureFlagPolicy.Inherit,
4951
val navigationBarColor: Color = Color.Unspecified
5052
) {
@@ -55,6 +57,7 @@ class BottomSheetDialogProperties constructor(
5557

5658
if (dismissOnBackPress != other.dismissOnBackPress) return false
5759
if (dismissOnClickOutside != other.dismissOnClickOutside) return false
60+
if (dismissWithAnimation != other.dismissWithAnimation) return false
5861
if (securePolicy != other.securePolicy) return false
5962
if (navigationBarColor != other.navigationBarColor) return false
6063

@@ -64,25 +67,26 @@ class BottomSheetDialogProperties constructor(
6467
override fun hashCode(): Int {
6568
var result = dismissOnBackPress.hashCode()
6669
result = 31 * result + dismissOnClickOutside.hashCode()
70+
result = 31 * result + dismissWithAnimation.hashCode()
6771
result = 31 * result + securePolicy.hashCode()
6872
result = 31 * result + navigationBarColor.hashCode()
6973
return result
7074
}
7175
}
7276

7377
/**
74-
* Opens a dialog with the given content.
78+
* Opens a bottomsheet dialog with the given content.
7579
*
7680
* The dialog is visible as long as it is part of the composition hierarchy.
77-
* In order to let the user dismiss the Dialog, the implementation of [onDismissRequest] should
81+
* In order to let the user dismiss the BottomSheetDialog, the implementation of [onDismissRequest] should
7882
* contain a way to remove to remove the dialog from the composition hierarchy.
7983
*
8084
* Example usage:
8185
*
82-
* @sample androidx.compose.ui.samples.DialogSample
86+
* @sample com.holix.android.bottomsheetdialogcomposedemo.MainActivity
8387
*
8488
* @param onDismissRequest Executes when the user tries to dismiss the dialog.
85-
* @param properties [DialogProperties] for further customization of this dialog's behavior.
89+
* @param properties [BottomSheetDialogProperties] for further customization of this dialog's behavior.
8690
* @param content The content to be displayed inside the dialog.
8791
*/
8892
@Composable
@@ -137,7 +141,7 @@ fun BottomSheetDialog(
137141
}
138142

139143
/**
140-
* Provides the underlying window of a dialog.
144+
* Provides the underlying window of a bottomsheet dialog.
141145
*
142146
* Implemented by dialog's root layout.
143147
*/
@@ -185,10 +189,22 @@ private class BottomSheetDialogWrapper(
185189
ViewRootForInspector {
186190
private val bottomSheetDialogLayout: BottomSheetDialogLayout
187191

192+
private val bottomSheetCallbackForAnimation: BottomSheetCallback = object : BottomSheetCallback() {
193+
override fun onSlide(bottomSheet: View, slideOffset: Float) {
194+
}
195+
196+
override fun onStateChanged(bottomSheet: View, newState: Int) {
197+
if (newState == STATE_HIDDEN) {
198+
onDismissRequest()
199+
}
200+
}
201+
}
202+
188203
private val maxSupportedElevation = 30.dp
189204

190205
override val subCompositionView: AbstractComposeView get() = bottomSheetDialogLayout
191206

207+
192208
init {
193209
val window = window ?: error("Dialog has no window")
194210
window.requestFeature(Window.FEATURE_NO_TITLE)
@@ -215,18 +231,6 @@ private class BottomSheetDialogWrapper(
215231
}
216232
}
217233
}
218-
this.behavior.addBottomSheetCallback(
219-
object : BottomSheetBehavior.BottomSheetCallback() {
220-
override fun onSlide(bottomSheet: View, slideOffset: Float) {
221-
}
222-
223-
override fun onStateChanged(bottomSheet: View, newState: Int) {
224-
if (newState == STATE_HIDDEN) {
225-
onDismissRequest()
226-
}
227-
}
228-
}
229-
)
230234

231235
/**
232236
* Disables clipping for [this] and all its descendant [ViewGroup]s until we reach a
@@ -285,6 +289,15 @@ private class BottomSheetDialogWrapper(
285289
}
286290
}
287291

292+
override fun setDismissWithAnimation(dismissWithAnimation: Boolean) {
293+
super.setDismissWithAnimation(dismissWithAnimation)
294+
if (dismissWithAnimation) {
295+
behavior.addBottomSheetCallback(bottomSheetCallbackForAnimation)
296+
} else {
297+
behavior.removeBottomSheetCallback(bottomSheetCallbackForAnimation)
298+
}
299+
}
300+
288301
fun updateParameters(
289302
onDismissRequest: () -> Unit,
290303
properties: BottomSheetDialogProperties,
@@ -296,19 +309,26 @@ private class BottomSheetDialogWrapper(
296309
setLayoutDirection(layoutDirection)
297310
setCanceledOnTouchOutside(properties.dismissOnClickOutside)
298311
setNavigationBarColor(properties.navigationBarColor)
312+
dismissWithAnimation = properties.dismissWithAnimation
299313
}
300314

301315
fun disposeComposition() {
302316
bottomSheetDialogLayout.disposeComposition()
303317
}
304318

305319
override fun cancel() {
306-
onDismissRequest()
320+
if (properties.dismissWithAnimation) {
321+
// call setState(STATE_HIDDEN) -> onDismissRequest will be called in BottomSheetCallback
322+
super.cancel()
323+
} else {
324+
// dismiss with window animation
325+
onDismissRequest()
326+
}
307327
}
308328

309329
override fun onBackPressed() {
310330
if (properties.dismissOnBackPress) {
311-
onDismissRequest()
331+
cancel()
312332
}
313333
}
314334
}

0 commit comments

Comments
 (0)