Skip to content

Commit

Permalink
add builder to verify input
Browse files Browse the repository at this point in the history
  • Loading branch information
ghasemdev committed Feb 19, 2021
1 parent 66e9e58 commit 8b19dd1
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 25 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions VerifyCodeEditText/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
defaultConfig {
minSdkVersion 17
targetSdkVersion 30
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.jakode.verifycodeedittext
import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.text.InputType
import android.util.AttributeSet
Expand All @@ -18,11 +19,7 @@ import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat

class VerifyCodeEditText(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int
) : LinearLayout(context, attrs, defStyleAttr) {
class VerifyCodeEditText(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context) : this(context, null)

Expand Down Expand Up @@ -57,18 +54,22 @@ class VerifyCodeEditText(
private var bottomIconWidth = 0

init {
setupField(attrs, defStyleAttr)
// Layout Settings
orientation = HORIZONTAL
layoutDirection = LAYOUT_DIRECTION_LTR
gravity = Gravity.CENTER
isFocusableInTouchMode = true

// Initialize
if (!::stringBuilder.isInitialized) stringBuilder = StringBuilder()
if (!::viewList.isInitialized) viewList = ArrayList()

// Setup Bottom Icons
setupField(attrs, defStyleAttr) // Setup filed
setupBottomIcon() // Setup Bottom Icons
}

private fun setupBottomIcon() {
viewList.forEach { removeView(it) }
viewList.clear()
for (i in 0 until viewCount) {
val underLineCodeView: TextView = getUnderLineIcon(i)
viewList.add(underLineCodeView)
Expand Down Expand Up @@ -210,4 +211,66 @@ class VerifyCodeEditText(

private fun getColorFromRes(colorRes: Int) = ContextCompat.getColor(context, colorRes)
private fun getDrawableFromRes(drawableRes: Int) = ContextCompat.getDrawable(context, drawableRes)

class Builder(initialize: Builder.() -> Unit) {
init { initialize() }

private var textHolder: Text? = null
private var bottomIconHolder: BottomIcon? = null
private var verifyCell: VerifyCell? = null

fun text(initialize: Text.() -> Unit) {
textHolder = Text().apply { initialize() }
}

fun bottomIcon(initialize: BottomIcon.() -> Unit) {
bottomIconHolder = BottomIcon().apply { initialize() }
}

fun verifyCell(initialize: VerifyCell.() -> Unit) {
verifyCell = VerifyCell().apply { initialize() }
}

fun build(context: Context): VerifyCodeEditText {
return VerifyCodeEditText(context).apply {
textHolder?.apply {
textSize = size
textColor = color
textFontRes = fontRes
}
bottomIconHolder?.apply {
bottomSelectedIcon = selectedIcon
bottomUnSelectedIcon = unSelectedIcon
bottomErrorIcon = errorIcon
bottomIconHeight = iconHeight
bottomIconWidth = iconWidth
}
verifyCell?.apply {
viewCount = count.value
itemCenterSpaceSize = spaceSize
}
}.also { it.setupBottomIcon() }
}

data class Text(
var size: Float = 18f,
var color: Int = Color.parseColor("#000000"),
var fontRes: Int = Int.MIN_VALUE
)

data class BottomIcon(
var selectedIcon: Drawable? = null,
var unSelectedIcon: Drawable? = null,
var errorIcon: Drawable? = null,
var iconHeight: Int = 1,
var iconWidth: Int = 32,
)

data class VerifyCell(
var count: ViewCount = ViewCount.Four,
var spaceSize: Int = 18
)

enum class ViewCount(val value: Int) { Four(4), Five(5), Six(6) }
}
}
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.jakode.verifycodeedittexttest"
minSdkVersion 17
targetSdkVersion 30
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,33 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val verifyCodeEditText = findViewById<VerifyCodeEditText>(R.id.verifyCodeEditText)
val verifyCodeEditText = findViewById<VerifyCodeEditText>(R.id.verify)
verifyCodeEditText.setCompleteListener { complete ->
if (complete) {
Toast.makeText(this@MainActivity, verifyCodeEditText.text, Toast.LENGTH_SHORT).show()
Toast.makeText(this@MainActivity, verifyCodeEditText.text, Toast.LENGTH_SHORT)
.show()
verifyCodeEditText.setCodeItemErrorLineDrawable()
}
}
verifyCodeEditText.text = "99999"

/*val verifyCodeEditText = VerifyCodeEditText.Builder {
text {
size = 20F
color = Color.parseColor("#000000")
}
bottomIcon {
iconHeight = 5
iconWidth = 60
selectedIcon = ContextCompat.getDrawable(this@MainActivity, R.drawable.bottom_selected_icon)
unSelectedIcon = ContextCompat.getDrawable(this@MainActivity, R.drawable.bottom_unselected_icon)
errorIcon = ContextCompat.getDrawable(this@MainActivity, R.drawable.bottom_error_icon)
}
verifyCell {
count = VerifyCodeEditText.Builder.ViewCount.Five
spaceSize = 48
}
}.build(context = this)
findViewById<ConstraintLayout>(R.id.layout).addView(verifyCodeEditText)*/
}
}
8 changes: 0 additions & 8 deletions app/src/main/res/drawable/stroke.xml

This file was deleted.

6 changes: 3 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
xmlns:tools="http:https://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.jakode.verifycodeedittext.VerifyCodeEditText
android:id="@+id/verifyCodeEditText"
android:id="@+id/verify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/stroke"
android:layoutDirection="ltr"
android:paddingBottom="12dp"
app:BottomIconWidth="40dp"
app:ItemSpaceSize="28dp"
app:TextColor="@color/black"
app:TextSize="16sp"
app:ViewCount="Five"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down

0 comments on commit 8b19dd1

Please sign in to comment.