Skip to content

Commit

Permalink
feat(顶部): 清除历史记录
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaoyaning committed Aug 24, 2022
1 parent af1f8b7 commit 2de9b8e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
24 changes: 9 additions & 15 deletions app/src/main/java/com/jyn/composecalculator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?) {
Expand All @@ -39,8 +40,12 @@ class MainActivity : ComponentActivity() {
ComposeCalculatorTheme {
//是否是竖屏
isPortrait = LocalConfiguration.current.orientation == ORIENTATION_PORTRAIT
val viewModel = viewModel<DateViewModel>()
LogUtils.tag("viewModel").i("MainActivity viewModel : $viewModel")

LocalDensity.current.run {
statusBarHeight = resources.getDimensionPixelSize(
resources.getIdentifier("status_bar_height", "dimen", "android")
).toDp()
}

ContentView()

Expand All @@ -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")
}
}
}
Expand Down
72 changes: 47 additions & 25 deletions app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.*
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -132,8 +137,8 @@ fun TopResultView() {
@Composable
fun TextBox(process: Float, onClick: () -> Unit) {
val viewModel = viewModel<DateViewModel>()
val openDialog: MutableState<Int> = remember { mutableStateOf(-1) }
if (openDialog.value != -1) {
val openDialog: MutableState<Boolean> = remember { mutableStateOf(false) }
if (openDialog.value) {
DeleteDialog(openDialog)
}
Column(
Expand All @@ -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),
Expand Down Expand Up @@ -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
)
}

Expand All @@ -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)
Expand All @@ -234,11 +252,11 @@ fun TextBox(process: Float, onClick: () -> Unit) {
}

@Composable
fun DeleteDialog(openDialog: MutableState<Int>) {
fun DeleteDialog(openDialog: MutableState<Boolean>) {
val viewModel = viewModel<DateViewModel>()
AlertDialog(
backgroundColor = myTheme.bottomBg,
onDismissRequest = { openDialog.value = -1 },
onDismissRequest = { openDialog.value = false },
title = {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
Expand All @@ -249,20 +267,20 @@ fun DeleteDialog(openDialog: MutableState<Int>) {
)
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Text(
text = "是否决定删除该记录",
text = "是否决定清除历史记录",
color = myTheme.textColor,
style = MaterialTheme.typography.h6
)
}
}, 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)
}
})
Expand All @@ -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
Expand Down

0 comments on commit 2de9b8e

Please sign in to comment.