Skip to content

Commit 8acad75

Browse files
authored
Merge pull request #1 from ihermandev/develop
add possibility to get formatted and raw text from the FormatWatcher
2 parents 8281269 + 5a8418d commit 8acad75

File tree

7 files changed

+125
-22
lines changed

7 files changed

+125
-22
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## v1.0.1
2+
- Added functionality in order to get the formatted and raw text
3+
4+
## v1.0.0
5+
- The first stable release

README.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# FormatWatcher
2-
![version](https://img.shields.io/badge/format--watcher-v1.0.0-blue)
2+
![version](https://img.shields.io/badge/format--watcher-v1.0.1-blue)
33

44
The FormatWatcher library provides a simple and easy-to-use way to apply predefined formatting to user
55
input for Android EditText. The formatter is separator sensitive, meaning if a separator is entered
@@ -13,17 +13,25 @@ repositories {
1313
}
1414
1515
dependencies {
16-
implementation 'io.github.ihermandev:format-watcher:1.0.0'
16+
implementation 'io.github.ihermandev:format-watcher:1.0.1'
1717
}
1818
```
1919
## Usage
20-
Simply create a new instance of the FormatWatcher and set it as the TextWatcher for the EditText.
20+
Simply create a new instance of the `FormatWatcher` and set it as the `TextWatcher` interface for
21+
the `EditText`.
2122
```kotlin
2223
val formatter = FormatWatcher("###-##-####", '#')
2324
editText.addTextChangedListener(formatter)
2425
```
25-
The first argument passed to the FormatWatcher constructor is the format string to be applied to the
26+
The first argument passed to the `FormatWatcher` constructor is the format string to be applied to the
2627
text, the second argument is the character to be used as a placeholder in the format string.
28+
29+
In order to obtain the formatted or raw input you can call `currentInput` and `rawInput` accordingly
30+
via `FormatWatcher` instance.
31+
```kotlin
32+
formatter.currentInput
33+
formatter.rawInput
34+
```
2735
## Customization
2836
You can customize the behavior of the FormatWatcher class by changing the format string, and the
2937
placeholder character to match your specific requirements.

format-watcher/build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ plugins {
55

66
ext {
77
PUBLISH_GROUP_ID = 'io.github.ihermandev'
8-
PUBLISH_VERSION = '1.0.0'
98
PUBLISH_ARTIFACT_ID = 'format-watcher'
109
}
1110

format-watcher/src/main/java/com/github/ihermandev/formatwatcher/FormatWatcher.kt

+39-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ class FormatWatcher(
3535
format.length
3636
}
3737

38+
// Current formatted editable input
39+
var currentInput = ""
40+
private set
41+
42+
// Raw input without format applied
43+
val rawInput: String
44+
get() = if (currentInput.isNotEmpty()) {
45+
getRawInput(currentInput)
46+
} else currentInput
47+
48+
3849
/**
3950
* @see [android.text.TextWatcher.onTextChanged]
4051
*/
@@ -58,7 +69,10 @@ class FormatWatcher(
5869
// Check if the format is already in use
5970
if (isFormatApplying) return
6071
// Check if the editable is removing
61-
if (isCharRemoving) return
72+
if (isCharRemoving) {
73+
currentInput = editable.toString()
74+
return
75+
}
6276

6377
isFormatApplying = true
6478

@@ -87,6 +101,8 @@ class FormatWatcher(
87101
}
88102

89103
}
104+
105+
currentInput = editable.toString()
90106
// Reset flags
91107
isFormatApplying = false
92108
isInputCopied = false
@@ -118,6 +134,28 @@ class FormatWatcher(
118134
}
119135
}
120136

137+
/**
138+
* Removes all non-placeholder character
139+
*
140+
* @param input representing text from which the formatting should be removed.
141+
*/
142+
private fun getRawInput(input: String): String {
143+
val output = StringBuilder()
144+
145+
for (i in input.indices) {
146+
val inputChar = input[i]
147+
if (i < formatLength) {
148+
if (format[i] == placeholderInFormat) {
149+
output.append(inputChar)
150+
}
151+
} else {
152+
output.append(inputChar)
153+
}
154+
}
155+
156+
return output.toString()
157+
}
158+
121159
companion object {
122160
const val DEFAULT_CHAR_IN_FORMAT = '*'
123161
}

gradle/publish-module.gradle

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ apply plugin: 'maven-publish'
22
apply plugin: 'signing'
33
apply plugin: 'org.jetbrains.dokka'
44

5+
apply from: "$rootProject.projectDir/gradle/version.gradle"
6+
57
task androidSourcesJar(type: Jar) {
68
archiveClassifier.set('sources')
79
if (project.plugins.findPlugin("com.android.library")) {
@@ -32,7 +34,7 @@ artifacts {
3234
}
3335

3436
group = PUBLISH_GROUP_ID
35-
version = PUBLISH_VERSION
37+
version = versionName
3638

3739
afterEvaluate {
3840
publishing {
@@ -41,7 +43,7 @@ afterEvaluate {
4143
// The coordinates of the library
4244
groupId PUBLISH_GROUP_ID
4345
artifactId PUBLISH_ARTIFACT_ID
44-
version PUBLISH_VERSION
46+
version versionName
4547

4648
// Two artifacts, the `aar` (or `jar`) and the sources
4749
if (project.plugins.findPlugin("com.android.library")) {

sample/src/main/java/com/github/ihermandev/sample/MainActivity.kt

+64-14
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import android.os.Bundle
44
import android.widget.EditText
55
import androidx.annotation.StringRes
66
import androidx.appcompat.app.AppCompatActivity
7+
import androidx.constraintlayout.widget.ConstraintLayout
78
import com.github.ihermandev.formatwatcher.FormatWatcher
9+
import com.google.android.material.snackbar.Snackbar
810

911
class MainActivity : AppCompatActivity() {
1012

13+
private val rootContainer by lazy {
14+
findViewById<ConstraintLayout>(R.id.container)
15+
}
16+
1117
private val editText1 by lazy {
1218
findViewById<EditText>(R.id.editText1)
1319
}
@@ -36,25 +42,69 @@ class MainActivity : AppCompatActivity() {
3642
findViewById<EditText>(R.id.editText7)
3743
}
3844

45+
private val formatWatcher1 by lazy {
46+
FormatWatcher(getStringResource(R.string.format_set1),
47+
placeholderInFormat = '#')
48+
}
49+
50+
private val formatWatcher2 by lazy {
51+
FormatWatcher(getStringResource(R.string.format_set2),
52+
placeholderInFormat = '*')
53+
}
54+
55+
private val formatWatcher3 by lazy {
56+
FormatWatcher(getStringResource(R.string.format_set3),
57+
placeholderInFormat = '#')
58+
}
59+
60+
private val formatWatcher4 by lazy {
61+
FormatWatcher(getStringResource(R.string.format_set4),
62+
placeholderInFormat = '#')
63+
}
64+
65+
private val formatWatcher5 by lazy {
66+
FormatWatcher(getStringResource(R.string.format_set5),
67+
placeholderInFormat = '*')
68+
}
69+
70+
private val formatWatcher6 by lazy {
71+
FormatWatcher(getStringResource(R.string.format_set6),
72+
placeholderInFormat = '#')
73+
}
74+
75+
private val formatWatcher7 by lazy {
76+
FormatWatcher(getStringResource(R.string.format_set7),
77+
placeholderInFormat = '#')
78+
}
79+
3980
override fun onCreate(savedInstanceState: Bundle?) {
4081
super.onCreate(savedInstanceState)
4182
setContentView(R.layout.activity_main)
4283

43-
editText1.addTextChangedListener(FormatWatcher(getStringResource(R.string.format_set1),
44-
placeholderInFormat = '#'))
45-
editText2.addTextChangedListener(FormatWatcher(getStringResource(R.string.format_set2),
46-
placeholderInFormat = '*'))
47-
editText3.addTextChangedListener(FormatWatcher(getStringResource(R.string.format_set3),
48-
placeholderInFormat = '#'))
49-
editText4.addTextChangedListener(FormatWatcher(getStringResource(R.string.format_set4),
50-
placeholderInFormat = '#'))
51-
editText5.addTextChangedListener(FormatWatcher(getStringResource(R.string.format_set5),
52-
placeholderInFormat = '*'))
53-
editText6.addTextChangedListener(FormatWatcher(getStringResource(R.string.format_set6),
54-
placeholderInFormat = '#'))
55-
editText7.addTextChangedListener(FormatWatcher(getStringResource(R.string.format_set7),
56-
placeholderInFormat = '#'))
84+
editText1.handleEditTextFormatting(formatWatcher1)
85+
editText2.handleEditTextFormatting(formatWatcher2)
86+
editText3.handleEditTextFormatting(formatWatcher3)
87+
editText4.handleEditTextFormatting(formatWatcher4)
88+
editText5.handleEditTextFormatting(formatWatcher5)
89+
editText6.handleEditTextFormatting(formatWatcher6)
90+
editText7.handleEditTextFormatting(formatWatcher7)
91+
}
92+
93+
private fun EditText.handleEditTextFormatting(
94+
formatWatcher: FormatWatcher,
95+
) = this.apply {
96+
addTextChangedListener(formatWatcher)
97+
setOnLongClickListener {
98+
showSnackbarMessage(formatWatcher.rawInput, formatWatcher.currentInput)
99+
true
100+
}
57101
}
58102

59103
private fun getStringResource(@StringRes id: Int): String = resources.getString(id)
104+
105+
private fun showSnackbarMessage(rawInput: String, formattedInput: String) =
106+
Snackbar.make(rootContainer,
107+
"Raw input is ${rawInput.ifEmpty { "empty" }}; the length is ${rawInput.length} \n" +
108+
"Formatted input is ${formattedInput.ifEmpty { "empty" }}; the length is ${formattedInput.length}",
109+
Snackbar.LENGTH_SHORT).show()
60110
}

sample/src/main/res/layout/activity_main.xml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/container"
45
android:layout_width="match_parent"
56
android:layout_height="match_parent"
67
tools:context=".MainActivity">

0 commit comments

Comments
 (0)