-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSliderPreference.kt
More file actions
163 lines (158 loc) · 5.85 KB
/
SliderPreference.kt
File metadata and controls
163 lines (158 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhanghai.compose.preference
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.material3.Slider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableFloatState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
public inline fun LazyListScope.sliderPreference(
key: String,
defaultValue: Float,
crossinline title: @Composable (Float) -> Unit,
modifier: Modifier = Modifier.fillMaxWidth(),
crossinline rememberState: @Composable () -> MutableState<Float> = {
rememberPreferenceState(key, defaultValue)
},
valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
valueSteps: Int = 0,
crossinline rememberSliderState: @Composable (Float) -> MutableFloatState = {
remember { mutableFloatStateOf(it) }
},
crossinline enabled: (Float) -> Boolean = { true },
noinline icon: @Composable ((Float) -> Unit)? = null,
noinline summary: @Composable ((Float) -> Unit)? = null,
noinline valueText: @Composable ((Float) -> Unit)? = null,
) {
item(key = key, contentType = "SliderPreference") {
val state = rememberState()
val value by state
val sliderState = rememberSliderState(value)
val sliderValue by sliderState
SliderPreference(
state = state,
title = { title(sliderValue) },
modifier = modifier,
valueRange = valueRange,
valueSteps = valueSteps,
sliderState = sliderState,
enabled = enabled(value),
icon = icon?.let { { it(sliderValue) } },
summary = summary?.let { { it(sliderValue) } },
valueText = valueText?.let { { it(sliderValue) } },
)
}
}
@Composable
public fun SliderPreference(
state: MutableState<Float>,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
valueSteps: Int = 0,
sliderState: MutableFloatState = remember { mutableFloatStateOf(state.value) },
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
summary: @Composable (() -> Unit)? = null,
valueText: @Composable (() -> Unit)? = null,
) {
var value by state
var sliderValue by sliderState
SliderPreference(
value = value,
onValueChange = { value = it },
sliderValue = sliderValue,
onSliderValueChange = { sliderValue = it },
title = title,
modifier = modifier,
valueRange = valueRange,
valueSteps = valueSteps,
enabled = enabled,
icon = icon,
summary = summary,
valueText = valueText,
)
}
@Composable
public fun SliderPreference(
value: Float,
onValueChange: (Float) -> Unit,
sliderValue: Float,
onSliderValueChange: (Float) -> Unit,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
valueSteps: Int = 0,
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
summary: @Composable (() -> Unit)? = null,
valueText: @Composable (() -> Unit)? = null,
) {
var lastValue by remember { mutableFloatStateOf(value) }
SideEffect {
if (value != lastValue) {
onSliderValueChange(value)
lastValue = value
}
}
Preference(
title = title,
modifier = modifier,
enabled = enabled,
icon = icon,
summary = {
Column {
summary?.invoke()
Row(verticalAlignment = Alignment.CenterVertically) {
// onValueChangeFinished() may be invoked before a recomposition has
// happened for onValueChange(), for example in the clicking case, so make
// onValueChange() share the latest value to onValueChangeFinished().
var latestSliderValue = sliderValue
Slider(
value = sliderValue,
onValueChange = {
onSliderValueChange(it)
latestSliderValue = it
},
modifier = Modifier.weight(1f),
enabled = enabled,
valueRange = valueRange,
steps = valueSteps,
onValueChangeFinished = { onValueChange(latestSliderValue) },
)
if (valueText != null) {
val theme = LocalPreferenceTheme.current
Box(modifier = Modifier.padding(start = theme.horizontalSpacing)) {
valueText()
}
}
}
}
},
)
}