Skip to content

Commit 1a8fed8

Browse files
Removed onAmountChange: (String) -> Unit and merge with NumberKeyboardListener implementation, Fixed rawAmount having 0 in front. (#47)
1 parent ec61e74 commit 1a8fed8

File tree

8 files changed

+53
-38
lines changed

8 files changed

+53
-38
lines changed

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ dependencies {
3838

3939
[CHANGELOG](https://github.com/davidmigloz/number-keyboard/blob/master/CHANGELOG.md)
4040

41+
## [5.0.2]
42+
43+
- Fixed `rawAmount` having 0 in front.
44+
45+
### ⚠️ Breaking Changes
46+
47+
- Removed `onAmountChange: (String) -> Unit` and merge with `NumberKeyboardListener` implementation.
48+
49+
**🧭 Migration Guide**
50+
51+
```kotlin
52+
var amountWithCurrency by remember { mutableStateOf("$currencySymbol 0") }
53+
var amount by remember { mutableStateOf("") }
54+
55+
NumberKeyboard(
56+
amount = amount,
57+
listener = object : NumberKeyboardListener {
58+
override fun onUpdated(data: NumberKeyboardData) {
59+
amountWithCurrency = data.currency
60+
amount = data.rawAmount
61+
}
62+
}
63+
)
64+
```
65+
4166
## [5.0.0] - Kotlin Multiplatform Version
4267

4368
### ✨ New Features
@@ -113,11 +138,11 @@ NumberKeyboard(
113138
#### Use `NumberKeyboard` Composable in your layout:
114139

115140
```kotlin
141+
var amountWithCurrency by remember { mutableStateOf("$currencySymbol 0") }
116142
var amount by remember { mutableStateOf("") }
117143

118144
NumberKeyboard(
119145
amount = amount,
120-
onAmountChanged = { amount = it },
121146
maxAllowedAmount = 999.00,
122147
maxAllowedDecimals = 0,
123148
roundUpToMax = false,
@@ -148,7 +173,8 @@ NumberKeyboard(
148173
},
149174
listener = object : NumberKeyboardListener {
150175
override fun onUpdated(data: NumberKeyboardData) {
151-
text = data.int.toString()
176+
amountWithCurrency = data.currency
177+
amount = data.rawAmount
152178
}
153179
}
154180
)
@@ -157,17 +183,17 @@ NumberKeyboard(
157183
##### Attribute
158184

159185
- `amount` - String: Variable that keeps
160-
- `onAmountChanged` - (String)-> Unit:
161186
- `maxAllowedAmount` - Double (default: 10_000.0): Maximum amount allowed for the `NumberKeyboard`
162187
output
163188
- `maxAllowedDecimals` - Int (default: 2): Maximum decimal points allowed for the `NumberKeyboard`
164189
output
165190
- `currencySymbol` - String (default: "$"): Currency symbol for the `NumberKeyboardData` currency
166191
- `format` Enum(default: NumberKeyboardFormat.Normal) Defines the layout of the number pad. Options:
167-
• Normal: Standard ascending layout (1–9 top to bottom, like a phone dial pad).
168-
• Inverted: Descending layout (9–1 top to bottom, like a calculator).
169-
• Scrambled: Digits are shuffled once at composition.
170-
• AlwaysScrambled: Digits reshuffle every time the user taps a key — great for max security or mild chaos.
192+
• Normal: Standard ascending layout (1–9 top to bottom, like a phone dial pad).
193+
• Inverted: Descending layout (9–1 top to bottom, like a calculator).
194+
• Scrambled: Digits are shuffled once at composition.
195+
• AlwaysScrambled: Digits reshuffle every time the user taps a key — great for max security or
196+
mild chaos.
171197
- `roundUpToMax` - Boolean (default: true): Behaviour to round up to the max allowed amount if
172198
amount has exceeded
173199
- `verticalArrangement` - Arrangement.HorizontalOrVertical (default: 8.dp): Vertical spacing between

lib/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010

1111
val versionMajor = 5 // API Changes, adding big new feature, redesign the App
1212
val versionMinor = 0 // New features in a backwards-compatible manner
13-
val versionPatch = 1 // Backwards-compatible bug fixes
13+
val versionPatch = 2 // Backwards-compatible bug fixes
1414
val versionClassifier: String? = null // Pre-releases (alpha, beta, rc, SNAPSHOT...)
1515

1616
kotlin {

lib/src/commonMain/kotlin/com/davidmiguel/numberkeyboard/NumberKeyboard.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import kotlin.math.pow
1717
@Composable
1818
fun NumberKeyboard(
1919
amount: String,
20-
onAmountChanged: (String) -> Unit,
2120
maxAllowedAmount: Double = 10_000.0,
2221
maxAllowedDecimals: Int = 2,
2322
currencySymbol: String = "$",
@@ -69,17 +68,15 @@ fun NumberKeyboard(
6968
val clickedListener = object : NumberKeyboardClickedListener {
7069
override fun onNumberClicked(number: Int) {
7170
if (amount.isEmpty() && number == 0) return
72-
val appended = amount + number.toString()
71+
val appended = if (amount == "0") number.toString() else amount + number.toString()
7372
val standardised = appended.replace(',', '.').toDoubleOrNull() ?: 0.0
7473

7574
if (getNumberOfDecimals(appended, decimalSeparator) > maxAllowedDecimals) return
7675

7776
if (standardised in 0.0..maxAllowedAmount) {
78-
onAmountChanged(appended)
7977
listener?.onUpdated(NumberKeyboardData(appended, decimalSeparator, groupingSeparator, currencySymbol))
8078
} else if (roundUpToMax) {
8179
val maxAmount = formatMaxAmount(maxAllowedAmount, maxAllowedDecimals, decimalSeparator)
82-
onAmountChanged(maxAmount)
8380
listener?.onUpdated(NumberKeyboardData(maxAmount, decimalSeparator, groupingSeparator, currencySymbol))
8481
}
8582
}
@@ -91,7 +88,6 @@ fun NumberKeyboard(
9188
} else {
9289
"$amount$decimalSeparator"
9390
}
94-
onAmountChanged(updated)
9591
listener?.onUpdated(NumberKeyboardData(updated, decimalSeparator, groupingSeparator, currencySymbol))
9692
}
9793
}
@@ -110,7 +106,6 @@ fun NumberKeyboard(
110106
}
111107

112108
val updated = if (cleanedAmount.length <= 1) "" else cleanedAmount.dropLast(1)
113-
onAmountChanged(updated)
114109
listener?.onUpdated(NumberKeyboardData(updated, decimalSeparator, groupingSeparator, currencySymbol))
115110
}
116111
}

sample/composeApp/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212

1313
val versionMajor = 3 // API Changes, adding big new feature, redesign the App
1414
val versionMinor = 0 // New features in a backwards-compatible manner
15-
val versionPatch = 0 // Backwards-compatible bug fixes
15+
val versionPatch = 1 // Backwards-compatible bug fixes
1616
val versionClassifier: String? = null // Pre-releases (alpha, beta, rc, SNAPSHOT...)
1717

1818
kotlin {

sample/composeApp/src/commonMain/kotlin/com/davidmiguel/numberkeyboard/sample/BiometricScreen.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ fun BiometricScreen(innerPadding: PaddingValues) {
5454

5555
Spacer(modifier = Modifier.height(16.dp))
5656

57-
var text by remember { mutableStateOf("0") }
57+
var amount by remember { mutableStateOf("0") }
5858

5959
Text(
60-
text = text,
60+
text = amount,
6161
style = MaterialTheme.typography.displayLarge,
6262
)
6363

@@ -67,10 +67,8 @@ fun BiometricScreen(innerPadding: PaddingValues) {
6767
.weight(1F)
6868
.height(48.dp)
6969
val buttonTextStyle = MaterialTheme.typography.titleMedium
70-
var amount by remember { mutableStateOf("") }
7170
NumberKeyboard(
7271
amount = amount,
73-
onAmountChanged = { amount = it },
7472
maxAllowedAmount = 9_999.00,
7573
maxAllowedDecimals = 0,
7674
roundUpToMax = false,
@@ -87,9 +85,7 @@ fun BiometricScreen(innerPadding: PaddingValues) {
8785
modifier = buttonModifier,
8886
textStyle = buttonTextStyle,
8987
imageVector = Icons.Rounded.Fingerprint,
90-
clicked = {
91-
// Toast.makeText(context, "Biometrics Triggered", Toast.LENGTH_SHORT).show()
92-
}
88+
clicked = {}
9389
)
9490
},
9591
rightAuxButton = { clickedListener ->
@@ -102,7 +98,7 @@ fun BiometricScreen(innerPadding: PaddingValues) {
10298
},
10399
listener = object : NumberKeyboardListener {
104100
override fun onUpdated(data: NumberKeyboardData) {
105-
text = data.int.toString()
101+
amount = data.int.toString()
106102
}
107103
}
108104
)

sample/composeApp/src/commonMain/kotlin/com/davidmiguel/numberkeyboard/sample/CustomScreen.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ fun CustomScreen(innerPadding: PaddingValues) {
8989
}
9090
}
9191

92-
var text by remember { mutableStateOf("$currencySymbol 0") }
92+
var amountWithCurrency by remember { mutableStateOf("$currencySymbol 0") }
93+
var amount by remember { mutableStateOf("") }
9394

9495
Text(
95-
text = text,
96+
text = amountWithCurrency,
9697
style = MaterialTheme.typography.displayLarge,
9798
)
9899

@@ -102,10 +103,8 @@ fun CustomScreen(innerPadding: PaddingValues) {
102103
.weight(1F)
103104
.aspectRatio(1F)
104105
val buttonTextStyle = MaterialTheme.typography.titleMedium
105-
var amount by remember { mutableStateOf("") }
106106
NumberKeyboard(
107107
amount = amount,
108-
onAmountChanged = { amount = it },
109108
maxAllowedAmount = 8_888.888,
110109
maxAllowedDecimals = 3,
111110
currencySymbol = currencySymbol,
@@ -141,7 +140,8 @@ fun CustomScreen(innerPadding: PaddingValues) {
141140
groupingSeparator = getGroupingSeparator(),
142141
listener = object : NumberKeyboardListener {
143142
override fun onUpdated(data: NumberKeyboardData) {
144-
text = data.currency
143+
amountWithCurrency = data.currency
144+
amount = data.rawAmount
145145
}
146146
}
147147
)

sample/composeApp/src/commonMain/kotlin/com/davidmiguel/numberkeyboard/sample/DecimalScreen.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ fun DecimalScreen(innerPadding: PaddingValues) {
4646

4747
Spacer(modifier = Modifier.height(16.dp))
4848

49-
var text by remember { mutableStateOf("$currencySymbol 0") }
49+
var amountWithCurrency by remember { mutableStateOf("$currencySymbol 0") }
50+
var amount by remember { mutableStateOf("") }
5051

5152
Text(
52-
text = text,
53+
text = amountWithCurrency,
5354
style = MaterialTheme.typography.displayLarge,
5455
)
5556

@@ -59,10 +60,8 @@ fun DecimalScreen(innerPadding: PaddingValues) {
5960
.weight(1F)
6061
.height(48.dp)
6162
val buttonTextStyle = MaterialTheme.typography.titleMedium
62-
var amount by remember { mutableStateOf("") }
6363
NumberKeyboard(
6464
amount = amount,
65-
onAmountChanged = { amount = it },
6665
button = { number, clickedListener ->
6766
NumberKeyboardButton(
6867
modifier = buttonModifier,
@@ -89,7 +88,8 @@ fun DecimalScreen(innerPadding: PaddingValues) {
8988
},
9089
listener = object : NumberKeyboardListener {
9190
override fun onUpdated(data: NumberKeyboardData) {
92-
text = data.currency
91+
amountWithCurrency = data.currency
92+
amount = data.rawAmount
9393
}
9494
}
9595
)

sample/composeApp/src/commonMain/kotlin/com/davidmiguel/numberkeyboard/sample/IntegerScreen.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ fun IntegerScreen(innerPadding: PaddingValues) {
5151

5252
Spacer(modifier = Modifier.height(16.dp))
5353

54-
var text by remember { mutableStateOf("0") }
54+
var amount by remember { mutableStateOf("0") }
5555

5656
Text(
57-
text = text,
57+
text = amount,
5858
style = MaterialTheme.typography.displayLarge,
5959
)
6060

@@ -64,10 +64,8 @@ fun IntegerScreen(innerPadding: PaddingValues) {
6464
.weight(1F)
6565
.height(48.dp)
6666
val buttonTextStyle = MaterialTheme.typography.titleMedium
67-
var amount by remember { mutableStateOf("") }
6867
NumberKeyboard(
6968
amount = amount,
70-
onAmountChanged = { amount = it },
7169
maxAllowedDecimals = 0,
7270
button = { number, clickedListener ->
7371
NumberKeyboardButton(
@@ -87,7 +85,7 @@ fun IntegerScreen(innerPadding: PaddingValues) {
8785
},
8886
listener = object : NumberKeyboardListener {
8987
override fun onUpdated(data: NumberKeyboardData) {
90-
text = data.int.toString()
88+
amount = data.int.toString()
9189
}
9290
}
9391
)

0 commit comments

Comments
 (0)