Simplify pull down scaffold animation logic

This commit is contained in:
MM20 2022-04-25 19:36:52 +02:00
parent f4fe64af67
commit d329ec7eaa
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 24 additions and 10 deletions

View 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
}
}

View File

@ -44,6 +44,7 @@ import com.google.accompanist.pager.rememberPagerState
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import de.mm20.launcher2.ktx.isAtLeastApiLevel
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.launcher.search.SearchBar
import de.mm20.launcher2.ui.launcher.search.SearchBarLevel
@ -99,7 +100,7 @@ fun PullDownScaffold(
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) }
val blurWallpaper = isSearchOpen || offsetY.value > 48.dp || widgetsScrollState.value > 0
@ -127,24 +128,18 @@ fun PullDownScaffold(
val consumed = when {
isSearchOpen && (offsetY.value > 0.dp || source == NestedScrollSource.Drag && searchScrollState.value - available.y < 0) -> {
val consumed = available.y - searchScrollState.value
scope.launch {
offsetY.snapTo((offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(0.dp, 64.dp))
}
offsetY.value = (offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(0.dp, 64.dp)
consumed
}
isSearchOpen && (offsetY.value < 0.dp || source == NestedScrollSource.Drag && searchScrollState.value - available.y > searchScrollState.maxValue) -> {
val consumed =
available.y - (searchScrollState.maxValue - searchScrollState.value)
scope.launch {
offsetY.snapTo((offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(-64.dp, 0.dp))
}
offsetY.value = (offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(-64.dp, 0.dp)
consumed
}
!isSearchOpen && (offsetY.value > 0.dp || source == NestedScrollSource.Drag && widgetsScrollState.value - available.y < 0) -> {
val consumed = available.y - widgetsScrollState.value
scope.launch {
offsetY.snapTo((offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(0.dp, 64.dp))
}
offsetY.value = (offsetY.value + (consumed * 0.5f / dp).dp).coerceIn(0.dp, 64.dp)
consumed
}
else -> {