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