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 7e6d63e..645b33c 100644 --- a/app/src/main/java/com/jyn/composecalculator/ui/BottomBtnView.kt +++ b/app/src/main/java/com/jyn/composecalculator/ui/BottomBtnView.kt @@ -1,14 +1,13 @@ package com.jyn.composecalculator.ui -import android.content.res.Configuration -import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.* +import androidx.compose.material3.Button +import androidx.compose.material3.Icon +import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp @@ -18,6 +17,7 @@ import com.jyn.composecalculator.BOTTOM_FRACTION import com.jyn.composecalculator.DateViewModel import com.jyn.composecalculator.R import com.jyn.composecalculator.isPortrait +import com.jyn.composecalculator.ui.util.textClick /** * 底部按钮 @@ -113,18 +113,4 @@ fun ItemBtn(text: String) { ) } } -} - -fun textClick(viewModel: DateViewModel, text: String) { - when (text) { - "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "00", - "÷", "×", "-", "+" - -> viewModel.append(text) - "D" - -> viewModel.delete() - "C" - -> viewModel.clear() - "=" - -> viewModel.calculate() - } } \ No newline at end of file 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 8bbfc26..9d9ecc0 100644 --- a/app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt +++ b/app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt @@ -1,6 +1,5 @@ package com.jyn.composecalculator.ui -import android.content.res.Configuration import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.indication @@ -15,7 +14,10 @@ import androidx.compose.material.rememberSwipeableState import androidx.compose.material.swipeable import androidx.compose.material3.Surface import androidx.compose.material3.Text -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -31,8 +33,9 @@ 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.ui.draw.CursorView -import com.jyn.composecalculator.ui.draw.SlideIndicator +import com.jyn.composecalculator.ui.view.CursorView +import com.jyn.composecalculator.ui.view.InputText +import com.jyn.composecalculator.ui.view.SlideIndicator /** * 上层结果 @@ -103,65 +106,4 @@ fun TextBox(process: Float) { SlideIndicator(process) } } -} - -@Composable -fun ItemText(input: String, result: String) { - Column(verticalArrangement = Arrangement.Bottom) { - Text(modifier = Modifier.fillMaxWidth(), text = input, textAlign = TextAlign.End) - } -} - -@Composable -fun InputText(input: String) { - val viewModel = viewModel() - - val mutableInteractionSource = remember { MutableInteractionSource() } - val inputScrollState = rememberScrollState() - val resultScrollState = rememberScrollState() - val inputTextWidth = remember { mutableStateOf(0) } - val resultTextWidth = remember { mutableStateOf(0) } - - LaunchedEffect(inputTextWidth.value) { - inputScrollState.scrollTo(inputTextWidth.value) //自动滚动到最后一个 - } - LaunchedEffect(inputTextWidth.value) { - resultScrollState.scrollTo(resultTextWidth.value) //自动滚动到最后一个 - } - - Column( - modifier = Modifier.height(viewModel.textBoxHeight), - verticalArrangement = Arrangement.Center - ) { - Row(verticalAlignment = Alignment.CenterVertically) { - Text( - modifier = Modifier - .weight(1f) - .horizontalScroll(inputScrollState) - .indication( //水波纹效果怎么去除无效呢,bug? - indication = null, - interactionSource = mutableInteractionSource - ) - .onSizeChanged { inputTextWidth.value = it.width }, - text = input, - maxLines = 1, - fontSize = 50.sp, - textAlign = TextAlign.End, - ) - CursorView() - } - - Text( - text = viewModel.resultText.value, - modifier = Modifier - .fillMaxWidth() - .padding(end = 5.dp) - .horizontalScroll(inputScrollState) - .onSizeChanged { resultTextWidth.value = it.width }, - maxLines = 1, - fontSize = 30.sp, - color = Color.Gray, - textAlign = TextAlign.End, - ) - } } \ No newline at end of file diff --git a/app/src/main/java/com/jyn/composecalculator/ui/calculate/Calculate.kt b/app/src/main/java/com/jyn/composecalculator/ui/util/Calculate.kt similarity index 56% rename from app/src/main/java/com/jyn/composecalculator/ui/calculate/Calculate.kt rename to app/src/main/java/com/jyn/composecalculator/ui/util/Calculate.kt index 2acb761..1bbc354 100644 --- a/app/src/main/java/com/jyn/composecalculator/ui/calculate/Calculate.kt +++ b/app/src/main/java/com/jyn/composecalculator/ui/util/Calculate.kt @@ -1,4 +1,4 @@ -package com.jyn.composecalculator.ui.calculate +package com.jyn.composecalculator.ui.util fun calculateU(input: String): String { return "XXXXX" 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 new file mode 100644 index 0000000..27e5e88 --- /dev/null +++ b/app/src/main/java/com/jyn/composecalculator/ui/util/Click.kt @@ -0,0 +1,17 @@ +package com.jyn.composecalculator.ui.util + +import com.jyn.composecalculator.DateViewModel + +fun textClick(viewModel: DateViewModel, text: String) { + when (text) { + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "00", + "÷", "×", "-", "+" + -> viewModel.append(text) + "D" + -> viewModel.delete() + "C" + -> viewModel.clear() + "=" + -> viewModel.calculate() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/jyn/composecalculator/ui/draw/CursorView.kt b/app/src/main/java/com/jyn/composecalculator/ui/view/CursorView.kt similarity index 96% rename from app/src/main/java/com/jyn/composecalculator/ui/draw/CursorView.kt rename to app/src/main/java/com/jyn/composecalculator/ui/view/CursorView.kt index 611fa78..6c4ed2a 100644 --- a/app/src/main/java/com/jyn/composecalculator/ui/draw/CursorView.kt +++ b/app/src/main/java/com/jyn/composecalculator/ui/view/CursorView.kt @@ -1,4 +1,4 @@ -package com.jyn.composecalculator.ui.draw +package com.jyn.composecalculator.ui.view import androidx.compose.animation.core.* import androidx.compose.foundation.background diff --git a/app/src/main/java/com/jyn/composecalculator/ui/view/ItemTextView.kt b/app/src/main/java/com/jyn/composecalculator/ui/view/ItemTextView.kt new file mode 100644 index 0000000..b8cf36b --- /dev/null +++ b/app/src/main/java/com/jyn/composecalculator/ui/view/ItemTextView.kt @@ -0,0 +1,90 @@ +package com.jyn.composecalculator.ui.view + +import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.indication +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.lifecycle.viewmodel.compose.viewModel +import com.jyn.composecalculator.DateViewModel + + +@Composable +fun InputText(input: String) { + val viewModel = viewModel() + + val mutableInteractionSource = remember { MutableInteractionSource() } + val inputScrollState = rememberScrollState() + val resultScrollState = rememberScrollState() + val inputTextWidth = remember { mutableStateOf(0) } + val resultTextWidth = remember { mutableStateOf(0) } + + LaunchedEffect(inputTextWidth.value) { + inputScrollState.scrollTo(inputTextWidth.value) //自动滚动到最后一个 + } + LaunchedEffect(inputTextWidth.value) { + resultScrollState.scrollTo(resultTextWidth.value) //自动滚动到最后一个 + } + + Column( + modifier = Modifier.height(viewModel.textBoxHeight), + verticalArrangement = Arrangement.Center + ) { + Row(verticalAlignment = Alignment.CenterVertically) { + Text( + modifier = Modifier + .weight(1f) + .horizontalScroll(inputScrollState) + .indication( //水波纹效果怎么去除无效呢,bug? + indication = null, + interactionSource = mutableInteractionSource + ) + .onSizeChanged { inputTextWidth.value = it.width }, + text = input, + maxLines = 1, + fontSize = 50.sp, + textAlign = TextAlign.End, + ) + CursorView() + } + + Text( + text = viewModel.resultText.value, + modifier = Modifier + .fillMaxWidth() + .padding(end = 5.dp) + .horizontalScroll(inputScrollState) + .onSizeChanged { resultTextWidth.value = it.width }, + maxLines = 1, + fontSize = 30.sp, + color = Color.Gray, + textAlign = TextAlign.End, + ) + } +} + +@Composable +fun ItemText(input: String, result: String) { + Column(verticalArrangement = Arrangement.Bottom) { + Text(modifier = Modifier.fillMaxWidth(), text = input, textAlign = TextAlign.End) + } +} + +@Preview +@Composable +fun InputTextPreview() { + InputText("12345678") +} \ No newline at end of file diff --git a/app/src/main/java/com/jyn/composecalculator/ui/draw/SlideIndicator.kt b/app/src/main/java/com/jyn/composecalculator/ui/view/SlideIndicator.kt similarity index 98% rename from app/src/main/java/com/jyn/composecalculator/ui/draw/SlideIndicator.kt rename to app/src/main/java/com/jyn/composecalculator/ui/view/SlideIndicator.kt index 94ff382..e9ab4c6 100644 --- a/app/src/main/java/com/jyn/composecalculator/ui/draw/SlideIndicator.kt +++ b/app/src/main/java/com/jyn/composecalculator/ui/view/SlideIndicator.kt @@ -1,4 +1,4 @@ -package com.jyn.composecalculator.ui.draw +package com.jyn.composecalculator.ui.view import androidx.compose.foundation.Canvas import androidx.compose.foundation.background