Skip to content

Commit

Permalink
feat(顶部): 历史记录点击转换输入
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaoyaning committed Aug 10, 2022
1 parent da81738 commit 4a49d52
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 34 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/com/jyn/composecalculator/DateViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ data class Date(var input: String, var result: String)

class DateViewModel(application: Application) : AndroidViewModel(application) {
var textBoxHeight = 0.dp
var results = mutableListOf<Date>()
var results = mutableListOf<Date>().apply {
add(Date("111111+1","11111112"))
add(Date("211111+1","21111112"))
add(Date("311111+1","31111112"))
add(Date("411111+1","41111112"))
add(Date("511111+1","51111112"))
add(Date("611111+1","61111112"))
add(Date("711111+1","71111112"))
}
var inputText = mutableStateOf("")
var resultText = mutableStateOf("")
var lastInputText = mutableStateOf("")
Expand Down Expand Up @@ -49,7 +57,7 @@ class DateViewModel(application: Application) : AndroidViewModel(application) {
}
try {
val eval = Expression(expr).setPrecision(18).eval()
resultText.value = eval.toString()
resultText.value = eval.toPlainString()
lastInputText.value = inputText.value

if (results.size > 10) results.removeFirst()
Expand Down
39 changes: 28 additions & 11 deletions app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.jyn.composecalculator.ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.indication
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.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.FractionalThreshold
import androidx.compose.material.*
import androidx.compose.material.SwipeableDefaults.resistanceConfig
import androidx.compose.material.TabRowDefaults.Divider
import androidx.compose.material.rememberSwipeableState
import androidx.compose.material.swipeable
import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -23,9 +26,11 @@ 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.theme.SecondaryRippleTheme
import com.jyn.composecalculator.ui.view.InputText
import com.jyn.composecalculator.ui.view.ItemText
import com.jyn.composecalculator.ui.view.SlideIndicator
import kotlinx.coroutines.launch

/**
* 上层结果
Expand All @@ -51,6 +56,12 @@ fun TopResultView() {
val state = rememberSwipeableState(true)
val blockSizePx = with(LocalDensity.current) { -topHeight.toPx() }
val anchors = mapOf(0f to false, blockSizePx to true)

// val coroutineScope = rememberCoroutineScope()
// coroutineScope.launch {
// state.animateTo(true, SwipeableDefaults.AnimationSpec)
// }

Surface(
modifier = Modifier
.width(screenWidth)
Expand All @@ -76,6 +87,9 @@ fun TopResultView() {
}
}

/**
* 计算布局 & 历史列表
*/
@Composable
fun TextBox(process: Float) {
val viewModel = viewModel<DateViewModel>()
Expand All @@ -89,14 +103,17 @@ fun TextBox(process: Float) {
modifier = Modifier
.weight(1f),
reverseLayout = true,
userScrollEnabled = false,
userScrollEnabled = true,
) {
itemsIndexed(viewModel.results) { index, item ->
ItemText(input = item.input, result = item.result)
if (index < viewModel.results.size - 1) {
Spacer(modifier = Modifier.height(5.dp))
Divider()
Spacer(modifier = Modifier.height(5.dp))
Box(modifier = Modifier.clickable {
viewModel.inputText.value = item.input
viewModel.resultText.value = item.result
}) {
ItemText(input = item.input, result = item.result)
if (index < viewModel.results.size - 1) {
Divider()
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/jyn/composecalculator/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ package com.jyn.composecalculator.ui.theme
import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.material.ripple.RippleAlpha
import androidx.compose.material.ripple.RippleTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
Expand Down Expand Up @@ -58,3 +64,16 @@ fun ComposeCalculatorTheme(
)
}

@Immutable
object SecondaryRippleTheme : RippleTheme {
@Composable
override fun defaultColor() = Color.Transparent

@Composable
override fun rippleAlpha() = RippleAlpha(
pressedAlpha = 0f,
focusedAlpha = 0f,
draggedAlpha = 0f,
hoveredAlpha = 0f
)
}
51 changes: 30 additions & 21 deletions app/src/main/java/com/jyn/composecalculator/ui/view/ItemTextView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.jyn.composecalculator.ui.view
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand Down Expand Up @@ -77,32 +78,40 @@ fun InputText(input: String) {
@OptIn(ExperimentalTextApi::class)
@Composable
fun ItemText(input: String, result: String) {
Column(verticalArrangement = Arrangement.Bottom) {
Text(
modifier = Modifier.fillMaxWidth(),
text = input,
fontSize = 23.sp,
maxLines = 1,
textAlign = TextAlign.End,
style = TextStyle(
platformStyle = PlatformTextStyle(
includeFontPadding = false
Column(
modifier = Modifier.padding(top = 10.dp, bottom = 10.dp),
verticalArrangement = Arrangement.Bottom
) {
SelectionContainer {
Text(
modifier = Modifier.fillMaxWidth(),
text = input,
fontSize = 23.sp,
maxLines = 1,
color = Color.Gray,
textAlign = TextAlign.End,
style = TextStyle(
platformStyle = PlatformTextStyle(
includeFontPadding = false
)
)
)
)
}

Text(
modifier = Modifier.fillMaxWidth(),
text = result,
fontSize = 33.sp,
maxLines = 1,
textAlign = TextAlign.End,
style = TextStyle(
platformStyle = PlatformTextStyle(
includeFontPadding = false
SelectionContainer {
Text(
modifier = Modifier.fillMaxWidth(),
text = result,
fontSize = 33.sp,
maxLines = 1,
textAlign = TextAlign.End,
style = TextStyle(
platformStyle = PlatformTextStyle(
includeFontPadding = false
)
)
)
)
}
}
}

Expand Down

0 comments on commit 4a49d52

Please sign in to comment.