Skip to content

Commit a490799

Browse files
Merge pull request #9 from developer-kaczmarek/feature/increase_max_count_of_banknotes
Добавлена поддержка пятизначных значений количества купюр
2 parents 42206d8 + e4f96bb commit a490799

File tree

5 files changed

+158
-48
lines changed

5 files changed

+158
-48
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ android {
1313
applicationId "kaczmarek.moneycalculator"
1414
minSdk min_sdk_version
1515
targetSdk target_sdk_version
16-
versionCode 5
17-
versionName "3.0"
16+
versionCode 6
17+
versionName "3.1"
1818

1919
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2020
vectorDrawables {

app/src/main/kotlin/kaczmarek/moneycalculator/calculator/ui/CalculatorUi.kt

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package kaczmarek.moneycalculator.calculator.ui
22

3+
import AutoResizeText
4+
import FontSizeRange
35
import androidx.compose.foundation.*
46
import androidx.compose.foundation.interaction.MutableInteractionSource
57
import androidx.compose.foundation.layout.*
@@ -25,6 +27,7 @@ import androidx.compose.ui.text.font.FontWeight
2527
import androidx.compose.ui.text.style.TextAlign
2628
import androidx.compose.ui.tooling.preview.Preview
2729
import androidx.compose.ui.unit.dp
30+
import androidx.compose.ui.unit.sp
2831
import kaczmarek.moneycalculator.R
2932
import kaczmarek.moneycalculator.core.theme.AppTheme
3033
import kaczmarek.moneycalculator.core.utils.resolve
@@ -140,7 +143,7 @@ fun BanknoteCard(
140143
} else {
141144
BorderStroke(1.dp, MaterialTheme.colors.onBackground)
142145
},
143-
backgroundColor = MaterialTheme.colors.background
146+
backgroundColor = MaterialTheme.colors.background,
144147
) {
145148
Column {
146149
Text(
@@ -150,50 +153,58 @@ fun BanknoteCard(
150153
text = item.name.resolve(),
151154
textAlign = TextAlign.Center,
152155
style = MaterialTheme.typography.body1,
153-
color = MaterialTheme.colors.onBackground
156+
color = MaterialTheme.colors.onBackground,
154157
)
155-
Row(modifier = Modifier.fillMaxWidth()) {
156-
Column {
157-
Text(
158-
modifier = Modifier
159-
.background(MaterialTheme.colors.surface)
160-
.padding(vertical = 10.dp)
161-
.padding(start = 10.dp),
162-
text = "шт",
163-
color = MaterialTheme.colors.onSurface,
164-
style = MaterialTheme.typography.body1
165-
)
166-
Text(
167-
modifier = Modifier
168-
.padding(vertical = 10.dp)
169-
.padding(start = 10.dp),
170-
text = "",
171-
style = MaterialTheme.typography.body1,
172-
fontWeight = FontWeight.Bold,
173-
color = MaterialTheme.colors.onBackground
174-
)
175-
}
176-
Column {
177-
Text(
178-
modifier = Modifier
179-
.fillMaxWidth()
180-
.background(MaterialTheme.colors.surface)
181-
.padding(10.dp),
182-
text = item.count,
183-
textAlign = TextAlign.End,
184-
style = MaterialTheme.typography.body1
185-
)
186-
Text(
187-
modifier = Modifier
188-
.fillMaxWidth()
189-
.padding(10.dp),
190-
text = item.amount,
191-
textAlign = TextAlign.End,
192-
style = MaterialTheme.typography.body1,
193-
fontWeight = FontWeight.Bold,
194-
color = MaterialTheme.colors.onBackground
195-
)
196-
}
158+
Row(
159+
modifier = Modifier
160+
.fillMaxWidth()
161+
.background(MaterialTheme.colors.surface)
162+
.padding(10.dp),
163+
verticalAlignment = Alignment.CenterVertically,
164+
) {
165+
Text(
166+
modifier = Modifier.width(30.dp),
167+
text = "шт",
168+
color = MaterialTheme.colors.onSurface,
169+
style = MaterialTheme.typography.body1,
170+
)
171+
Text(
172+
modifier = Modifier
173+
.fillMaxWidth()
174+
.weight(1.0f),
175+
text = item.count,
176+
textAlign = TextAlign.End,
177+
style = MaterialTheme.typography.body1,
178+
)
179+
}
180+
Row(
181+
modifier = Modifier
182+
.fillMaxWidth()
183+
.padding(10.dp),
184+
verticalAlignment = Alignment.CenterVertically,
185+
) {
186+
Text(
187+
modifier = Modifier.width(30.dp),
188+
text = "",
189+
style = MaterialTheme.typography.body1,
190+
fontWeight = FontWeight.Bold,
191+
color = MaterialTheme.colors.onBackground,
192+
)
193+
AutoResizeText(
194+
modifier = Modifier
195+
.fillMaxWidth()
196+
.weight(1.0f),
197+
text = item.amount,
198+
textAlign = TextAlign.End,
199+
style = MaterialTheme.typography.body1,
200+
fontWeight = FontWeight.Bold,
201+
color = MaterialTheme.colors.onBackground,
202+
fontSizeRange = FontSizeRange(
203+
min = 12.sp,
204+
max = 18.sp,
205+
),
206+
maxLines = 1,
207+
)
197208
}
198209
}
199210
}

app/src/main/kotlin/kaczmarek/moneycalculator/calculator/ui/RealCalculatorComponent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class RealCalculatorComponent(
8282
amount = (banknote.denomination * digit.toInt()).toFormattedAmount()
8383
)
8484
}
85-
index == selectedBanknoteIndex && banknote.count.count() < 3 -> {
85+
index == selectedBanknoteIndex && banknote.count.count() < 5 -> {
8686
val digits = banknote.count + digit
8787
it[index] = banknote.copy(
8888
count = digits,

app/src/main/kotlin/kaczmarek/moneycalculator/core/utils/BanknoteUtils.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ package kaczmarek.moneycalculator.core.utils
33
import java.text.DecimalFormat
44
import java.text.DecimalFormatSymbols
55
import java.util.*
6+
import kotlin.math.floor
67

78
fun Double.toFormattedAmount(): String {
89
val separator = DecimalFormatSymbols(Locale.getDefault()).apply {
910
groupingSeparator = ' '
1011
decimalSeparator = '.'
1112
}
12-
val decimalFormat = DecimalFormat().apply {
13+
14+
val currentPattern = if (this.isInteger()) "#.##" else "0.00"
15+
val decimalFormat = DecimalFormat(currentPattern).apply {
1316
isGroupingUsed = true
1417
groupingSize = 3
1518
decimalFormatSymbols = separator
1619
}
1720
return decimalFormat.format(this)
21+
}
22+
23+
fun Double.isInteger(): Boolean {
24+
return floor(this) == this
1825
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import androidx.compose.material.LocalTextStyle
2+
import androidx.compose.material.Text
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.getValue
5+
import androidx.compose.runtime.mutableStateOf
6+
import androidx.compose.runtime.remember
7+
import androidx.compose.runtime.setValue
8+
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.draw.drawWithContent
10+
import androidx.compose.ui.graphics.Color
11+
import androidx.compose.ui.text.TextStyle
12+
import androidx.compose.ui.text.font.FontFamily
13+
import androidx.compose.ui.text.font.FontStyle
14+
import androidx.compose.ui.text.font.FontWeight
15+
import androidx.compose.ui.text.style.TextAlign
16+
import androidx.compose.ui.text.style.TextDecoration
17+
import androidx.compose.ui.text.style.TextOverflow
18+
import androidx.compose.ui.unit.TextUnit
19+
import androidx.compose.ui.unit.sp
20+
21+
@Composable
22+
fun AutoResizeText(
23+
text: String,
24+
fontSizeRange: FontSizeRange,
25+
modifier: Modifier = Modifier,
26+
color: Color = Color.Unspecified,
27+
fontStyle: FontStyle? = null,
28+
fontWeight: FontWeight? = null,
29+
fontFamily: FontFamily? = null,
30+
letterSpacing: TextUnit = TextUnit.Unspecified,
31+
textDecoration: TextDecoration? = null,
32+
textAlign: TextAlign? = null,
33+
lineHeight: TextUnit = TextUnit.Unspecified,
34+
overflow: TextOverflow = TextOverflow.Clip,
35+
softWrap: Boolean = true,
36+
maxLines: Int = Int.MAX_VALUE,
37+
style: TextStyle = LocalTextStyle.current,
38+
) {
39+
var fontSizeValue by remember { mutableStateOf(fontSizeRange.max.value) }
40+
var readyToDraw by remember { mutableStateOf(false) }
41+
var changedText by remember { mutableStateOf("") }
42+
43+
Text(
44+
text = text,
45+
color = color,
46+
maxLines = maxLines,
47+
fontStyle = fontStyle,
48+
fontWeight = fontWeight,
49+
fontFamily = fontFamily,
50+
letterSpacing = letterSpacing,
51+
textDecoration = textDecoration,
52+
textAlign = textAlign,
53+
lineHeight = lineHeight,
54+
overflow = overflow,
55+
softWrap = softWrap,
56+
style = style,
57+
fontSize = fontSizeValue.sp,
58+
onTextLayout = {
59+
if (it.hasVisualOverflow) {
60+
changedText = text
61+
val nextFontSizeValue = fontSizeValue - fontSizeRange.step.value
62+
if (nextFontSizeValue <= fontSizeRange.min.value) {
63+
fontSizeValue = fontSizeRange.min.value
64+
readyToDraw = true
65+
} else {
66+
fontSizeValue = nextFontSizeValue
67+
}
68+
} else {
69+
if (changedText != text) {
70+
fontSizeValue = fontSizeRange.max.value
71+
}
72+
readyToDraw = true
73+
}
74+
},
75+
modifier = modifier.drawWithContent { if (readyToDraw) drawContent() }
76+
)
77+
}
78+
79+
data class FontSizeRange(
80+
val min: TextUnit,
81+
val max: TextUnit,
82+
val step: TextUnit = DEFAULT_TEXT_STEP,
83+
) {
84+
init {
85+
require(min < max) { "min should be less than max, $this" }
86+
require(step.value > 0) { "step should be greater than 0, $this" }
87+
}
88+
89+
companion object {
90+
private val DEFAULT_TEXT_STEP = 1.sp
91+
}
92+
}

0 commit comments

Comments
 (0)