Skip to content

Commit

Permalink
Merge pull request #12 from GO-GO-NEVER-END/feature/common/royal_flus…
Browse files Browse the repository at this point in the history
…h_edit_text

양 끝에 아이콘을 추가할 수 있는 Edit Text & 검증 텍스트와 텍스트 제한 기능이 있는 Edit Text 구현
  • Loading branch information
ldh019 authored Mar 26, 2024
2 parents f4e1e30 + cd5b180 commit 10b4546
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 0 deletions.
5 changes: 5 additions & 0 deletions feature/feature-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("UnstableApiUsage")

@Suppress("DSL_SCOPE_VIOLATION")

plugins {
Expand All @@ -6,6 +8,9 @@ plugins {

android {
namespace = "com.ggne.feature_common"
buildFeatures {
viewBinding = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.ggne.feature_common.custom

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.ggne.feature_common.R
import com.ggne.feature_common.databinding.RoyalFlushIconEditTextBinding

class RoyalFlushIconEditText(context: Context, attrs: AttributeSet) :
ConstraintLayout(context, attrs) {

private val binding =
RoyalFlushIconEditTextBinding.inflate(LayoutInflater.from(context), this, true)

init {

context.theme.obtainStyledAttributes(
attrs,
R.styleable.RoyalFlushIconEditText,
0,
0,
).apply {
try {

getResourceId(
R.styleable.RoyalFlushIconEditText_startIcon,
EMPTY_RESOURCE,
).let { resource ->
if (resource == EMPTY_RESOURCE) {
binding.ivStartIcon.visibility = GONE
} else {
binding.ivStartIcon.setImageResource(resource)
}
}

getResourceId(
R.styleable.RoyalFlushIconEditText_endIcon,
EMPTY_RESOURCE,
).let { resource ->
if (resource == EMPTY_RESOURCE) {
binding.ivEndIcon.visibility = GONE
} else {
binding.ivEndIcon.setImageResource(resource)
}
}

getString(R.styleable.RoyalFlushIconEditText_royalFlushIconEditTextHint).let { hint ->
binding.et.hint = hint
}
} finally {
recycle()
}
}

removeAllViews()
addView(binding.root)
}

companion object {
private const val EMPTY_RESOURCE = -1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.ggne.feature_common.custom

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.ggne.feature_common.extensions.EMPTY
import com.ggne.feature_common.R
import com.ggne.feature_common.extensions.addAfterTextChangedListener
import com.ggne.feature_common.databinding.RoyalFlushVerificationEditTextBinding

class RoyalFlushVerificationEditText(context: Context, attrs: AttributeSet) :
ConstraintLayout(context, attrs) {

private var maxLength: Int
private var verificationText: String = String.EMPTY

val text
get() = binding.et.text.toString()

val editText
get() = binding.et

private val binding =
RoyalFlushVerificationEditTextBinding.inflate(LayoutInflater.from(context), this, true)

init {

context.theme.obtainStyledAttributes(
attrs,
R.styleable.RoyalFlushVerificationEditText,
0,
0,
).apply {
try {

getString(R.styleable.RoyalFlushVerificationEditText_royalFlushVerificationEditTextHint).let { hint ->
binding.et.hint = hint
}

getInt(R.styleable.RoyalFlushVerificationEditText_maxLength, Int.MAX_VALUE).let { inputtedMaxLength ->
maxLength = inputtedMaxLength
}

getBoolean(R.styleable.RoyalFlushVerificationEditText_isCounterVisible, true).let { isVisible ->
if (isVisible) {

with(binding.counterTv) {
visibility = VISIBLE
text = context.getString(R.string.text_counter_format, 0, maxLength)
}

binding.et.addAfterTextChangedListener { text ->
if (text.length <= maxLength) {
binding.counterTv.text = context.getString(R.string.text_counter_format, text.length, maxLength)
} else {
with(binding.et) {
setText(text.substring(0, maxLength))
setSelection(maxLength)
}
}
}

} else {
binding.counterTv.visibility = GONE
maxLength = Int.MAX_VALUE
}

}

getBoolean(R.styleable.RoyalFlushVerificationEditText_isVerificationVisible, false).let { isVisible ->
if (isVisible) {
getString(R.styleable.RoyalFlushVerificationEditText_verificationText).let { inputtedVerificationText ->
verificationText = inputtedVerificationText ?: String.EMPTY
}
setVerification(true)
} else {
setVerification(false)
}
}


} finally {
recycle()
}
}

removeAllViews()
addView(binding.root)

}

fun setVerificationText(text: String) {
verificationText = text
}

fun setVerification(isVerified: Boolean) {
if (isVerified) {
binding.verificationTv.visibility = VISIBLE
binding.verificationTv.text = verificationText
} else {
binding.verificationTv.visibility = GONE
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ggne.feature_common.extensions

val String.Companion.EMPTY: String
get() = ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ggne.feature_common.extensions

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText

fun EditText.addAfterTextChangedListener(block: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
block(s.toString())
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<selector >
<item xmlns:android="http://schemas.android.com/apk/res/android">
<shape android:shape="rectangle">
<solid android:color="#DFDBCC" />
<corners android:radius="8dp" />
</shape>
</item>
</selector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/royal_flush_edit_text_background"
android:paddingHorizontal="18dp"
android:paddingVertical="14dp">

<ImageView
android:id="@+id/iv_start_icon"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/start_ic"
android:paddingEnd="4dp"
app:layout_constraintBottom_toBottomOf="@id/et"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/et" />

<EditText
android:id="@+id/et"
android:layout_width="0dp"
android:layout_height="0dp"
android:autofillHints="yes"
android:background="@null"
android:gravity="center_vertical"
android:inputType="text"
android:maxLines="1"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_end_icon"
app:layout_constraintStart_toEndOf="@id/iv_start_icon"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="LabelFor" />

<ImageView
android:id="@+id/iv_end_icon"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/end_ic"
android:paddingStart="4dp"
app:layout_constraintBottom_toBottomOf="@id/et"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/et" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/et_container"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/royal_flush_edit_text_background"
android:paddingHorizontal="18dp"
android:paddingVertical="14dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<EditText
android:id="@+id/et"
android:layout_width="0dp"
android:layout_height="0dp"
android:autofillHints="yes"
android:background="@null"
android:gravity="center_vertical"
android:inputType="text"
android:maxLines="1"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/tv_counter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="LabelFor" />

<TextView
android:id="@+id/tv_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

<TextView
android:id="@+id/tv_verification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginTop="6dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/et_container" />

</androidx.constraintlayout.widget.ConstraintLayout>
16 changes: 16 additions & 0 deletions feature/feature-common/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoyalFlushIconEditText">
<attr name="startIcon" format="reference" />
<attr name="endIcon" format="reference" />
<attr name="royalFlushIconEditTextHint" format="string" />
</declare-styleable>

<declare-styleable name="RoyalFlushVerificationEditText">
<attr name="isCounterVisible" format="boolean" />
<attr name="isVerificationVisible" format="boolean" />
<attr name="verificationText" format="string" />
<attr name="maxLength" format="integer" />
<attr name="royalFlushVerificationEditTextHint" format="string" />
</declare-styleable>
</resources>
7 changes: 7 additions & 0 deletions feature/feature-common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="start_ic">시작 아이콘</string>
<string name="end_ic">끝 아이콘</string>
<string name="text_counter_format">%d/%d</string>
<string name="can_use_nickname">사용 가능한 닉네임입니다.</string>
</resources>

0 comments on commit 10b4546

Please sign in to comment.