Skip to content

Commit fe3caef

Browse files
committed
Fix length otp code
1 parent 9a63ebe commit fe3caef

File tree

8 files changed

+131
-12
lines changed

8 files changed

+131
-12
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
android:supportsRtl="true"
1313
android:theme="@style/Theme.MyApplication"
1414
tools:targetApi="31">
15+
<activity
16+
android:name=".WhiteActivity"
17+
android:exported="false" />
1518
<activity
1619
android:name=".MainActivity"
1720
android:exported="true">

app/src/main/java/com/otp/code/MainActivity.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.otp.code
22

3+
import android.content.Intent
34
import androidx.appcompat.app.AppCompatActivity
45
import android.os.Bundle
56
import android.widget.Toast
@@ -31,6 +32,9 @@ class MainActivity : AppCompatActivity() {
3132
binding.verifyCode.showError("Invalid Code")
3233
}
3334
})
35+
binding.btnChangeBg.setOnClickListener {
36+
startActivity(Intent(this, WhiteActivity::class.java))
37+
}
3438
}
3539

3640
override fun onStart() {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.otp.code
2+
3+
import android.content.Intent
4+
import androidx.appcompat.app.AppCompatActivity
5+
import android.os.Bundle
6+
import android.widget.Toast
7+
import com.otp.code.databinding.WhiteActivityBinding
8+
import kotlinx.coroutines.CoroutineScope
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.Job
11+
import kotlinx.coroutines.delay
12+
import kotlinx.coroutines.launch
13+
import kotlin.random.Random
14+
import kotlin.random.nextInt
15+
16+
class WhiteActivity : AppCompatActivity() {
17+
private lateinit var binding: WhiteActivityBinding
18+
private var job: Job? = null
19+
private var currentCode: String = ""
20+
21+
override fun onCreate(savedInstanceState: Bundle?) {
22+
super.onCreate(savedInstanceState)
23+
binding = WhiteActivityBinding.inflate(layoutInflater)
24+
setContentView(binding.root)
25+
setupViews()
26+
}
27+
28+
private fun setupViews() {
29+
binding.verifyCode.setOnVerifyListener(onVerify = {
30+
if (it == currentCode) {
31+
Toast.makeText(this, "Verify Success", Toast.LENGTH_SHORT).show()
32+
} else {
33+
binding.verifyCode.showError("Invalid Code")
34+
}
35+
})
36+
binding.btnChangeBg.setOnClickListener { finish() }
37+
}
38+
39+
override fun onStart() {
40+
super.onStart()
41+
job = CoroutineScope(Dispatchers.Main).launch {
42+
while (true) {
43+
currentCode = Random.nextInt(999..9999).toString()
44+
binding.tvCode.text = "Code: $currentCode"
45+
delay(60_000)
46+
}
47+
}
48+
}
49+
50+
override fun onStop() {
51+
job?.cancel()
52+
job = null
53+
super.onStop()
54+
}
55+
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/container"
56
android:layout_width="match_parent"
67
android:layout_height="match_parent"
7-
android:background="#2B2B2B"
8+
android:background="@color/color_2b2b2b"
89
tools:context=".MainActivity">
910

1011
<TextView
@@ -22,8 +23,16 @@
2223
android:layout_width="match_parent"
2324
android:layout_height="wrap_content"
2425
android:layout_marginTop="100dp"
25-
app:code_color="#DDAA22"
26-
app:code_radius="20dp"
2726
app:layout_constraintTop_toBottomOf="@id/tv_code" />
2827

28+
<Button
29+
android:id="@+id/btn_change_bg"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:layout_marginTop="100dp"
33+
android:text="Change background"
34+
app:layout_constraintEnd_toEndOf="parent"
35+
app:layout_constraintStart_toStartOf="parent"
36+
app:layout_constraintTop_toBottomOf="@id/verify_code" />
37+
2938
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/container"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context=".MainActivity">
9+
10+
<TextView
11+
android:id="@+id/tv_code"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:layout_marginTop="100dp"
15+
android:textColor="@android:color/black"
16+
app:layout_constraintEnd_toEndOf="parent"
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintTop_toTopOf="parent" />
19+
20+
<com.otp.otp_code_view.OtpCodeView
21+
android:id="@+id/verify_code"
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content"
24+
app:code_color="#CC171717"
25+
app:code_stroke_color="#185C1F"
26+
app:code_done_stroke_color="#1AA327"
27+
app:code_background_color="#667A7979"
28+
app:code_error_color="#A50202"
29+
app:code_stroke_error_color="#F11919"
30+
app:code_length="4"
31+
app:code_radius="10dp"
32+
app:code_stroke_width="2dp"
33+
android:layout_marginTop="100dp"
34+
app:layout_constraintTop_toBottomOf="@id/tv_code" />
35+
36+
<Button
37+
android:id="@+id/btn_change_bg"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:layout_marginTop="100dp"
41+
android:text="Change background"
42+
app:layout_constraintEnd_toEndOf="parent"
43+
app:layout_constraintStart_toStartOf="parent"
44+
app:layout_constraintTop_toBottomOf="@id/verify_code" />
45+
46+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<resources>
33
<color name="black">#FF000000</color>
44
<color name="white">#FFFFFFFF</color>
5+
<color name="color_2b2b2b">#2B2B2B</color>
56
</resources>

otp_code_view/src/main/java/com/otp/otp_code_view/OtpCodeView.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ class OtpCodeView @JvmOverloads constructor(
8585
val allViews = getAllViews()
8686
val listTextView = allViews.drop(1).map { it as TextView }
8787
val mainEditText = allViews[0] as EditText
88-
mainEditText.filters = arrayOf(InputFilter.LengthFilter(listTextView.size))
88+
mainEditText.filters = arrayOf(InputFilter.LengthFilter(codeLength))
8989
mainEditText.setOnClickListener { mainEditText.setSelection(mainEditText.text.length) }
9090
mainEditText.doOnTextChanged { text, _, _, _ -> handleShowCode(text, listTextView) }
91-
listTextView.forEach {
92-
it.setTextColor(codeColor)
93-
it.background = getDrawable(codeStrokeColor, false)
91+
listTextView.forEachIndexed { index, textview ->
92+
if (index >= codeLength) textview.visibility = View.GONE
93+
textview.setTextColor(codeColor)
94+
textview.background = getDrawable(codeStrokeColor, false)
9495
}
9596
}
9697

@@ -99,7 +100,7 @@ class OtpCodeView @JvmOverloads constructor(
99100
text.toString().apply {
100101
handleBackgroundHasCode(listTextView)
101102
handleBackgroundNotHasCode(listTextView)
102-
if (length == listTextView.size) {
103+
if (length == codeLength) {
103104
handleBackgroundDoneEnter(listTextView)
104105
onVerifyDone?.invoke(this)
105106
binding.root.hideKeyBoard()
@@ -108,15 +109,15 @@ class OtpCodeView @JvmOverloads constructor(
108109
}
109110

110111
private fun String.handleBackgroundDoneEnter(listTextView: List<TextView>) {
111-
(listTextView.indices).forEach {
112+
(0 until codeLength).forEach {
112113
listTextView[it].background = getDrawable(codeDoneStrokeColor, true)
113114
listTextView[it].setTextColor(codeColor)
114115
listTextView[it].text = get(it).toString()
115116
}
116117
}
117118

118119
private fun String.handleBackgroundNotHasCode(listTextView: List<TextView>) {
119-
(length until listTextView.size).forEach {
120+
(length until codeLength).forEach {
120121
listTextView[it].background = getDrawable(codeStrokeColor, false)
121122
listTextView[it].setTextColor(codeColor)
122123
listTextView[it].text = ""
@@ -164,7 +165,7 @@ class OtpCodeView @JvmOverloads constructor(
164165
binding.tvCodeInvalid.text = error
165166
val views = getAllViews()
166167
val tvs = views.drop(1).map { it as TextView }
167-
(tvs.indices).forEach {
168+
(0 until codeLength).forEach {
168169
tvs[it].background = getDrawable(codeStrokeErrorColor, true)
169170
tvs[it].setTextColor(codeErrorColor)
170171
}

otp_code_view/src/main/res/layout/verify_otp_code_view.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
android:layout_width="match_parent"
122122
android:layout_height="wrap_content"
123123
android:layout_marginHorizontal="20dp"
124-
android:layout_marginTop="20dp"
124+
android:layout_marginTop="10dp"
125125
android:gravity="center_vertical"
126126
android:padding="5dp"
127127
android:text="@string/invalid_code"

0 commit comments

Comments
 (0)