From f648ef1aa6f90258e279088dc4d470894cbc3e4e Mon Sep 17 00:00:00 2001 From: Sad Ellie Date: Sat, 28 Oct 2023 22:21:10 +0300 Subject: [PATCH] Close history view on back button press closes #112 --- .../unitto/feature/calculator/CalculatorScreen.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorScreen.kt b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorScreen.kt index 2fa0d461..b4f64630 100644 --- a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorScreen.kt +++ b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorScreen.kt @@ -18,12 +18,14 @@ package com.sadellie.unitto.feature.calculator +import androidx.activity.compose.BackHandler import androidx.compose.animation.Crossfade import androidx.compose.animation.core.tween import androidx.compose.foundation.gestures.AnchoredDraggableState import androidx.compose.foundation.gestures.DraggableAnchors import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.anchoredDraggable +import androidx.compose.foundation.gestures.animateTo import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height @@ -44,6 +46,7 @@ import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier @@ -68,6 +71,7 @@ import com.sadellie.unitto.feature.calculator.components.CalculatorKeyboard import com.sadellie.unitto.feature.calculator.components.HistoryItemHeight import com.sadellie.unitto.feature.calculator.components.HistoryList import com.sadellie.unitto.feature.calculator.components.TextBox +import kotlinx.coroutines.launch import java.text.SimpleDateFormat import java.util.Locale @@ -171,6 +175,7 @@ private fun Ready( modifier = Modifier.padding(paddingValues), ) { val density = LocalDensity.current + val coroutineScope = rememberCoroutineScope() val textBoxHeight = maxHeight * 0.25f @@ -225,6 +230,12 @@ private fun Ready( showClearHistoryButton = dragState.currentValue == DragState.OPEN } + BackHandler(dragState.currentValue != DragState.CLOSED) { + coroutineScope.launch { + dragState.animateTo(DragState.CLOSED) + } + } + HistoryList( modifier = Modifier .fillMaxWidth()