diff --git a/app/src/main/java/com/jyn/composecalculator/DateViewModel.kt b/app/src/main/java/com/jyn/composecalculator/DateViewModel.kt index 77eed48..1f5ceb2 100644 --- a/app/src/main/java/com/jyn/composecalculator/DateViewModel.kt +++ b/app/src/main/java/com/jyn/composecalculator/DateViewModel.kt @@ -2,6 +2,7 @@ package com.jyn.composecalculator import android.app.Application import android.widget.Toast +import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.unit.dp import androidx.lifecycle.AndroidViewModel @@ -13,7 +14,7 @@ data class Date(var time: String, var input: String, var result: String) class DateViewModel(application: Application) : AndroidViewModel(application) { var textBoxHeight = 0.dp - var results = mutableListOf().apply { + var results = mutableStateListOf().apply { add(Date(getNow(), "24+32", "56")) add(Date(getNow(), "32+24", "56")) add(Date(getNow(), "32+24-6", "50")) @@ -27,7 +28,11 @@ class DateViewModel(application: Application) : AndroidViewModel(application) { var lastInputText = mutableStateOf("") fun appendNum(text: String) { - inputText.value = inputText.value + text + val replace = text.replace("sin", "sin(") + .replace("cos", "cos(") + .replace("tan", "tan(") + .replace("log", "log(") + inputText.value = inputText.value + replace } fun appendCompute(text: String) { @@ -52,8 +57,12 @@ class DateViewModel(application: Application) : AndroidViewModel(application) { return } - val expr = inputText.value.replace('×', '*').replace('÷', '/').replace("%", "/100") - val expressions = arrayOf('+', '-', '*', '/') + val expr = inputText.value + .replace('×', '*') + .replace('÷', '/') + .replace("%", "/100") + val expressions = + arrayOf("+", "-", "*", "/", "sin", "cos", "tan", "log", "^", "√", "!", "π") if (expr.isEmpty()) return if (!expressions.any { expr.contains(it) }) { return diff --git a/app/src/main/java/com/jyn/composecalculator/ui/BottomBtnView.kt b/app/src/main/java/com/jyn/composecalculator/ui/BottomBtnView.kt index f8be9fa..f0a1ad9 100644 --- a/app/src/main/java/com/jyn/composecalculator/ui/BottomBtnView.kt +++ b/app/src/main/java/com/jyn/composecalculator/ui/BottomBtnView.kt @@ -28,9 +28,9 @@ import com.jyn.composecalculator.ui.util.textClick * Created by jiaoyaning on 2022/8/6. */ val optionColumns = listOf( - listOf("INV", "sin", "ln", "π", "("), + listOf("DEG", "sin", "ln", "π", "("), listOf("RAD", "cos", "log", "e", ")"), - listOf("%", "tan", "√", "^", "!") + listOf("FACT", "tan", "√", "^", "!") ) private val numberColumns = listOf( 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 2da31b9..a35713c 100644 --- a/app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt +++ b/app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt @@ -2,13 +2,14 @@ package com.jyn.composecalculator.ui import androidx.activity.OnBackPressedCallback import androidx.activity.compose.LocalOnBackPressedDispatcherOwner +import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.clickable 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.itemsIndexed +import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.material.SwipeableDefaults.resistanceConfig @@ -29,6 +30,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel 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.ui.theme.evaluator import com.jyn.composecalculator.ui.theme.myTheme import com.jyn.composecalculator.ui.view.InputText @@ -91,11 +93,15 @@ fun TopResultView() { shadowElevation = 3.dp ) { process = state.offset.value / blockSizePx - TextBox(1 - process, onClick = { - coroutineScope.launch { - state.animateTo(true, SwipeableDefaults.AnimationSpec) + Row() { + Box(modifier = Modifier.weight(1f)) { + TextBox(1 - process) { + coroutineScope.launch { + state.animateTo(!state.targetValue, SwipeableDefaults.AnimationSpec) + } + } } - }) + } } val callback = remember { @@ -121,6 +127,7 @@ fun TopResultView() { /** * 计算布局 & 历史列表 */ +@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterialApi::class) @Composable fun TextBox(process: Float, onClick: () -> Unit) { val viewModel = viewModel() @@ -142,21 +149,24 @@ fun TextBox(process: Float, onClick: () -> Unit) { Box { InputText(process) - TopAppBar( - modifier = Modifier.height(40.dp), - title = { Text("当前表达式", color = myTheme.textColor) }, - navigationIcon = { - IconButton(onClick) { - Icon( - Icons.Filled.ArrowBack, - null, - tint = myTheme.textColor - ) - } - }, - backgroundColor = myTheme.topBg, - elevation = 0.dp - ) + //TitleBar + if (isPortrait) { + TopAppBar( + modifier = Modifier.height(40.dp), + title = { Text("当前表达式", color = myTheme.textColor) }, + navigationIcon = { + IconButton(onClick) { + Icon( + Icons.Filled.ArrowBack, + null, + tint = myTheme.textColor + ) + } + }, + backgroundColor = myTheme.topBg, + elevation = 0.dp + ) + } } } @@ -168,7 +178,7 @@ fun TextBox(process: Float, onClick: () -> Unit) { reverseLayout = true, userScrollEnabled = true, ) { - itemsIndexed(viewModel.results) { index, item -> + items(viewModel.results) { item -> Box( modifier = Modifier.clickable( indication = null, diff --git a/app/src/main/java/com/jyn/composecalculator/ui/util/Click.kt b/app/src/main/java/com/jyn/composecalculator/ui/util/Click.kt index 141853c..517bb4c 100644 --- a/app/src/main/java/com/jyn/composecalculator/ui/util/Click.kt +++ b/app/src/main/java/com/jyn/composecalculator/ui/util/Click.kt @@ -6,7 +6,7 @@ fun textClick(viewModel: DateViewModel, text: String) { when (text) { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "00", -> viewModel.appendNum(text) - "÷", "×", "-", "+",".", + "÷", "×", "-", "+", ".", -> viewModel.appendCompute(text) "D" -> viewModel.delete() @@ -14,5 +14,7 @@ fun textClick(viewModel: DateViewModel, text: String) { -> viewModel.clear() "=" -> viewModel.calculate() + else + -> viewModel.appendNum(text) } } \ No newline at end of file