-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changes: add slide animation with controller
- Loading branch information
1 parent
79f76d5
commit e23aa85
Showing
15 changed files
with
860 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/src/main/java/pro/jayeshseth/animations/ui/composables/ControllerTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package pro.jayeshseth.animations.ui.composables | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun ControllerTemplate( | ||
title: @Composable () -> Unit, | ||
content: @Composable () -> Unit, | ||
modifier: Modifier = Modifier | ||
) { | ||
Column( | ||
modifier = modifier, | ||
verticalArrangement = Arrangement.spacedBy(8.dp) | ||
) { | ||
Row( | ||
horizontalArrangement = Arrangement.SpaceBetween, | ||
verticalAlignment = Alignment.CenterVertically, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
) { | ||
title() | ||
} | ||
content() | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
app/src/main/java/pro/jayeshseth/animations/ui/composables/DropDownTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package pro.jayeshseth.animations.ui.composables | ||
|
||
import androidx.compose.foundation.layout.ColumnScope | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.ExposedDropdownMenuBox | ||
import androidx.compose.material3.ExposedDropdownMenuDefaults | ||
import androidx.compose.material3.MenuAnchorType | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.dp | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun DropDownTemplate( | ||
value: String, | ||
expanded: Boolean, | ||
onExpandedChange: (Boolean) -> Unit, | ||
title: @Composable () -> Unit, | ||
onDismissRequest: () -> Unit, | ||
content: @Composable ColumnScope.() -> Unit, | ||
modifier: Modifier = Modifier | ||
) { | ||
ControllerTemplate( | ||
modifier = modifier, | ||
title = title, | ||
content = { | ||
ExposedDropdownMenuBox( | ||
expanded = expanded, | ||
onExpandedChange = onExpandedChange, | ||
) { | ||
TextField( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.menuAnchor(MenuAnchorType.PrimaryNotEditable), | ||
value = value, | ||
onValueChange = {}, | ||
readOnly = true, | ||
singleLine = true, | ||
shape = RoundedCornerShape(50.dp), | ||
trailingIcon = { | ||
ExposedDropdownMenuDefaults.TrailingIcon( | ||
expanded = expanded | ||
) | ||
}, | ||
colors = ExposedDropdownMenuDefaults.textFieldColors( | ||
focusedIndicatorColor = Color.Transparent, | ||
unfocusedIndicatorColor = Color.Transparent, | ||
disabledIndicatorColor = Color.Transparent, | ||
), | ||
) | ||
ExposedDropdownMenu( | ||
expanded = expanded, | ||
onDismissRequest = onDismissRequest, | ||
shape = RoundedCornerShape(25.dp), | ||
content = content, | ||
) | ||
} | ||
} | ||
) | ||
} |
104 changes: 104 additions & 0 deletions
104
app/src/main/java/pro/jayeshseth/animations/ui/composables/SliderTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package pro.jayeshseth.animations.ui.composables | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.LocalContentColor | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Slider | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.mutableFloatStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import kotlin.math.roundToInt | ||
|
||
@Composable | ||
fun SliderTemplate( | ||
title: String, | ||
value: Float, | ||
step: Float, | ||
onValueChange: (Float) -> Unit, | ||
valueRange: ClosedFloatingPointRange<Float> | ||
) { | ||
val sliderValue = rememberUpdatedState(value) | ||
ControllerTemplate( | ||
title = { | ||
Row( | ||
horizontalArrangement = Arrangement.SpaceBetween, | ||
verticalAlignment = Alignment.CenterVertically, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
) { | ||
Text( | ||
text = title, | ||
color = MaterialTheme.colorScheme.onBackground, | ||
) | ||
CompositionLocalProvider( | ||
LocalContentColor provides MaterialTheme.colorScheme.onBackground, | ||
) { | ||
val snappedValue = snapSliderValue(valueRange.start, sliderValue.value, step) | ||
Text( | ||
text = "${snappedValue.roundToInt()}", | ||
color = MaterialTheme.colorScheme.onSurfaceVariant, | ||
) | ||
} | ||
} | ||
}, | ||
content = { | ||
Slider( | ||
value = sliderValue.value, | ||
onValueChange = onValueChange, | ||
onValueChangeFinished = { }, | ||
valueRange = valueRange, | ||
steps = getSteps(valueRange, step), | ||
modifier = Modifier | ||
.padding(top = 2.dp, bottom = 12.dp) | ||
.height(24.dp), | ||
) | ||
} | ||
) | ||
} | ||
|
||
private fun getSteps(valueRange: ClosedFloatingPointRange<Float>, step: Float): Int { | ||
if (step == 0f) return 0 | ||
val start = valueRange.start | ||
val end = valueRange.endInclusive | ||
val steps = ((end - start) / step).toInt() | ||
require(start + step * steps == end) { | ||
"value range must be a multiple of step" | ||
} | ||
return steps - 1 | ||
} | ||
|
||
private fun snapSliderValue(start: Float, value: Float, step: Float): Float { | ||
if (step == 0f) return value | ||
val distance = value - start | ||
val stepsFromStart = (distance / step).roundToInt() | ||
val snappedDistance = stepsFromStart * step | ||
return start + snappedDistance | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
private fun Preview() { | ||
val value = remember { | ||
mutableFloatStateOf(0f) | ||
} | ||
SliderTemplate( | ||
title = "title", | ||
value = value.floatValue, | ||
step = 0.1f, | ||
onValueChange = { | ||
value.floatValue = it | ||
}, | ||
valueRange = 0f..100f | ||
) | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/pro/jayeshseth/animations/ui/composables/Toggler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package pro.jayeshseth.animations.ui.composables | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun Toggler( | ||
title: String, | ||
checked: Boolean, | ||
onCheckedChanged: (Boolean) -> Unit, | ||
modifier: Modifier = Modifier, | ||
enabled: Boolean = true | ||
) { | ||
Row( | ||
modifier = modifier, | ||
horizontalArrangement = Arrangement.SpaceBetween, | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Text(text = title) | ||
Spacer(modifier = Modifier.padding(8.dp)) | ||
Switch( | ||
checked = checked, | ||
onCheckedChange = onCheckedChanged, | ||
enabled = enabled | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.