Skip to content

Commit

Permalink
style(横竖屏): 横竖屏样式调整
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaoyaning committed Aug 9, 2022
1 parent 51e6efd commit 79339f3
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 86 deletions.
22 changes: 4 additions & 18 deletions app/src/main/java/com/jyn/composecalculator/ui/BottomBtnView.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.jyn.composecalculator.ui

import android.content.res.Configuration
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -18,6 +17,7 @@ import com.jyn.composecalculator.BOTTOM_FRACTION
import com.jyn.composecalculator.DateViewModel
import com.jyn.composecalculator.R
import com.jyn.composecalculator.isPortrait
import com.jyn.composecalculator.ui.util.textClick

/**
* 底部按钮
Expand Down Expand Up @@ -113,18 +113,4 @@ fun ItemBtn(text: String) {
)
}
}
}

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()
}
}
72 changes: 7 additions & 65 deletions app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jyn.composecalculator.ui

import android.content.res.Configuration
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.indication
Expand All @@ -15,7 +14,10 @@ import androidx.compose.material.rememberSwipeableState
import androidx.compose.material.swipeable
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
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
Expand All @@ -31,8 +33,9 @@ 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.draw.CursorView
import com.jyn.composecalculator.ui.draw.SlideIndicator
import com.jyn.composecalculator.ui.view.CursorView
import com.jyn.composecalculator.ui.view.InputText
import com.jyn.composecalculator.ui.view.SlideIndicator

/**
* 上层结果
Expand Down Expand Up @@ -103,65 +106,4 @@ fun TextBox(process: Float) {
SlideIndicator(process)
}
}
}

@Composable
fun ItemText(input: String, result: String) {
Column(verticalArrangement = Arrangement.Bottom) {
Text(modifier = Modifier.fillMaxWidth(), text = input, textAlign = TextAlign.End)
}
}

@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,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jyn.composecalculator.ui.calculate
package com.jyn.composecalculator.ui.util

fun calculateU(input: String): String {
return "XXXXX"
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/jyn/composecalculator/ui/util/Click.kt
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()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jyn.composecalculator.ui.draw
package com.jyn.composecalculator.ui.view

import androidx.compose.animation.core.*
import androidx.compose.foundation.background
Expand Down
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")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jyn.composecalculator.ui.draw
package com.jyn.composecalculator.ui.view

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
Expand Down

0 comments on commit 79339f3

Please sign in to comment.