-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4fc46a
commit d759d3f
Showing
20 changed files
with
484 additions
and
557 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
105 changes: 0 additions & 105 deletions
105
MaskedEditText/src/main/java/com/vicmikhailau/maskededittext/IFormattedString.java
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
MaskedEditText/src/main/java/com/vicmikhailau/maskededittext/IFormattedString.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,84 @@ | ||
package com.vicmikhailau.maskededittext | ||
|
||
|
||
interface IFormattedString : CharSequence { | ||
val unMaskedString: String | ||
} | ||
|
||
|
||
internal abstract class AbstractFormattedString(val mMask: Mask, val inputString: String) : IFormattedString { | ||
private var mFormattedString: String? = null | ||
final override val unMaskedString: String | ||
|
||
|
||
init { | ||
unMaskedString = this.buildRawString(inputString) | ||
} | ||
|
||
|
||
internal abstract fun formatString(): String | ||
|
||
internal abstract fun buildRawString(str: String): String | ||
|
||
|
||
override val length: Int | ||
get() = toString().length | ||
|
||
override fun toString(): String { | ||
if (mFormattedString == null) { | ||
mFormattedString = formatString() | ||
} | ||
return mFormattedString!! | ||
} | ||
|
||
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence { | ||
return toString().subSequence(startIndex, endIndex) | ||
} | ||
|
||
override fun get(index: Int): Char { | ||
return toString()[index] | ||
} | ||
} | ||
|
||
internal class FormattedString(mask: Mask, rawString: String) : AbstractFormattedString(mask, rawString) { | ||
|
||
override fun buildRawString(str: String): String { | ||
val builder = StringBuilder() | ||
val inputLen = mMask.size().coerceAtMost(str.length) | ||
for (i in 0 until inputLen) { | ||
val ch = str[i] | ||
if (!mMask.isValidPrepopulateCharacter(ch, i)) | ||
builder.append(ch) | ||
} | ||
return builder.toString() | ||
} | ||
|
||
override fun formatString(): String { | ||
val builder = StringBuilder() | ||
|
||
var strIndex = 0 | ||
var maskCharIndex = 0 | ||
var stringCharacter: Char | ||
|
||
while (strIndex < inputString.length && maskCharIndex < mMask.size()) { | ||
val maskChar = mMask[maskCharIndex] | ||
|
||
stringCharacter = inputString[strIndex] | ||
|
||
when { | ||
maskChar.isValidCharacter(stringCharacter) -> { | ||
builder.append(maskChar.processCharacter(stringCharacter)) | ||
strIndex += 1 | ||
maskCharIndex += 1 | ||
} | ||
maskChar.isPrepopulate -> { | ||
builder.append(maskChar.processCharacter(stringCharacter)) | ||
maskCharIndex += 1 | ||
} | ||
else -> strIndex += 1 | ||
} | ||
} | ||
|
||
return builder.toString() | ||
} | ||
} |
75 changes: 0 additions & 75 deletions
75
MaskedEditText/src/main/java/com/vicmikhailau/maskededittext/Mask.java
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
MaskedEditText/src/main/java/com/vicmikhailau/maskededittext/Mask.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,65 @@ | ||
package com.vicmikhailau.maskededittext | ||
|
||
import java.util.* | ||
|
||
|
||
class Mask() { | ||
lateinit var formatString: String | ||
private lateinit var mMask: List<MaskCharacter> | ||
private val mFabric: MaskCharacterFabric = MaskCharacterFabric() | ||
private var mPrepopulateCharacter: MutableList<MaskCharacter>? = null | ||
|
||
constructor(fmtString: String) : this() { | ||
formatString = fmtString | ||
mMask = buildMask(formatString) | ||
} | ||
|
||
fun size(): Int { | ||
return mMask.size | ||
} | ||
|
||
operator fun get(index: Int): MaskCharacter { | ||
return mMask[index] | ||
} | ||
|
||
|
||
fun isValidPrepopulateCharacter(ch: Char, at: Int): Boolean { | ||
return try { | ||
val character = mMask[at] | ||
character.isPrepopulate && character.isValidCharacter(ch) | ||
} catch (e: IndexOutOfBoundsException) { | ||
false | ||
} | ||
|
||
} | ||
|
||
fun isValidPrepopulateCharacter(ch: Char): Boolean { | ||
for (maskCharacter in mPrepopulateCharacter!!) { | ||
if (maskCharacter.isValidCharacter(ch)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
|
||
private fun buildMask(fmtString: String): List<MaskCharacter> { | ||
val result = ArrayList<MaskCharacter>() | ||
mPrepopulateCharacter = ArrayList() | ||
for (ch in fmtString.toCharArray()) { | ||
val maskCharacter = mFabric.buildCharacter(ch) | ||
if (maskCharacter?.isPrepopulate == true) { | ||
mPrepopulateCharacter!!.add(maskCharacter) | ||
} | ||
result.add(maskCharacter!!) | ||
} | ||
return result | ||
} | ||
|
||
|
||
fun getFormattedString(value: String): IFormattedString { | ||
return FormattedString(this, value) | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.