Skip to content

Commit

Permalink
[*] Moved project to kotlin.
Browse files Browse the repository at this point in the history
  • Loading branch information
VicMikhailau committed Nov 5, 2019
1 parent b4fc46a commit d759d3f
Show file tree
Hide file tree
Showing 20 changed files with 484 additions and 557 deletions.
9 changes: 8 additions & 1 deletion MaskedEditText/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

ext {
bintrayRepo = 'maven'
Expand Down Expand Up @@ -43,13 +45,18 @@ android {

ext {
androidx_appcompat = "1.1.0"
androidx_core_ktx = "1.1.0"
}

dependencies {
api fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
api "androidx.appcompat:appcompat:$androidx_appcompat"
implementation "androidx.core:core-ktx:$androidx_core_ktx"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
repositories {
mavenCentral()
}

This file was deleted.

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()
}
}

This file was deleted.

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)
}


}
Loading

0 comments on commit d759d3f

Please sign in to comment.