@@ -3,29 +3,20 @@ package dev.robercoding.decimal.formatter
3
3
import androidx.compose.foundation.layout.Arrangement
4
4
import androidx.compose.foundation.layout.Column
5
5
import androidx.compose.foundation.layout.fillMaxSize
6
+ import androidx.compose.foundation.layout.padding
6
7
import androidx.compose.foundation.layout.systemBarsPadding
7
- import androidx.compose.foundation.text.KeyboardOptions
8
- import androidx.compose.material3.MaterialTheme
9
- import androidx.compose.material3.OutlinedTextFieldDefaults
8
+ import androidx.compose.foundation.rememberScrollState
9
+ import androidx.compose.foundation.verticalScroll
10
10
import androidx.compose.material3.Surface
11
- import androidx.compose.material3.Text
12
11
import androidx.compose.runtime.Composable
12
+ import androidx.compose.runtime.collectAsState
13
13
import androidx.compose.runtime.getValue
14
- import androidx.compose.runtime.mutableStateOf
15
14
import androidx.compose.runtime.remember
16
- import androidx.compose.runtime.setValue
17
15
import androidx.compose.ui.Alignment
18
16
import androidx.compose.ui.Modifier
19
- import androidx.compose.ui.graphics.Color
20
- import androidx.compose.ui.text.input.KeyboardType
21
17
import androidx.compose.ui.unit.dp
22
- import dev.robercoding.decimal.formatter.compose.components.DecimalTextField
23
- import dev.robercoding.decimal.formatter.compose.components.OutlinedDecimalTextField
24
- import dev.robercoding.decimal.formatter.compose.formatter.UiDecimalFormatter
25
- import dev.robercoding.decimal.formatter.compose.formatter.rememberUiDecimalFormatter
26
- import dev.robercoding.decimal.formatter.compose.model.DecimalValue
27
- import dev.robercoding.decimal.formatter.core.formatter.DecimalFormatterConfiguration
28
- import dev.robercoding.decimal.formatter.core.utils.logMessage
18
+ import dev.robercoding.decimal.formatter.percentage.PercentageSection
19
+ import dev.robercoding.decimal.formatter.price.PriceSection
29
20
import org.jetbrains.compose.resources.ExperimentalResourceApi
30
21
31
22
@OptIn(ExperimentalResourceApi ::class )
@@ -34,98 +25,22 @@ fun App() {
34
25
Surface (
35
26
modifier = Modifier .fillMaxSize().systemBarsPadding()
36
27
) {
37
- val decimalFormatterPrefix = rememberUiDecimalFormatter(DecimalFormatterConfiguration .european(), prefix = " €" )
38
- val decimalFormatter = rememberUiDecimalFormatter(DecimalFormatterConfiguration .european())
39
-
40
- var outlinedAmountValuePrefix by remember { mutableStateOf(decimalFormatterPrefix.format(50524.02 .toString())) }
41
- var outlinedAmountValueWithoutPrefix by remember { mutableStateOf(decimalFormatter.format(" 552423232323,64" )) }
42
-
43
- var decimalValuePrefix by remember { mutableStateOf(decimalFormatterPrefix.format(" 552423232323,64" )) }
44
- var decimalValueWithoutPrefix by remember { mutableStateOf(decimalFormatter.format(" 552423232323,64" )) }
28
+ val viewModel = remember { DecimalFormatterViewModel () }
29
+ val state by viewModel.uiState.collectAsState()
45
30
46
31
Column (
47
32
horizontalAlignment = Alignment .CenterHorizontally ,
48
- verticalArrangement = Arrangement .spacedBy(16 .dp)
33
+ verticalArrangement = Arrangement .spacedBy(16 .dp),
34
+ modifier = Modifier .padding(horizontal = 32 .dp).verticalScroll(rememberScrollState())
49
35
) {
50
- // Outlined region
51
- OutlinedDecimalTextFieldComposable (
52
- currentAmount = outlinedAmountValuePrefix,
53
- onValueChange = { decimalValue -> outlinedAmountValuePrefix = decimalValue },
54
- decimalFormatter = decimalFormatterPrefix
55
- )
56
-
57
- OutlinedDecimalTextFieldComposable (
58
- currentAmount = outlinedAmountValueWithoutPrefix,
59
- onValueChange = { decimalValue -> outlinedAmountValueWithoutPrefix = decimalValue },
60
- decimalFormatter = decimalFormatterPrefix
61
- )
62
-
63
- // No decor TextField region
64
- DecimalTextFieldComposable (
65
- currentAmount = decimalValuePrefix,
66
- onFormattedValueChange = { decimalValue -> decimalValuePrefix = decimalValue },
67
- decimalFormatter = decimalFormatterPrefix
68
- )
69
-
70
- DecimalTextFieldComposable (
71
- currentAmount = decimalValueWithoutPrefix,
72
- onFormattedValueChange = { decimalValue -> decimalValueWithoutPrefix = decimalValue },
73
- decimalFormatter = decimalFormatterPrefix
36
+ PercentageSection ()
37
+ WeightSection ()
38
+ PriceSection (
39
+ priceEuro = state.priceEuro,
40
+ priceEuropean = state.priceEuropean,
41
+ onPriceEuroChange = { newPrice -> viewModel.setPriceEuro(newPrice) },
42
+ onPriceEuropeanChange = { newPrice -> viewModel.setPriceEuropean(newPrice) },
74
43
)
75
44
}
76
-
77
45
}
78
46
}
79
-
80
- @Composable
81
- fun OutlinedDecimalTextFieldComposable (
82
- currentAmount : DecimalValue ,
83
- onValueChange : (DecimalValue ) -> Unit ,
84
- decimalFormatter : UiDecimalFormatter
85
- ) {
86
- OutlinedDecimalTextField (
87
- modifier = Modifier ,
88
- decimalValue = currentAmount,
89
- onValueChange = onValueChange,
90
- label = { Text (" Price (€)" , style = MaterialTheme .typography.labelSmall) },
91
- keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Number ),
92
- textStyle = MaterialTheme .typography.bodyLarge,
93
- shape = MaterialTheme .shapes.small,
94
- colors = OutlinedTextFieldDefaults .colors().copy(
95
- focusedTextColor = if (currentAmount.display == " 0,00" ) {
96
- logMessage(" Using gray color for zero value" )
97
- Color .Gray
98
- } else {
99
- logMessage(" Using default color for non-zero value: $currentAmount " )
100
- MaterialTheme .colorScheme.onSurface
101
- },
102
- unfocusedTextColor = if (currentAmount.display == " 0,00" ) {
103
- logMessage(" Using gray color for zero value" )
104
- Color .Gray
105
- } else {
106
- logMessage(" Using default color for non-zero value: $currentAmount " )
107
- MaterialTheme .colorScheme.onSurface
108
- }
109
- ),
110
- decimalFormatter = decimalFormatter
111
- )
112
- }
113
-
114
- @Composable
115
- fun DecimalTextFieldComposable (
116
- currentAmount : DecimalValue ,
117
- onFormattedValueChange : (DecimalValue ) -> Unit ,
118
- decimalFormatter : UiDecimalFormatter ,
119
- ) {
120
- DecimalTextField (
121
- modifier = Modifier ,
122
- value = currentAmount,
123
- onValueChange = { value ->
124
- onFormattedValueChange(value)
125
- },
126
- prefix = " $" ,
127
- keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Number ),
128
- textStyle = MaterialTheme .typography.headlineLarge,
129
- decimalFormatter = decimalFormatter
130
- )
131
- }
0 commit comments