Skip to content

Instantly share code, notes, and snippets.

@feresr
Created July 8, 2021 15:14
Show Gist options
  • Save feresr/0de175d4526a65da123fc3b282e3357a to your computer and use it in GitHub Desktop.
Save feresr/0de175d4526a65da123fc3b282e3357a to your computer and use it in GitHub Desktop.
open class SliderContextualViewModel(
val filterFlow: StateFlow<Map<FILTER, FloatArray>>,
val onFilterChanged: (FILTER, FloatArray) -> Unit,
val onFilterCommitted: () -> Unit = {}
)
@Composable
fun SliderContextual(modifier: Modifier, viewModel: SliderContextualViewModel) {
val sliding = remember { mutableStateOf(false) }
Card(
modifier = modifier.navigationBarsPadding().wrapContentSize().padding(16.dp),
shape = RoundedCornerShape(16.dp),
backgroundColor = if (sliding.value) MaterialTheme.colors.surface.copy(alpha = 0.0f) else MaterialTheme.colors.surface,
elevation = if (sliding.value) 0.dp else 1.dp
) {
Column(Modifier.padding(24.dp)) {
for (filter in viewModel.filterFlow.value.keys) {
FilterSlider(filter = filter, sliding = sliding, viewModel = viewModel)
}
}
}
}
@Composable
fun FilterSlider(
filter: FILTER,
sliding: MutableState<Boolean>,
viewModel: SliderContextualViewModel
) {
val value = viewModel.filterFlow.collectAsState().value[filter]
//TODO: NEEDED? .buffer(0, onBufferOverflow = BufferOverflow.DROP_OLDEST)
val thisSliding = remember { mutableStateOf(false) }
LabeledSlider(
modifier = Modifier.alpha(if (!sliding.value || (sliding.value && thisSliding.value)) 1.0f else 0.0f),
label = filter.name,
value = value?.get(0) ?: filter.defaultValue[0],
valueRange = filter.range,
onDoubleTap = {
//value.value = floatArrayOf(filter.defaultValue[0])
viewModel.onFilterChanged(filter, filter.defaultValue)
},
onValueChangeFinished = {
sliding.value = false
thisSliding.value = false
viewModel.onFilterCommitted()
}
) { newValue ->
var clamped = newValue
if (clamped in (filter.defaultValue[0] - 0.05)..(filter.defaultValue[0] + 0.05)) {
clamped = filter.defaultValue[0]
}
//value.value = floatArrayOf(clamped)
viewModel.onFilterChanged(filter, floatArrayOf(clamped))
sliding.value = true
thisSliding.value = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment