Skip to content

Commit a67b35c

Browse files
committed
add BottomSheetBehaviorProperties to set BottomSheetBehavior.
docs for BottomSheetBehavior : https://developer.android.com/reference/com/google/android/material/bottomsheet/BottomSheetBehavior
1 parent bc21ff6 commit a67b35c

File tree

1 file changed

+117
-4
lines changed
  • bottomsheetdialog-compose/src/main/kotlin/com/holix/android/bottomsheetdialog/compose

1 file changed

+117
-4
lines changed

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

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import android.content.Context
55
import android.graphics.Outline
66
import android.os.Build
77
import android.view.*
8+
import androidx.annotation.FloatRange
9+
import androidx.annotation.IntRange
10+
import androidx.annotation.Px
811
import androidx.compose.runtime.*
912
import androidx.compose.runtime.saveable.rememberSaveable
1013
import androidx.compose.ui.Modifier
@@ -25,14 +28,13 @@ import androidx.lifecycle.ViewTreeLifecycleOwner
2528
import androidx.lifecycle.ViewTreeViewModelStoreOwner
2629
import androidx.savedstate.findViewTreeSavedStateRegistryOwner
2730
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
28-
import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback
29-
import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_HIDDEN
31+
import com.google.android.material.bottomsheet.BottomSheetBehavior.*
3032
import com.google.android.material.bottomsheet.BottomSheetDialog
3133
import java.util.*
3234

3335

3436
/**
35-
* Properties used to customize the behavior of a [BottomSheetDialog].
37+
* Properties used to customize [BottomSheetDialog].
3638
*
3739
* @property dismissOnBackPress whether the dialog can be dismissed by pressing the back button.
3840
* If true, pressing the back button will call onDismissRequest.
@@ -49,7 +51,8 @@ class BottomSheetDialogProperties constructor(
4951
val dismissOnClickOutside: Boolean = true,
5052
val dismissWithAnimation: Boolean = false,
5153
val securePolicy: SecureFlagPolicy = SecureFlagPolicy.Inherit,
52-
val navigationBarProperties: NavigationBarProperties = NavigationBarProperties()
54+
val navigationBarProperties: NavigationBarProperties = NavigationBarProperties(),
55+
val behaviorProperties: BottomSheetBehaviorProperties = BottomSheetBehaviorProperties()
5356
) {
5457

5558
@Deprecated("Use NavigationBarProperties(color = navigationBarColor) instead")
@@ -76,6 +79,7 @@ class BottomSheetDialogProperties constructor(
7679
if (dismissWithAnimation != other.dismissWithAnimation) return false
7780
if (securePolicy != other.securePolicy) return false
7881
if (navigationBarProperties != other.navigationBarProperties) return false
82+
if (behaviorProperties != other.behaviorProperties) return false
7983

8084
return true
8185
}
@@ -86,10 +90,99 @@ class BottomSheetDialogProperties constructor(
8690
result = 31 * result + dismissWithAnimation.hashCode()
8791
result = 31 * result + securePolicy.hashCode()
8892
result = 31 * result + navigationBarProperties.hashCode()
93+
result = 31 * result + behaviorProperties.hashCode()
8994
return result
9095
}
9196
}
9297

98+
/**
99+
* Properties used to set [com.google.android.material.bottomsheet.BottomSheetBehavior].
100+
*
101+
* @see [com.google.android.material.bottomsheet.BottomSheetBehavior]
102+
*/
103+
@Immutable
104+
class BottomSheetBehaviorProperties(
105+
val state: BottomSheetDialogState = BottomSheetDialogState.Collapsed,
106+
val maxWidth: Size = Size.NotSet,
107+
val maxHeight: Size = Size.NotSet,
108+
val isDraggable: Boolean = true,
109+
@IntRange(from = 0)
110+
val expandedOffset: Int = 0,
111+
@FloatRange(from = 0.0, to = 1.0, fromInclusive = false, toInclusive = false)
112+
val halfExpandedRatio: Float = 0.5F,
113+
val isHideable: Boolean = true,
114+
val peekHeight: PeekHeight = PeekHeight.Auto,
115+
val isFitToContents: Boolean = true,
116+
val skipCollapsed: Boolean = false,
117+
val isGestureInsetBottomIgnored: Boolean = false
118+
) {
119+
override fun equals(other: Any?): Boolean {
120+
if (this === other) return true
121+
if (other !is BottomSheetBehaviorProperties) return false
122+
123+
if (state != other.state) return false
124+
if (maxWidth != other.maxWidth) return false
125+
if (maxHeight != other.maxHeight) return false
126+
if (isDraggable != other.isDraggable) return false
127+
if (expandedOffset != other.expandedOffset) return false
128+
if (halfExpandedRatio != other.halfExpandedRatio) return false
129+
if (isHideable != other.isHideable) return false
130+
if (peekHeight != other.peekHeight) return false
131+
if (isFitToContents != other.isFitToContents) return false
132+
if (skipCollapsed != other.skipCollapsed) return false
133+
if (isGestureInsetBottomIgnored != other.isGestureInsetBottomIgnored) return false
134+
135+
return true
136+
}
137+
138+
override fun hashCode(): Int {
139+
var result = state.hashCode()
140+
result = 31 * result + maxWidth.hashCode()
141+
result = 31 * result + maxHeight.hashCode()
142+
result = 31 * result + isDraggable.hashCode()
143+
result = 31 * result + expandedOffset.hashCode()
144+
result = 31 * result + halfExpandedRatio.hashCode()
145+
result = 31 * result + isHideable.hashCode()
146+
result = 31 * result + peekHeight.hashCode()
147+
result = 31 * result + isFitToContents.hashCode()
148+
result = 31 * result + skipCollapsed.hashCode()
149+
result = 31 * result + isGestureInsetBottomIgnored.hashCode()
150+
return result
151+
}
152+
153+
@Immutable
154+
enum class BottomSheetDialogState {
155+
@Stable
156+
Expanded,
157+
@Stable
158+
HalfExpanded,
159+
@Stable
160+
Collapsed,
161+
@Stable
162+
Hidden
163+
}
164+
165+
@JvmInline
166+
@Immutable
167+
value class Size(@Px val value: Int) {
168+
169+
companion object {
170+
@Stable
171+
val NotSet = Size(-1)
172+
}
173+
}
174+
175+
@JvmInline
176+
@Stable
177+
value class PeekHeight(val value: Int) {
178+
179+
companion object {
180+
@Stable
181+
val Auto = PeekHeight(PEEK_HEIGHT_AUTO)
182+
}
183+
}
184+
}
185+
93186
/**
94187
* Properties used to customize navigationBar.
95188
@@ -391,6 +484,25 @@ private class BottomSheetDialogWrapper(
391484
}
392485
}
393486

487+
private fun setBehaviorProperties(behaviorProperties: BottomSheetBehaviorProperties) {
488+
this.behavior.state = when (behaviorProperties.state) {
489+
BottomSheetBehaviorProperties.BottomSheetDialogState.Expanded -> STATE_EXPANDED
490+
BottomSheetBehaviorProperties.BottomSheetDialogState.Collapsed -> STATE_COLLAPSED
491+
BottomSheetBehaviorProperties.BottomSheetDialogState.HalfExpanded -> STATE_HALF_EXPANDED
492+
BottomSheetBehaviorProperties.BottomSheetDialogState.Hidden -> STATE_HIDDEN
493+
}
494+
this.behavior.maxWidth = behaviorProperties.maxWidth.value
495+
this.behavior.maxHeight = behaviorProperties.maxHeight.value
496+
this.behavior.isDraggable = behaviorProperties.isDraggable
497+
this.behavior.expandedOffset = behaviorProperties.expandedOffset
498+
this.behavior.halfExpandedRatio = behaviorProperties.halfExpandedRatio
499+
this.behavior.isHideable = behaviorProperties.isHideable
500+
this.behavior.peekHeight = behaviorProperties.peekHeight.value
501+
this.behavior.isFitToContents = behaviorProperties.isFitToContents
502+
this.behavior.skipCollapsed = behaviorProperties.skipCollapsed
503+
this.behavior.isGestureInsetBottomIgnored = behaviorProperties.isGestureInsetBottomIgnored
504+
}
505+
394506
fun updateParameters(
395507
onDismissRequest: () -> Unit,
396508
properties: BottomSheetDialogProperties,
@@ -402,6 +514,7 @@ private class BottomSheetDialogWrapper(
402514
setLayoutDirection(layoutDirection)
403515
setCanceledOnTouchOutside(properties.dismissOnClickOutside)
404516
setNavigationBarProperties(properties.navigationBarProperties)
517+
setBehaviorProperties(properties.behaviorProperties)
405518
dismissWithAnimation = properties.dismissWithAnimation
406519
}
407520

0 commit comments

Comments
 (0)