Skip to content

Commit

Permalink
add formatters docs
Browse files Browse the repository at this point in the history
  • Loading branch information
joykangangi committed Sep 24, 2024
1 parent 84d02e6 commit fdcbcf6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package com.dsc.form_builder.format
* The separator is an empty space as this is the most common option.
*
* Note: character limiting is not supported in the formatter.
*
* @author [Linus Muema](https://github.com/linusmuema)
*/
object CardFormatter: Formatter {
override fun format(value: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.dsc.form_builder.format

/**
* These are the formatting options for the [DateFormatter] class.
*
* @author [Joy Kangangi](https://github.com/joykangangi)
*/
enum class DateFormat(val pattern: String) {
DDMMYYYY("ddMMuuuu"),
Expand All @@ -12,7 +14,17 @@ enum class DateFormat(val pattern: String) {
YYMMDD("uuMMdd")
}

// Get the index where to place the separator
/**
* Determines the indices where separators should be placed in a [DateFormat].
*
* This function iterates over the characters of the [DateFormat] string representation
* and identifies the positions where the format changes, such as from one character type to another.
* These positions are used to insert separators between different segments of the date format.
*
* @return A mutable list of integers representing the positions where separators should be placed.
*
* @author [Linus Muema](https://github.com/linusmuema)
*/
private fun DateFormat.separatorIndices(): MutableList<Int> {
val indices = mutableListOf<Int>()
val stringFormat = this.toString()
Expand All @@ -36,6 +48,8 @@ private fun DateFormat.separatorIndices(): MutableList<Int> {
* The formatting function places the separator in the respective index as the user types.
*
* Note: character limiting is not supported in the formatter.
*
* @author [Linus Muema](https://github.com/linusmuema)
*/

class DateFormatter(private val dateFormat: DateFormat, private val separator: String) : Formatter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,26 @@ 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.
*
* @author [Linus Muema](https://github.com/linusmuema)
*/
interface Formatter {
fun format(value: String): String
}


/**
* Converts a [Formatter] to a [VisualTransformation] that can be applied to text input fields.
* This transformation modifies how the text is visually presented based on the provided [Formatter].
*
* The function applies the [Formatter.format] method to the input text and returns a [TransformedText],
* ensuring that the visual representation of the text differs from the raw input, while keeping the cursor
* offset mapping consistent with the original text.
*
* @return A [VisualTransformation] that formats the input text based on the [Formatter].
*
* @author [Linus Muema](https://github.com/linusmuema)
*/
internal fun Formatter.toVisualTransformation(): VisualTransformation {
return VisualTransformation {
val output = format(it.text)
Expand Down

0 comments on commit fdcbcf6

Please sign in to comment.