Simplify pull down scaffold animation logic
This commit is contained in:
parent
f4fe64af67
commit
d329ec7eaa
19
ui/src/main/java/de/mm20/launcher2/ui/ktx/MutableState.kt
Normal file
19
ui/src/main/java/de/mm20/launcher2/ui/ktx/MutableState.kt
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package de.mm20.launcher2.ui.ktx
|
||||||
|
|
||||||
|
import androidx.compose. animation.core.Animatable
|
||||||
|
import androidx.compose.animation.core.AnimationVector
|
||||||
|
import androidx.compose.animation.core.TwoWayConverter
|
||||||
|
import androidx.compose.animation.core.VectorConverter
|
||||||
|
import androidx.compose.runtime.MutableState
|
||||||
|
import androidx.compose.ui.unit.Dp
|
||||||
|
|
||||||
|
suspend fun MutableState<Dp>.animateTo(targetValue: Dp) {
|
||||||
|
animateTo(targetValue, Dp.VectorConverter)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend inline fun <T, V: AnimationVector> MutableState<T>.animateTo(targetValue: T, converter: TwoWayConverter<T, V>) {
|
||||||
|
val animatable = Animatable(this.value, converter)
|
||||||
|
animatable.animateTo(targetValue) {
|
||||||
|
this@animateTo.value = this.value
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -44,6 +44,7 @@ import com.google.accompanist.pager.rememberPagerState
|
|||||||
import com.google.accompanist.systemuicontroller.rememberSystemUiController
|
import com.google.accompanist.systemuicontroller.rememberSystemUiController
|
||||||
import de.mm20.launcher2.ktx.isAtLeastApiLevel
|
import de.mm20.launcher2.ktx.isAtLeastApiLevel
|
||||||
import de.mm20.launcher2.ui.R
|
import de.mm20.launcher2.ui.R
|
||||||
|
import de.mm20.launcher2.ui.ktx.animateTo
|
||||||
import de.mm20.launcher2.ui.ktx.toDp
|
import de.mm20.launcher2.ui.ktx.toDp
|
||||||
import de.mm20.launcher2.ui.launcher.search.SearchBar
|
import de.mm20.launcher2.ui.launcher.search.SearchBar
|
||||||
import de.mm20.launcher2.ui.launcher.search.SearchBarLevel
|
import de.mm20.launcher2.ui.launcher.search.SearchBarLevel
|
||||||
@ -99,7 +100,7 @@ fun PullDownScaffold(
|
|||||||
|
|
||||||
val dp = LocalDensity.current.density
|
val dp = LocalDensity.current.density
|
||||||
|
|
||||||
val offsetY = remember { Animatable(0.dp, Dp.VectorConverter) }
|
val offsetY = remember { mutableStateOf(0.dp) }
|
||||||
var searchBarOffset by remember { mutableStateOf(0.dp) }
|
var searchBarOffset by remember { mutableStateOf(0.dp) }
|
||||||
|
|
||||||
val blurWallpaper = isSearchOpen || offsetY.value > 48.dp || widgetsScrollState.value > 0
|
val blurWallpaper = isSearchOpen || offsetY.value > 48.dp || widgetsScrollState.value > 0
|
||||||
@ -127,24 +128,18 @@ fun PullDownScaffold(
|
|||||||
val consumed = when {
|
val consumed = when {
|
||||||
isSearchOpen && (offsetY.value > 0.dp || source == NestedScrollSource.Drag && searchScrollState.value - available.y < 0) -> {
|
isSearchOpen && (offsetY.value > 0.dp || source == NestedScrollSource.Drag && searchScrollState.value - available.y < 0) -> {
|
||||||
val consumed = available.y - searchScrollState.value
|
val consumed = available.y - searchScrollState.value
|
||||||
scope.launch {
|
offsetY.value = (offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(0.dp, 64.dp)
|
||||||
offsetY.snapTo((offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(0.dp, 64.dp))
|
|
||||||
}
|
|
||||||
consumed
|
consumed
|
||||||
}
|
}
|
||||||
isSearchOpen && (offsetY.value < 0.dp || source == NestedScrollSource.Drag && searchScrollState.value - available.y > searchScrollState.maxValue) -> {
|
isSearchOpen && (offsetY.value < 0.dp || source == NestedScrollSource.Drag && searchScrollState.value - available.y > searchScrollState.maxValue) -> {
|
||||||
val consumed =
|
val consumed =
|
||||||
available.y - (searchScrollState.maxValue - searchScrollState.value)
|
available.y - (searchScrollState.maxValue - searchScrollState.value)
|
||||||
scope.launch {
|
offsetY.value = (offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(-64.dp, 0.dp)
|
||||||
offsetY.snapTo((offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(-64.dp, 0.dp))
|
|
||||||
}
|
|
||||||
consumed
|
consumed
|
||||||
}
|
}
|
||||||
!isSearchOpen && (offsetY.value > 0.dp || source == NestedScrollSource.Drag && widgetsScrollState.value - available.y < 0) -> {
|
!isSearchOpen && (offsetY.value > 0.dp || source == NestedScrollSource.Drag && widgetsScrollState.value - available.y < 0) -> {
|
||||||
val consumed = available.y - widgetsScrollState.value
|
val consumed = available.y - widgetsScrollState.value
|
||||||
scope.launch {
|
offsetY.value = (offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(0.dp, 64.dp)
|
||||||
offsetY.snapTo((offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(0.dp, 64.dp))
|
|
||||||
}
|
|
||||||
consumed
|
consumed
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user