-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from jkuatdsc/feature/card-number-formatting
Formatting feature
- Loading branch information
Showing
20 changed files
with
211 additions
and
39 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
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
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
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
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
15 changes: 15 additions & 0 deletions
15
form-builder/src/main/java/com/dsc/form_builder/format/CardFormatter.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,15 @@ | ||
package com.dsc.form_builder.format | ||
|
||
/** | ||
* | ||
* This formatter is used for credit/debit card numbers. Can also be used for IBAN numbers. | ||
* It groups the input value into chunks of four characters. | ||
* The separator is an empty space as this is the most common option. | ||
* | ||
* Note: character limiting is not supported in the formatter. | ||
*/ | ||
object CardFormatter: Formatter { | ||
override fun format(value: String): String { | ||
return value.chunked(4).joinToString(" ") | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
form-builder/src/main/java/com/dsc/form_builder/format/DateFormat.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,56 @@ | ||
package com.dsc.form_builder.format | ||
|
||
/** | ||
* These are the formatting options for the [DateFormatter] class. | ||
*/ | ||
enum class DateFormat { | ||
DDMMYYYY, | ||
MMDDYYYY, | ||
YYYYDDMM, | ||
DDMMYY, | ||
MMDDYY, | ||
YYMMDD, | ||
} | ||
|
||
// Get the index where to place the separator | ||
private fun DateFormat.separatorIndices(): MutableList<Int> { | ||
val indices = mutableListOf<Int>() | ||
val stringFormat = this.toString() | ||
stringFormat.forEachIndexed { index, char -> | ||
if (index > 0){ | ||
val prev = stringFormat[index-1] | ||
|
||
if (prev != char) { | ||
if (indices.isNotEmpty()) indices.add(index+1) | ||
else indices.add(index) | ||
} | ||
} | ||
} | ||
return indices | ||
} | ||
|
||
|
||
/** | ||
* | ||
* This formatter is used for date inputs. You need to specify a [DateFormat] and a separator. | ||
* The formatting function places the separator in the respective index as the user types. | ||
* | ||
* Note: character limiting is not supported in the formatter. | ||
*/ | ||
|
||
class DateFormatter(private val dateFormat: DateFormat, private val separator: String): Formatter { | ||
override fun format(value: String): String { | ||
var formatted = value | ||
val indices = dateFormat.separatorIndices() | ||
|
||
// add first separator if user has exceeded that index | ||
val first = indices.first() | ||
if (value.length > first) formatted = formatted.replaceRange(first, first, separator) | ||
|
||
// add last separator if user has exceeded that index | ||
val last = indices.last() | ||
if (value.length >= last) formatted = formatted.replaceRange(last, last, separator) | ||
|
||
return formatted | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
form-builder/src/main/java/com/dsc/form_builder/format/Formatter.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,29 @@ | ||
package com.dsc.form_builder.format | ||
|
||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.input.OffsetMapping | ||
import androidx.compose.ui.text.input.TransformedText | ||
import androidx.compose.ui.text.input.VisualTransformation | ||
|
||
|
||
/** | ||
* | ||
* These are the formatting interface for the [TextFieldState]. | ||
* You can get the visual transformation to apply in your text input. | ||
*/ | ||
interface Formatter { | ||
fun format(value: String): String | ||
} | ||
|
||
internal fun Formatter.toVisualTransformation(): VisualTransformation { | ||
return VisualTransformation { | ||
val output = format(it.text) | ||
TransformedText( | ||
AnnotatedString(output), | ||
object : OffsetMapping { | ||
override fun originalToTransformed(offset: Int): Int = output.length | ||
override fun transformedToOriginal(offset: Int): Int = it.text.length | ||
} | ||
) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -39,4 +39,4 @@ internal class FormStateTest { | |
assert(ageState.value == "34" && !ageState.hasError) | ||
} | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
form-builder/src/test/java/com/dsc/form_builder/FormatterTest.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,42 @@ | ||
package com.dsc.form_builder | ||
|
||
import com.dsc.form_builder.format.CardFormatter | ||
import com.dsc.form_builder.format.DateFormat | ||
import com.dsc.form_builder.format.DateFormatter | ||
import com.dsc.form_builder.format.Formatter | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ArgumentsSource | ||
|
||
internal class FormatterTest { | ||
|
||
@Nested | ||
inner class DescribingFormatting { | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(CreditCardFormatterProvider::class) | ||
fun `credit card numbers are formatted correctly`(input: String, expected: String){ | ||
// Given a formatting class | ||
val classToTest: Formatter = CardFormatter | ||
|
||
// When formatting is applied | ||
val formatted = classToTest.format(input) | ||
|
||
// then the value should be formatted correctly | ||
assert(formatted == expected) | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(DateFormatterProvider::class) | ||
fun `dater inputs are formatted correctly`(format: DateFormat, separator: String, input: String, expected: String){ | ||
// Given a formatting class | ||
val classToTest: Formatter = DateFormatter(dateFormat = format, separator = separator) | ||
|
||
// When formatting is applied | ||
val formatted = classToTest.format(input) | ||
|
||
// then the value should be formatted correctly | ||
assert(formatted == expected) | ||
} | ||
} | ||
} |
Oops, something went wrong.