Skip to content

Commit

Permalink
filter paste text and extract numbers
Browse files Browse the repository at this point in the history
convert arabic and persian numbers to english
  • Loading branch information
Masoud Ashrafzadeh committed Feb 2, 2020
1 parent a569220 commit 0b7b842
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
app:ce_number_count="16"
android:textSize="32dp"
app:ce_part_count="4"
android:textColor="#ff0000"
android:textColor="@color/coal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
53 changes: 30 additions & 23 deletions cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sma6871.cardentry

import android.animation.Animator
import android.animation.ValueAnimator
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
Expand All @@ -13,9 +14,6 @@ import android.text.InputType
import android.text.TextWatcher
import android.text.method.DigitsKeyListener
import android.util.AttributeSet
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -53,8 +51,11 @@ class CardEntry : AppCompatEditText {


var lineColor = getColor(R.color.silverGray)
var selectionColor = getColor(R.color.selection)
var filledLineColor = getColor(R.color.green)

var selectionRect = Rect()
var selectionPaint = Paint()

fun onPinChange(onChange: (isComplete: Boolean, length: Int) -> Unit) {
addTextChangedListener(object : TextWatcher {
Expand Down Expand Up @@ -104,6 +105,12 @@ class CardEntry : AppCompatEditText {
ContextCompat.getColor(context, R.color.silverGray)
)
}
if (typedArray.hasValue(R.styleable.CardEntry_ce_selection_color)) {
selectionColor = typedArray.getColor(
R.styleable.CardEntry_ce_selection_color,
ContextCompat.getColor(context, R.color.selection)
)
}

if (typedArray.hasValue(R.styleable.CardEntry_ce_filled_line_color))
filledLineColor = typedArray.getInt(
Expand Down Expand Up @@ -139,8 +146,6 @@ class CardEntry : AppCompatEditText {
typedArray.recycle()

setBackgroundResource(0)
setTextIsSelectable(false)
isCursorVisible = false
inputType = InputType.TYPE_CLASS_NUMBER
keyListener = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
DigitsKeyListener.getInstance(Locale.US)
Expand All @@ -151,24 +156,6 @@ class CardEntry : AppCompatEditText {
val lengthFilter = InputFilter.LengthFilter(maxLength)
filters = arrayOf<InputFilter>(lengthFilter)

//Disable copy paste
super.setCustomSelectionActionModeCallback(object : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
return false
}

override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}

override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}

override fun onDestroyActionMode(mode: ActionMode?) {
}

})

addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
Expand All @@ -193,6 +180,20 @@ class CardEntry : AppCompatEditText {

}

override fun onTextContextMenuItem(id: Int): Boolean {
when (id) {
android.R.id.paste -> pasteNumbers()
else -> return super.onTextContextMenuItem(id)
}
return true
}

private fun pasteNumbers() {
val clipboardManager = context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = clipboardManager.primaryClip
setText(clipData?.getItemAt(0)?.text?.onlyNumber())
}

override fun onDraw(canvas: Canvas) {
//super.onDraw(canvas)

Expand Down Expand Up @@ -259,11 +260,17 @@ class CardEntry : AppCompatEditText {
}

private fun drawNumber(canvas: Canvas, text: CharSequence, i: Int, middle: Float, top: Int, animated: Boolean) {
val isSelected = i in selectionStart until selectionEnd
if (animated) {
paint.alpha = animatedAlpha
} else {
paint.alpha = 255
}
//TODO: draw selected background
paint.color = if (isSelected)
selectionColor
else
textColors.defaultColor
canvas.drawText(
text,
i,
Expand Down
34 changes: 25 additions & 9 deletions cardentry/src/main/java/com/sma6871/cardentry/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat


fun View.toPxF(dp:Int): Float{
fun View.toPxF(dp: Int): Float {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp.toFloat(),
resources.displayMetrics)
TypedValue.COMPLEX_UNIT_DIP,
dp.toFloat(),
resources.displayMetrics)
}
fun View.spToPxF(sp:Int): Float{

fun View.spToPxF(sp: Int): Float {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
sp.toFloat(),
resources.displayMetrics)
TypedValue.COMPLEX_UNIT_SP,
sp.toFloat(),
resources.displayMetrics)
}

fun View.getColor(@ColorRes colorId:Int):Int {
fun View.getColor(@ColorRes colorId: Int): Int {
return ContextCompat.getColor(context, colorId)
}

fun CharSequence.onlyNumber(): String {
return toString().toEnglishNumbers().trim().replace(Regex("[^0-9]"), "")
}

fun String.toEnglishNumbers(): String {
var result = this
val arabic = arrayOf('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩')
val persian = arrayOf('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹')
('0'..'9').forEachIndexed { index, number ->
result = result.replace(persian[index], number, true)
.replace(arabic[index], number, true)
}
return result
}
1 change: 1 addition & 0 deletions cardentry/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<declare-styleable name="CardEntry">
<attr name="ce_number_count" format="integer"/>
<attr name="ce_part_count" format="integer"/>
<attr name="ce_selection_color" format="color"/>
<attr name="ce_line_color" format="color"/>
<attr name="ce_filled_line_color" format="color"/>
<attr name="ce_has_animation" format="boolean"/>
Expand Down
1 change: 1 addition & 0 deletions cardentry/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<color name="silverGray">#FFE6E6E6</color>
<color name="coal">#FF4A4A4A</color>
<color name="green">#FF47B3B3</color>
<color name="selection">#F85656</color>
</resources>

0 comments on commit 0b7b842

Please sign in to comment.