diff --git a/app/src/main/java/com/jyn/composecalculator/MainActivity.kt b/app/src/main/java/com/jyn/composecalculator/MainActivity.kt index 9b5560b..40e23ed 100644 --- a/app/src/main/java/com/jyn/composecalculator/MainActivity.kt +++ b/app/src/main/java/com/jyn/composecalculator/MainActivity.kt @@ -13,8 +13,9 @@ import androidx.compose.runtime.SideEffect import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalConfiguration -import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp import androidx.core.view.WindowCompat import androidx.lifecycle.viewmodel.compose.viewModel import com.apkfuns.logutils.LogUtils @@ -27,7 +28,7 @@ import com.jyn.composecalculator.ui.theme.myTheme const val BOTTOM_FRACTION = 0.67f var isPortrait = false //横竖屏 var isDark = false //暗黑模式 -var statusBarHeight = 0 +var statusBarHeight = 25.dp //状态栏高度 class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -39,8 +40,12 @@ class MainActivity : ComponentActivity() { ComposeCalculatorTheme { //是否是竖屏 isPortrait = LocalConfiguration.current.orientation == ORIENTATION_PORTRAIT - val viewModel = viewModel() - LogUtils.tag("viewModel").i("MainActivity viewModel : $viewModel") + + LocalDensity.current.run { + statusBarHeight = resources.getDimensionPixelSize( + resources.getIdentifier("status_bar_height", "dimen", "android") + ).toDp() + } ContentView() @@ -52,17 +57,6 @@ class MainActivity : ComponentActivity() { darkIcons = !useDarkIcons ) } - - with(LocalContext.current) { - val size: Int = resources.getDimensionPixelSize( - resources.getIdentifier( - "status_bar_height", - "dimen", - "android" - ) - ) - } - LogUtils.tag("main").i("statusBarHeight:$statusBarHeight") } } } diff --git a/app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt b/app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt index c5d75c0..fc57139 100644 --- a/app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt +++ b/app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt @@ -2,6 +2,7 @@ package com.jyn.composecalculator.ui import androidx.activity.OnBackPressedCallback import androidx.activity.compose.LocalOnBackPressedDispatcherOwner +import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.clickable @@ -10,6 +11,7 @@ import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* @@ -22,11 +24,12 @@ import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha -import androidx.compose.ui.draw.scale import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp @@ -35,6 +38,8 @@ import com.apkfuns.logutils.LogUtils import com.jyn.composecalculator.BOTTOM_FRACTION import com.jyn.composecalculator.DateViewModel import com.jyn.composecalculator.isPortrait +import com.jyn.composecalculator.statusBarHeight +import com.jyn.composecalculator.ui.theme.btnEqualBgDark import com.jyn.composecalculator.ui.theme.evaluator import com.jyn.composecalculator.ui.theme.myTheme import com.jyn.composecalculator.ui.view.InputText @@ -132,8 +137,8 @@ fun TopResultView() { @Composable fun TextBox(process: Float, onClick: () -> Unit) { val viewModel = viewModel() - val openDialog: MutableState = remember { mutableStateOf(-1) } - if (openDialog.value != -1) { + val openDialog: MutableState = remember { mutableStateOf(false) } + if (openDialog.value) { DeleteDialog(openDialog) } Column( @@ -144,7 +149,7 @@ fun TextBox(process: Float, onClick: () -> Unit) { Surface( modifier = Modifier .background(myTheme.topListBg) - .padding(top = 25.dp, bottom = 10.dp), + .padding(top = statusBarHeight, bottom = 10.dp), color = myTheme.topBg, shape = RoundedCornerShape(bottomStart = 25.dp, bottomEnd = 25.dp), tonalElevation = 4.dp * (process), @@ -181,32 +186,45 @@ fun TextBox(process: Float, onClick: () -> Unit) { .padding(start = 10.dp, end = 10.dp) ) { LazyColumn( - Modifier.weight(1f), + Modifier + .weight(1f) + .padding(bottom = 10.dp), reverseLayout = true, userScrollEnabled = true, ) { - itemsIndexed(viewModel.results) { index, item -> - Box( - modifier = Modifier.combinedClickable( - onClick = {}, - onLongClick = { openDialog.value = index }) - ) { - ItemText(input = item.input, result = item.result) - } + item { + } + items(viewModel.results) { item -> + ItemText(input = item.input, result = item.result) } } - Divider( Modifier .alpha(process) - .padding(start = 10.dp) + .padding(start = 10.dp, bottom = 10.dp) .width(1.dp) .fillMaxHeight(), color = Color.Gray ) - TopBtn( - Modifier.width(80.dp * process) + TopBtn(Modifier.width(80.dp * process)) + } + + AnimatedVisibility(visible = (process >= 1f)) { + Text( + modifier = Modifier + .background(myTheme.topListBg) + .clickable( + indication = null, + interactionSource = remember { MutableInteractionSource() }) { + openDialog.value = viewModel.results.size != 0 + } + .fillMaxWidth() + .padding(bottom = 10.dp), + textAlign = TextAlign.Center, + color = if (viewModel.results.size == 0) Color.Gray else Color(0xFF2760F5), + text = if (viewModel.results.size == 0) "无历史记录" else "清除历史记录", + fontWeight = FontWeight.Bold ) } @@ -225,7 +243,7 @@ fun TextBox(process: Float, onClick: () -> Unit) { indication = null, interactionSource = remember { MutableInteractionSource() }) .background(evaluator(1 - process, myTheme.topListBg, myTheme.topBg)) - .padding(top = 10.dp, bottom = 10.dp + 15.dp * process) + .padding(top = 10.dp, bottom = 10.dp + 10.dp * process) .fillMaxWidth() ) { SlideIndicator(process) @@ -234,11 +252,11 @@ fun TextBox(process: Float, onClick: () -> Unit) { } @Composable -fun DeleteDialog(openDialog: MutableState) { +fun DeleteDialog(openDialog: MutableState) { val viewModel = viewModel() AlertDialog( backgroundColor = myTheme.bottomBg, - onDismissRequest = { openDialog.value = -1 }, + onDismissRequest = { openDialog.value = false }, title = { Row(verticalAlignment = Alignment.CenterVertically) { Icon( @@ -249,7 +267,7 @@ fun DeleteDialog(openDialog: MutableState) { ) Spacer(Modifier.size(ButtonDefaults.IconSpacing)) Text( - text = "是否决定删除该记录", + text = "是否决定清除历史记录", color = myTheme.textColor, style = MaterialTheme.typography.h6 ) @@ -257,12 +275,12 @@ fun DeleteDialog(openDialog: MutableState) { }, confirmButton = { TextButton( onClick = { - viewModel.results.removeAt(openDialog.value) - openDialog.value = -1 + viewModel.results.clear() + openDialog.value = false }, ) { Text("确认", fontWeight = FontWeight.W700, color = myTheme.textColor) } }, dismissButton = { - TextButton(onClick = { openDialog.value = -1 }) { + TextButton(onClick = { openDialog.value = false }) { Text("取消", fontWeight = FontWeight.W700, color = myTheme.textColor) } }) @@ -273,7 +291,11 @@ val columns = listOf("D", "÷", "×", "-", "+", "=") @Composable fun TopBtn(modifier: Modifier = Modifier) { - Column(modifier, horizontalAlignment = Alignment.End) { + Column( + modifier, + horizontalAlignment = Alignment.End, + verticalArrangement = Arrangement.SpaceBetween + ) { columns.forEach { Box( modifier = Modifier