Skip to content

Commit 06831ec

Browse files
author
jiaoyaning
committed
feat(顶部): 下拉隐藏输入框
1 parent 4a49d52 commit 06831ec

File tree

5 files changed

+97
-49
lines changed

5 files changed

+97
-49
lines changed

app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ android {
3131
}
3232
kotlinOptions {
3333
jvmTarget = '11'
34-
freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
34+
freeCompilerArgs += [
35+
'-opt-in=kotlin.RequiresOptIn', '-Xjvm-default', 'compatibility'
36+
]
3537
}
3638
buildFeatures {
3739
compose true

app/src/main/java/com/jyn/composecalculator/ui/BottomBtnView.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ import com.jyn.composecalculator.ui.util.textClick
2323
* 底部按钮
2424
* Created by jiaoyaning on 2022/8/6.
2525
*/
26+
val optionColumns = listOf(
27+
listOf("INV", "sin", "ln", "π", "("),
28+
listOf("RAD", "cos", "log", "e", ")"),
29+
listOf("%", "tan", "", "^", "!")
30+
)
31+
2632
private val numberColumns = listOf(
2733
listOf("C", "7", "4", "1", "00"),
2834
listOf("%", "8", "5", "2", "0"),
@@ -35,7 +41,7 @@ private val numberColumns = listOf(
3541
fun BottomBtnView() {
3642
Row(
3743
Modifier
38-
.padding(bottom = 10.dp)
44+
.padding(bottom = if (!isPortrait) 0.dp else 10.dp)
3945
.fillMaxHeight(BOTTOM_FRACTION)
4046
) {
4147
if (!isPortrait) {
@@ -46,6 +52,26 @@ fun BottomBtnView() {
4652
.padding(10.dp),
4753
horizontalArrangement = Arrangement.SpaceBetween
4854
) {
55+
optionColumns.forEach { numberColumn ->
56+
Column( //横排
57+
modifier = Modifier
58+
.padding(5.dp)
59+
.fillMaxHeight()
60+
.weight(1f),
61+
verticalArrangement = Arrangement.SpaceBetween
62+
) {
63+
numberColumn.forEach {
64+
Box(
65+
modifier = Modifier
66+
.padding(bottom = 10.dp)
67+
.weight(1f)
68+
.then(if (isPortrait) Modifier.aspectRatio(1f) else Modifier)
69+
) {
70+
ItemBtn(text = it)
71+
}
72+
}
73+
}
74+
}
4975
}
5076
}
5177

app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.jyn.composecalculator.ui
22

3+
import androidx.activity.OnBackPressedCallback
4+
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
35
import androidx.compose.foundation.clickable
46
import androidx.compose.foundation.gestures.Orientation
5-
import androidx.compose.foundation.gestures.detectTapGestures
6-
import androidx.compose.foundation.indication
77
import androidx.compose.foundation.interaction.MutableInteractionSource
88
import androidx.compose.foundation.layout.*
99
import androidx.compose.foundation.lazy.LazyColumn
@@ -12,11 +12,12 @@ import androidx.compose.foundation.shape.RoundedCornerShape
1212
import androidx.compose.material.*
1313
import androidx.compose.material.SwipeableDefaults.resistanceConfig
1414
import androidx.compose.material.TabRowDefaults.Divider
15-
import androidx.compose.material.ripple.LocalRippleTheme
1615
import androidx.compose.material3.Surface
17-
import androidx.compose.runtime.*
16+
import androidx.compose.runtime.Composable
17+
import androidx.compose.runtime.DisposableEffect
18+
import androidx.compose.runtime.remember
19+
import androidx.compose.runtime.rememberCoroutineScope
1820
import androidx.compose.ui.Modifier
19-
import androidx.compose.ui.input.pointer.pointerInput
2021
import androidx.compose.ui.platform.LocalConfiguration
2122
import androidx.compose.ui.platform.LocalDensity
2223
import androidx.compose.ui.tooling.preview.Preview
@@ -26,11 +27,11 @@ import androidx.lifecycle.viewmodel.compose.viewModel
2627
import com.apkfuns.logutils.LogUtils
2728
import com.jyn.composecalculator.BOTTOM_FRACTION
2829
import com.jyn.composecalculator.DateViewModel
29-
import com.jyn.composecalculator.ui.theme.SecondaryRippleTheme
3030
import com.jyn.composecalculator.ui.view.InputText
3131
import com.jyn.composecalculator.ui.view.ItemText
3232
import com.jyn.composecalculator.ui.view.SlideIndicator
3333
import kotlinx.coroutines.launch
34+
import kotlin.math.abs
3435

3536
/**
3637
* 上层结果
@@ -57,10 +58,7 @@ fun TopResultView() {
5758
val blockSizePx = with(LocalDensity.current) { -topHeight.toPx() }
5859
val anchors = mapOf(0f to false, blockSizePx to true)
5960

60-
// val coroutineScope = rememberCoroutineScope()
61-
// coroutineScope.launch {
62-
// state.animateTo(true, SwipeableDefaults.AnimationSpec)
63-
// }
61+
val coroutineScope = rememberCoroutineScope()
6462

6563
Surface(
6664
modifier = Modifier
@@ -83,8 +81,27 @@ fun TopResultView() {
8381
tonalElevation = 3.dp,
8482
shadowElevation = 3.dp
8583
) {
86-
TextBox(process = 100 - (state.offset.value / blockSizePx * 100))
84+
TextBox(process = state.offset.value / blockSizePx)
8785
}
86+
87+
val callback = remember {
88+
object : OnBackPressedCallback(true) {
89+
override fun handleOnBackPressed() {
90+
if (!state.currentValue) {
91+
coroutineScope.launch {
92+
state.animateTo(true, SwipeableDefaults.AnimationSpec)
93+
}
94+
}
95+
}
96+
}
97+
}
98+
val dispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher
99+
DisposableEffect(key1 = Unit, effect = {
100+
dispatcher?.addCallback(callback)
101+
onDispose {
102+
callback.remove()
103+
}
104+
})
88105
}
89106

90107
/**
@@ -106,10 +123,13 @@ fun TextBox(process: Float) {
106123
userScrollEnabled = true,
107124
) {
108125
itemsIndexed(viewModel.results) { index, item ->
109-
Box(modifier = Modifier.clickable {
110-
viewModel.inputText.value = item.input
111-
viewModel.resultText.value = item.result
112-
}) {
126+
Box(
127+
modifier = Modifier.clickable(
128+
indication = null,
129+
interactionSource = remember { MutableInteractionSource() }) {
130+
viewModel.inputText.value = item.input
131+
viewModel.resultText.value = item.result
132+
}) {
113133
ItemText(input = item.input, result = item.result)
114134
if (index < viewModel.results.size - 1) {
115135
Divider()
@@ -118,12 +138,13 @@ fun TextBox(process: Float) {
118138
}
119139
}
120140

121-
InputText(viewModel.inputText.value)
141+
InputText(process)
122142

123143
Spacer(modifier = Modifier.height(5.dp))
124144

125145
Box(
126146
modifier = Modifier
147+
.padding(top = 15.dp, bottom = 15.dp * abs(1 - process))
127148
.fillMaxWidth()
128149
) {
129150
SlideIndicator(process)

app/src/main/java/com/jyn/composecalculator/ui/view/ItemTextView.kt

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.compose.runtime.mutableStateOf
1111
import androidx.compose.runtime.remember
1212
import androidx.compose.ui.Alignment
1313
import androidx.compose.ui.Modifier
14+
import androidx.compose.ui.draw.alpha
1415
import androidx.compose.ui.graphics.Color
1516
import androidx.compose.ui.layout.onSizeChanged
1617
import androidx.compose.ui.text.ExperimentalTextApi
@@ -27,7 +28,7 @@ import com.jyn.composecalculator.DateViewModel
2728

2829

2930
@Composable
30-
fun InputText(input: String) {
31+
fun InputText(process: Float) {
3132
val viewModel = viewModel<DateViewModel>()
3233

3334
val inputScrollState = rememberScrollState()
@@ -43,7 +44,9 @@ fun InputText(input: String) {
4344
}
4445

4546
Column(
46-
modifier = Modifier.height(viewModel.textBoxHeight),
47+
modifier = Modifier
48+
.height(viewModel.textBoxHeight * process)
49+
.alpha(process),
4750
verticalArrangement = Arrangement.Center
4851
) {
4952
Row(verticalAlignment = Alignment.CenterVertically) {
@@ -52,7 +55,7 @@ fun InputText(input: String) {
5255
.weight(1f)
5356
.horizontalScroll(inputScrollState)
5457
.onSizeChanged { inputTextWidth.value = it.width },
55-
text = input,
58+
text = viewModel.inputText.value,
5659
maxLines = 1,
5760
fontSize = 50.sp,
5861
textAlign = TextAlign.End,
@@ -79,39 +82,35 @@ fun InputText(input: String) {
7982
@Composable
8083
fun ItemText(input: String, result: String) {
8184
Column(
82-
modifier = Modifier.padding(top = 10.dp, bottom = 10.dp),
85+
modifier = Modifier.padding(top = 30.dp, bottom = 30.dp),
8386
verticalArrangement = Arrangement.Bottom
8487
) {
85-
SelectionContainer {
86-
Text(
87-
modifier = Modifier.fillMaxWidth(),
88-
text = input,
89-
fontSize = 23.sp,
90-
maxLines = 1,
91-
color = Color.Gray,
92-
textAlign = TextAlign.End,
93-
style = TextStyle(
94-
platformStyle = PlatformTextStyle(
95-
includeFontPadding = false
96-
)
88+
Text(
89+
modifier = Modifier.fillMaxWidth(),
90+
text = input,
91+
fontSize = 23.sp,
92+
maxLines = 1,
93+
color = Color.Gray,
94+
textAlign = TextAlign.End,
95+
style = TextStyle(
96+
platformStyle = PlatformTextStyle(
97+
includeFontPadding = false
9798
)
9899
)
99-
}
100+
)
100101

101-
SelectionContainer {
102-
Text(
103-
modifier = Modifier.fillMaxWidth(),
104-
text = result,
105-
fontSize = 33.sp,
106-
maxLines = 1,
107-
textAlign = TextAlign.End,
108-
style = TextStyle(
109-
platformStyle = PlatformTextStyle(
110-
includeFontPadding = false
111-
)
102+
Text(
103+
modifier = Modifier.fillMaxWidth(),
104+
text = result,
105+
fontSize = 33.sp,
106+
maxLines = 1,
107+
textAlign = TextAlign.End,
108+
style = TextStyle(
109+
platformStyle = PlatformTextStyle(
110+
includeFontPadding = false
112111
)
113112
)
114-
}
113+
)
115114
}
116115
}
117116

app/src/main/java/com/jyn/composecalculator/ui/view/SlideIndicator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import com.jyn.composecalculator.ui.theme.black1
2020
*/
2121
@Composable
2222
fun SlideIndicator(process: Float) {
23-
var offsetProcess = process
23+
var offsetProcess = 1 - process
2424
if (offsetProcess < 0) offsetProcess *= 10
2525
LogUtils.tag("SlideIndicator").i("process: $offsetProcess")
2626
val width = 30.dp
@@ -35,7 +35,7 @@ fun SlideIndicator(process: Float) {
3535
val centerX = size.width / 2
3636
val centerY = size.height / 2
3737

38-
val yOffset = maxOffset.toPx() * offsetProcess / 100
38+
val yOffset = maxOffset.toPx() * offsetProcess
3939

4040
val half = width.toPx() / 2
4141
val leftStart = centerX - half

0 commit comments

Comments
 (0)