From 5fe41d90a06120b4aa6ec18dcdf28d69c4975408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D1=88=D0=BA=D0=BE=D0=B2=20=D0=94=D0=BC?= =?UTF-8?q?=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= <100100101104@mail.ru> Date: Sat, 15 Jan 2022 03:12:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=83=D0=B7=D0=BD=D0=B0=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82=D0=B2=D0=B0,=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D0=B0=D1=82=D1=82=D1=80=D0=B8=D0=B1=D1=83=D1=82?= =?UTF-8?q?=D1=83=20=D0=B2=20xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VerificationCodeEditText.kt | 38 ++++++--- .../layout/fragment_email_confirmation.xml | 1 + .../view_verification_code_edit_text.xml | 79 +++---------------- .../layout/view_verification_code_layout.xml | 12 +++ app/src/main/res/values/attrs.xml | 6 ++ 5 files changed, 56 insertions(+), 80 deletions(-) create mode 100644 app/src/main/res/layout/view_verification_code_layout.xml create mode 100644 app/src/main/res/values/attrs.xml diff --git a/app/src/main/java/com/example/android_curse/ui/emailconfirmation/VerificationCodeEditText.kt b/app/src/main/java/com/example/android_curse/ui/emailconfirmation/VerificationCodeEditText.kt index 75d33b7..deb6072 100644 --- a/app/src/main/java/com/example/android_curse/ui/emailconfirmation/VerificationCodeEditText.kt +++ b/app/src/main/java/com/example/android_curse/ui/emailconfirmation/VerificationCodeEditText.kt @@ -9,8 +9,10 @@ import android.view.LayoutInflater //import androidx.constraintlayout.solver.state.State import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.view.isVisible +import com.example.android_curse.R import com.example.android_curse.databinding.ViewVerificationCodeEditTextBinding import java.lang.Math.min +import kotlin.properties.Delegates class VerificationCodeEditText @JvmOverloads constructor( context: Context, @@ -23,17 +25,21 @@ class VerificationCodeEditText @JvmOverloads constructor( private val viewBinding = ViewVerificationCodeEditTextBinding.inflate(LayoutInflater.from(context), this) - private val slotViews: List = - listOf( - viewBinding.slot1, - viewBinding.slot2, - viewBinding.slot3, - viewBinding.slot4, - viewBinding.slot5, - viewBinding.slot6 - ) + private var numberOfSlots: Int by Delegates.observable(5) { _, _, newValue -> + for (i in 0 until newValue) { + LayoutInflater.from(context).inflate( + R.layout.view_verification_code_layout, viewBinding.slotLinearLayout + ) + } + slotViews = (0 until newValue).toMutableList().map { + viewBinding.slotLinearLayout.getChildAt(it) as VerificationCodeSlotView + } + slotValues = Array(newValue) { null } + } - private val slotValues: Array = Array(6) { null } + private var slotViews: List = emptyList() + + private var slotValues: Array = emptyArray() var onVerificationCodeFilledListener: (String) -> Unit = {} @@ -74,6 +80,18 @@ private val viewBinding = } slotValues.fillWith(viewBinding.realVerificationCodeEditText.text) slotViews.render(slotValues) + + context + .theme + .obtainStyledAttributes( + attrs, + R.styleable.VerificationCodeEditText, + defStyleAttr, + defStyleRes + ) + .apply { + numberOfSlots = getInt(R.styleable.VerificationCodeEditText_numberOfSlots, 6) + } } fun getCode(): String { diff --git a/app/src/main/res/layout/fragment_email_confirmation.xml b/app/src/main/res/layout/fragment_email_confirmation.xml index 180ba35..7c3b24e 100644 --- a/app/src/main/res/layout/fragment_email_confirmation.xml +++ b/app/src/main/res/layout/fragment_email_confirmation.xml @@ -21,6 +21,7 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/backButton" + app:numberOfSlots="5" android:layout_width="0dp" android:layout_height="wrap_content"/> diff --git a/app/src/main/res/layout/view_verification_code_edit_text.xml b/app/src/main/res/layout/view_verification_code_edit_text.xml index 66c4030..72c63e0 100644 --- a/app/src/main/res/layout/view_verification_code_edit_text.xml +++ b/app/src/main/res/layout/view_verification_code_edit_text.xml @@ -6,77 +6,16 @@ android:layout_height="wrap_content" tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"> - - - - - - - - - - - + app:layout_constraintEnd_toEndOf="@+id/realVerificationCodeEditText" + app:layout_constraintStart_toStartOf="@+id/realVerificationCodeEditText" + app:layout_constraintTop_toTopOf="@+id/realVerificationCodeEditText" /> + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml new file mode 100644 index 0000000..6690dee --- /dev/null +++ b/app/src/main/res/values/attrs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file