Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

добавил узнавание количества, по аттрибуту в xml #12

Open
wants to merge 1 commit into
base: #7_user_list+
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,17 +25,21 @@ class VerificationCodeEditText @JvmOverloads constructor(
private val viewBinding =
ViewVerificationCodeEditTextBinding.inflate(LayoutInflater.from(context), this)

private val slotViews: List<VerificationCodeSlotView> =
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<CharSequence?> = Array(6) { null }
private var slotViews: List<VerificationCodeSlotView> = emptyList<VerificationCodeSlotView>()

private var slotValues: Array<CharSequence?> = emptyArray<CharSequence?>()

var onVerificationCodeFilledListener: (String) -> Unit = {}

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_email_confirmation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

Expand Down
79 changes: 9 additions & 70 deletions app/src/main/res/layout/view_verification_code_edit_text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,16 @@
android:layout_height="wrap_content"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

<com.example.android_curse.ui.emailconfirmation.VerificationCodeSlotView
android:id="@+id/slot1"
android:layout_width="@dimen/verification_code_slot_width"
android:layout_height="@dimen/verification_code_slot_height"
android:background="@drawable/selector_bg_verification_code_slot"
app:layout_constraintBottom_toBottomOf="@+id/realVerificationCodeEditText"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="@+id/realVerificationCodeEditText"
app:layout_constraintRight_toLeftOf="@+id/slot2"
app:layout_constraintTop_toTopOf="@+id/realVerificationCodeEditText"
tools:ignore="RtlHardcoded" />

<com.example.android_curse.ui.emailconfirmation.VerificationCodeSlotView
android:id="@+id/slot2"
android:layout_width="@dimen/verification_code_slot_width"
android:layout_height="@dimen/verification_code_slot_height"
android:layout_marginLeft="8dp"
android:background="@drawable/selector_bg_verification_code_slot"
app:layout_constraintBottom_toBottomOf="@+id/realVerificationCodeEditText"
app:layout_constraintLeft_toRightOf="@+id/slot1"
app:layout_constraintRight_toLeftOf="@+id/slot3"
app:layout_constraintTop_toTopOf="@+id/realVerificationCodeEditText"
tools:ignore="RtlHardcoded" />

<com.example.android_curse.ui.emailconfirmation.VerificationCodeSlotView
android:id="@+id/slot3"
android:layout_width="@dimen/verification_code_slot_width"
android:layout_height="@dimen/verification_code_slot_height"
android:layout_marginLeft="8dp"
android:background="@drawable/selector_bg_verification_code_slot"
app:layout_constraintBottom_toBottomOf="@+id/realVerificationCodeEditText"
app:layout_constraintLeft_toRightOf="@+id/slot2"
app:layout_constraintRight_toLeftOf="@+id/slot4"
app:layout_constraintTop_toTopOf="@+id/realVerificationCodeEditText"
tools:ignore="RtlHardcoded" />

<com.example.android_curse.ui.emailconfirmation.VerificationCodeSlotView
android:id="@+id/slot4"
android:layout_width="@dimen/verification_code_slot_width"
android:layout_height="@dimen/verification_code_slot_height"
android:layout_marginLeft="8dp"
android:background="@drawable/selector_bg_verification_code_slot"
app:layout_constraintBottom_toBottomOf="@+id/realVerificationCodeEditText"
app:layout_constraintLeft_toRightOf="@+id/slot3"
app:layout_constraintRight_toLeftOf="@+id/slot5"
app:layout_constraintTop_toTopOf="@+id/realVerificationCodeEditText"
tools:ignore="RtlHardcoded" />

<com.example.android_curse.ui.emailconfirmation.VerificationCodeSlotView
android:id="@+id/slot5"
android:layout_width="@dimen/verification_code_slot_width"
android:layout_height="@dimen/verification_code_slot_height"
android:layout_marginLeft="8dp"
android:background="@drawable/selector_bg_verification_code_slot"
app:layout_constraintBottom_toBottomOf="@+id/realVerificationCodeEditText"
app:layout_constraintLeft_toRightOf="@+id/slot4"
app:layout_constraintRight_toLeftOf="@+id/slot6"
app:layout_constraintTop_toTopOf="@+id/realVerificationCodeEditText"
tools:ignore="RtlHardcoded" />

<com.example.android_curse.ui.emailconfirmation.VerificationCodeSlotView
android:id="@+id/slot6"
android:layout_width="@dimen/verification_code_slot_width"
android:layout_height="@dimen/verification_code_slot_height"
android:layout_marginLeft="8dp"
android:background="@drawable/selector_bg_verification_code_slot"
<LinearLayout
android:id="@+id/slotLinearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/realVerificationCodeEditText"
app:layout_constraintLeft_toRightOf="@+id/slot5"
app:layout_constraintRight_toRightOf="@+id/realVerificationCodeEditText"
app:layout_constraintTop_toTopOf="@+id/realVerificationCodeEditText"
tools:ignore="RtlHardcoded" />
app:layout_constraintEnd_toEndOf="@+id/realVerificationCodeEditText"
app:layout_constraintStart_toStartOf="@+id/realVerificationCodeEditText"
app:layout_constraintTop_toTopOf="@+id/realVerificationCodeEditText" />

<EditText
android:id="@+id/realVerificationCodeEditText"
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/view_verification_code_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:parentTag="androidx.appcompat.widget.LinearLayoutCompat">

<com.example.android_curse.ui.emailconfirmation.VerificationCodeSlotView
android:layout_width="@dimen/verification_code_slot_width"
android:layout_height="@dimen/verification_code_slot_height"
android:layout_marginStart="8dp"
android:background="@drawable/selector_bg_verification_code_slot" />

</merge>
6 changes: 6 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="VerificationCodeEditText">
<attr name="numberOfSlots" format="integer" />
</declare-styleable>
</resources>