-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jiaoyaning
committed
Aug 9, 2022
1 parent
51e6efd
commit 79339f3
Showing
7 changed files
with
121 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...mposecalculator/ui/calculate/Calculate.kt → ...yn/composecalculator/ui/util/Calculate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/jyn/composecalculator/ui/util/Click.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...n/composecalculator/ui/draw/CursorView.kt → ...n/composecalculator/ui/view/CursorView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/src/main/java/com/jyn/composecalculator/ui/view/ItemTextView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DateViewModel>() | ||
|
||
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") | ||
} |
2 changes: 1 addition & 1 deletion
2
...mposecalculator/ui/draw/SlideIndicator.kt → ...mposecalculator/ui/view/SlideIndicator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters