Skip to content

Commit

Permalink
perf: 优化样式
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaoyaning committed Aug 23, 2022
1 parent 3f6505b commit 930f457
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
17 changes: 13 additions & 4 deletions app/src/main/java/com/jyn/composecalculator/DateViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Date>().apply {
var results = mutableStateListOf<Date>().apply {
add(Date(getNow(), "24+32", "56"))
add(Date(getNow(), "32+24", "56"))
add(Date(getNow(), "32+24-6", "50"))
Expand All @@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
52 changes: 31 additions & 21 deletions app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -121,6 +127,7 @@ fun TopResultView() {
/**
* 计算布局 & 历史列表
*/
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterialApi::class)
@Composable
fun TextBox(process: Float, onClick: () -> Unit) {
val viewModel = viewModel<DateViewModel>()
Expand All @@ -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
)
}
}
}

Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/jyn/composecalculator/ui/util/Click.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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()
"C"
-> viewModel.clear()
"="
-> viewModel.calculate()
else
-> viewModel.appendNum(text)
}
}

0 comments on commit 930f457

Please sign in to comment.