From d329ec7eaa9810dc593bed1e6ca3a08e0b8761cc Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Mon, 25 Apr 2022 19:36:52 +0200 Subject: [PATCH] Simplify pull down scaffold animation logic --- .../de/mm20/launcher2/ui/ktx/MutableState.kt | 19 +++++++++++++++++++ .../launcher2/ui/launcher/PullDownScaffold.kt | 15 +++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 ui/src/main/java/de/mm20/launcher2/ui/ktx/MutableState.kt diff --git a/ui/src/main/java/de/mm20/launcher2/ui/ktx/MutableState.kt b/ui/src/main/java/de/mm20/launcher2/ui/ktx/MutableState.kt new file mode 100644 index 00000000..7179b6cc --- /dev/null +++ b/ui/src/main/java/de/mm20/launcher2/ui/ktx/MutableState.kt @@ -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.animateTo(targetValue: Dp) { + animateTo(targetValue, Dp.VectorConverter) +} + +suspend inline fun MutableState.animateTo(targetValue: T, converter: TwoWayConverter) { + val animatable = Animatable(this.value, converter) + animatable.animateTo(targetValue) { + this@animateTo.value = this.value + } +} \ No newline at end of file diff --git a/ui/src/main/java/de/mm20/launcher2/ui/launcher/PullDownScaffold.kt b/ui/src/main/java/de/mm20/launcher2/ui/launcher/PullDownScaffold.kt index 2bba4650..3bf2d600 100644 --- a/ui/src/main/java/de/mm20/launcher2/ui/launcher/PullDownScaffold.kt +++ b/ui/src/main/java/de/mm20/launcher2/ui/launcher/PullDownScaffold.kt @@ -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 -> {