Skip to content

Commit 0d12ef8

Browse files
committed
improve sample
able to set BottomSheetBehaviorProperties in sample app
1 parent 65cac63 commit 0d12ef8

File tree

5 files changed

+425
-47
lines changed

5 files changed

+425
-47
lines changed

app/src/main/kotlin/com/holix/android/bottomsheetdialogcomposedemo/MainActivity.kt

Lines changed: 185 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@ import androidx.compose.material.*
1313
import androidx.compose.runtime.getValue
1414
import androidx.compose.runtime.mutableStateOf
1515
import androidx.compose.runtime.remember
16-
import androidx.compose.runtime.saveable.rememberSaveable
1716
import androidx.compose.runtime.setValue
1817
import androidx.compose.ui.Modifier
1918
import androidx.compose.ui.graphics.luminance
2019
import androidx.compose.ui.unit.dp
20+
import com.holix.android.bottomsheetdialog.compose.BottomSheetBehaviorProperties
2121
import com.holix.android.bottomsheetdialog.compose.BottomSheetDialog
2222
import com.holix.android.bottomsheetdialog.compose.BottomSheetDialogProperties
2323
import com.holix.android.bottomsheetdialog.compose.NavigationBarProperties
24-
import com.holix.android.bottomsheetdialogcomposedemo.preferences.BooleanPreference
25-
import com.holix.android.bottomsheetdialogcomposedemo.preferences.ColorPreference
26-
import com.holix.android.bottomsheetdialogcomposedemo.preferences.PreferenceCategory
27-
import com.holix.android.bottomsheetdialogcomposedemo.preferences.SingleChoicePreference
24+
import com.holix.android.bottomsheetdialogcomposedemo.preferences.*
2825

2926
class MainActivity : AppCompatActivity() {
3027
override fun onCreate(savedInstanceState: Bundle?) {
@@ -37,30 +34,64 @@ class MainActivity : AppCompatActivity() {
3734
lightColors()
3835
}
3936
) {
40-
var showBottomSheetDialog by rememberSaveable {
37+
var showBottomSheetDialog by remember {
4138
mutableStateOf(false)
4239
}
4340
// BottomSheetProperties
44-
var dismissOnBackPress by rememberSaveable {
41+
var dismissOnBackPress by remember {
4542
mutableStateOf(true)
4643
}
47-
var dismissOnClickOutside by rememberSaveable {
44+
var dismissOnClickOutside by remember {
4845
mutableStateOf(true)
4946
}
50-
var dismissWithAnimation by rememberSaveable {
47+
var dismissWithAnimation by remember {
5148
mutableStateOf(false)
5249
}
5350
// NavigationBarProperties
5451
val surfaceColor = MaterialTheme.colors.surface
5552
var navigationBarColor by remember(surfaceColor) {
5653
mutableStateOf(surfaceColor)
5754
}
58-
var darkIcons by rememberSaveable {
55+
var darkIcons by remember {
5956
mutableStateOf(DarkIconsValue.Default)
6057
}
61-
var navigationBarContrastEnforced by rememberSaveable {
58+
var navigationBarContrastEnforced by remember {
6259
mutableStateOf(true)
6360
}
61+
// BottomSheetBehaviorProperties
62+
var state by remember {
63+
mutableStateOf(BottomSheetBehaviorProperties.State.Collapsed)
64+
}
65+
var maxWidth by remember {
66+
mutableStateOf(BottomSheetBehaviorProperties.Size.NotSet)
67+
}
68+
var maxHeight by remember {
69+
mutableStateOf(BottomSheetBehaviorProperties.Size.NotSet)
70+
}
71+
var isDraggable by remember {
72+
mutableStateOf(true)
73+
}
74+
var expandedOffset by remember {
75+
mutableStateOf(0)
76+
}
77+
var halfExpandedRatio by remember {
78+
mutableStateOf(0.5F)
79+
}
80+
var isHideable by remember {
81+
mutableStateOf(true)
82+
}
83+
var peekHeight by remember {
84+
mutableStateOf(BottomSheetBehaviorProperties.PeekHeight.Auto)
85+
}
86+
var isFitToContents by remember {
87+
mutableStateOf(true)
88+
}
89+
var skipCollapsed by remember {
90+
mutableStateOf(false)
91+
}
92+
var isGestureInsetBottomIgnored by remember {
93+
mutableStateOf(false)
94+
}
6495
if (showBottomSheetDialog) {
6596
BottomSheetDialog(
6697
onDismissRequest = {
@@ -79,6 +110,19 @@ class MainActivity : AppCompatActivity() {
79110
DarkIconsValue.False -> false
80111
},
81112
navigationBarContrastEnforced = navigationBarContrastEnforced
113+
),
114+
behaviorProperties = BottomSheetBehaviorProperties(
115+
state = state,
116+
maxWidth = maxWidth,
117+
maxHeight = maxHeight,
118+
isDraggable = isDraggable,
119+
expandedOffset = expandedOffset,
120+
halfExpandedRatio = halfExpandedRatio,
121+
isHideable = isHideable,
122+
peekHeight = peekHeight,
123+
isFitToContents = isFitToContents,
124+
skipCollapsed = skipCollapsed,
125+
isGestureInsetBottomIgnored = isGestureInsetBottomIgnored
82126
)
83127
)
84128
) {
@@ -93,7 +137,7 @@ class MainActivity : AppCompatActivity() {
93137
verticalArrangement = Arrangement.spacedBy(16.dp)
94138
) {
95139
Text(text = "Title", style = MaterialTheme.typography.h5)
96-
repeat(5) { index ->
140+
repeat(30) { index ->
97141
Text(
98142
text = "Item $index",
99143
style = MaterialTheme.typography.body1
@@ -154,8 +198,7 @@ class MainActivity : AppCompatActivity() {
154198
value.name,
155199
when (value) {
156200
DarkIconsValue.Default ->
157-
"Default (color.luminance() > 0.5F) = " +
158-
"${navigationBarColor.luminance() > 0.5F}"
201+
"Default (color.luminance() > 0.5F)"
159202
else -> value.name
160203
}
161204
)
@@ -168,6 +211,134 @@ class MainActivity : AppCompatActivity() {
168211
},
169212
label = "navigationBarContrastEnforced"
170213
)
214+
PreferenceCategory("BottomSheetBehaviorProperties")
215+
SingleChoicePreference(
216+
value = state.name,
217+
onValueChange = {
218+
state = BottomSheetBehaviorProperties.State.valueOf(it)
219+
},
220+
label = "state",
221+
options = BottomSheetBehaviorProperties.State.values().map { value ->
222+
Pair(value.name, value.name)
223+
}
224+
)
225+
IntPreference(
226+
value = maxWidth.value,
227+
onValueChange = {
228+
maxWidth = BottomSheetBehaviorProperties.Size(it)
229+
},
230+
label = "maxWidth (px)",
231+
helpText = "Default: not set (-1)",
232+
description = { maxWidth ->
233+
if (maxWidth == -1) {
234+
"not set (-1)"
235+
} else {
236+
"${maxWidth}px"
237+
}
238+
},
239+
validate = { maxWidth -> maxWidth == -1 || maxWidth > 0 }
240+
)
241+
IntPreference(
242+
value = maxHeight.value,
243+
onValueChange = {
244+
maxHeight = BottomSheetBehaviorProperties.Size(it)
245+
},
246+
label = "maxHeight (px)",
247+
helpText = "Default: not set (-1)",
248+
description = { maxHeight ->
249+
if (maxHeight == -1) {
250+
"not set (-1)"
251+
} else {
252+
"${maxHeight}px"
253+
}
254+
},
255+
validate = { maxHeight -> maxHeight == -1 || maxHeight > 0 }
256+
)
257+
BooleanPreference(
258+
value = isDraggable,
259+
onValueChange = {
260+
isDraggable = it
261+
},
262+
label = "isDraggable"
263+
)
264+
IntPreference(
265+
value = expandedOffset,
266+
onValueChange = {
267+
expandedOffset = it
268+
},
269+
label = "expandedOffset (px)",
270+
helpText = "expandedOffset >= 0",
271+
description = { expandedOffset ->
272+
if (expandedOffset == 0) {
273+
"Default (0)"
274+
} else {
275+
"${expandedOffset}px"
276+
}
277+
},
278+
validate = { expandedOffset -> expandedOffset >= 0 }
279+
)
280+
FloatPreference(
281+
value = halfExpandedRatio,
282+
onValueChange = {
283+
halfExpandedRatio = it
284+
},
285+
label = "halfExpandedRatio",
286+
helpText = "0 < halfExpandedRatio < 1",
287+
description = { halfExpandedRatio ->
288+
if (halfExpandedRatio == 0.5F) {
289+
"Default (0.5)"
290+
} else {
291+
"$halfExpandedRatio"
292+
}
293+
},
294+
validate = { halfExpandedRatio ->
295+
0 < halfExpandedRatio && halfExpandedRatio < 1
296+
}
297+
)
298+
BooleanPreference(
299+
value = isHideable,
300+
onValueChange = {
301+
isHideable = it
302+
},
303+
label = "isHideable"
304+
)
305+
IntPreference(
306+
value = peekHeight.value,
307+
onValueChange = {
308+
peekHeight = BottomSheetBehaviorProperties.PeekHeight(it)
309+
},
310+
label = "peekHeight (px)",
311+
helpText = "peekHeight > 0",
312+
description = { peekHeight ->
313+
if (peekHeight == -1) {
314+
"Auto (-1)"
315+
} else {
316+
"${peekHeight}px"
317+
}
318+
},
319+
validate = { peekHeight -> peekHeight == -1 || peekHeight > 0 }
320+
)
321+
BooleanPreference(
322+
value = isFitToContents,
323+
onValueChange = {
324+
isFitToContents = it
325+
},
326+
label = "isFitToContents"
327+
)
328+
BooleanPreference(
329+
value = skipCollapsed,
330+
onValueChange = {
331+
skipCollapsed = it
332+
},
333+
label = "skipCollapsed"
334+
)
335+
BooleanPreference(
336+
value = isGestureInsetBottomIgnored,
337+
onValueChange = {
338+
isGestureInsetBottomIgnored = it
339+
},
340+
label = "isGestureInsetBottomIgnored"
341+
)
171342
}
172343
Button(
173344
modifier = Modifier

app/src/main/kotlin/com/holix/android/bottomsheetdialogcomposedemo/preferences/ColorPreference.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package com.holix.android.bottomsheetdialogcomposedemo.preferences
22

33
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.clickable
45
import androidx.compose.foundation.layout.*
56
import androidx.compose.foundation.shape.RoundedCornerShape
67
import androidx.compose.material.Button
78
import androidx.compose.material.MaterialTheme
89
import androidx.compose.material.Surface
910
import androidx.compose.material.Text
1011
import androidx.compose.runtime.*
11-
import androidx.compose.runtime.saveable.rememberSaveable
1212
import androidx.compose.ui.Alignment
1313
import androidx.compose.ui.Modifier
1414
import androidx.compose.ui.draw.clip
1515
import androidx.compose.ui.graphics.Color
1616
import androidx.compose.ui.graphics.toArgb
1717
import androidx.compose.ui.text.font.FontWeight
1818
import androidx.compose.ui.unit.dp
19-
import androidx.compose.ui.window.Dialog
2019
import com.github.skydoves.colorpicker.compose.*
20+
import com.holix.android.bottomsheetdialog.compose.BottomSheetBehaviorProperties
21+
import com.holix.android.bottomsheetdialog.compose.BottomSheetDialog
22+
import com.holix.android.bottomsheetdialog.compose.BottomSheetDialogProperties
2123

2224
@Composable
2325
fun ColorPreference(
@@ -27,15 +29,21 @@ fun ColorPreference(
2729
) {
2830
val surfaceColor = MaterialTheme.colors.surface
2931
val currentOnValueChange by rememberUpdatedState(onValueChange)
30-
var showColorPickerDialog by rememberSaveable {
32+
var showColorPickerDialog by remember {
3133
mutableStateOf(false)
3234
}
3335
if (showColorPickerDialog) {
3436
var targetColor by remember {
3537
mutableStateOf(value)
3638
}
37-
Dialog(
38-
onDismissRequest = { showColorPickerDialog = false }
39+
BottomSheetDialog(
40+
onDismissRequest = { showColorPickerDialog = false },
41+
properties = BottomSheetDialogProperties(
42+
behaviorProperties = BottomSheetBehaviorProperties(
43+
state = BottomSheetBehaviorProperties.State.Expanded,
44+
skipCollapsed = true
45+
)
46+
)
3947
) {
4048
Surface {
4149
Column(
@@ -91,20 +99,15 @@ fun ColorPreference(
9199
Row(
92100
modifier = Modifier
93101
.fillMaxWidth()
94-
.padding(16.dp, 8.dp),
102+
.clickable { showColorPickerDialog = true }
103+
.padding(16.dp),
95104
verticalAlignment = Alignment.CenterVertically,
96105
horizontalArrangement = Arrangement.spacedBy(8.dp)
97106
) {
98107
Text(text = label, fontWeight = FontWeight.Bold)
99108
when (value) {
100109
MaterialTheme.colors.surface -> {
101110
Text("Default - MaterialTheme.colors.surface")
102-
Spacer(modifier = Modifier.weight(1F))
103-
Button(
104-
onClick = { showColorPickerDialog = true }
105-
) {
106-
Text("Set")
107-
}
108111
}
109112
else -> {
110113
Box(

0 commit comments

Comments
 (0)