diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index d326780e..a95a3d92 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,8 +1,5 @@ - - diff --git a/libraries/phonenumber/build.gradle b/libraries/phonenumber/build.gradle index 1b19af81..d0af564a 100644 --- a/libraries/phonenumber/build.gradle +++ b/libraries/phonenumber/build.gradle @@ -31,6 +31,10 @@ android { dataBinding { enabled true } + + androidExtensions { + experimental = true + } } dependencies { diff --git a/libraries/phonenumber/src/main/java/com/trendyol/uicomponents/phonenumber/PhoneNumberTextInputEditText.kt b/libraries/phonenumber/src/main/java/com/trendyol/uicomponents/phonenumber/PhoneNumberTextInputEditText.kt index 09191628..07ab89c8 100644 --- a/libraries/phonenumber/src/main/java/com/trendyol/uicomponents/phonenumber/PhoneNumberTextInputEditText.kt +++ b/libraries/phonenumber/src/main/java/com/trendyol/uicomponents/phonenumber/PhoneNumberTextInputEditText.kt @@ -3,15 +3,24 @@ package com.trendyol.uicomponents.phonenumber import android.content.ClipData import android.content.ClipboardManager import android.content.Context +import android.os.Bundle +import android.os.Parcelable import android.telephony.PhoneNumberFormattingTextWatcher import android.text.Editable import android.text.InputType import android.text.TextWatcher import android.util.AttributeSet +import androidx.core.os.bundleOf import com.google.android.material.textfield.TextInputEditText +import kotlinx.android.parcel.Parcelize class PhoneNumberTextInputEditText : TextInputEditText { + var viewState: PhoneNumberTextInputEditTextViewState = PhoneNumberTextInputEditTextViewState( + maskable = false, + maskCharacter = '*' + ) + constructor(context: Context) : this(context, null) constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, R.attr.editTextStyle) @@ -29,11 +38,9 @@ class PhoneNumberTextInputEditText : TextInputEditText { maxLines = 1 setSingleLine() setOnFocusChangeListener { _, hasFocus -> - if (hasFocus) { - if (length() == 0) { - setText(FIRST_CHARACTER_ZERO) - setSelection(AFTER_ZERO_SELECTION_INDEX) - } + if (hasFocus && (text.isNullOrEmpty() || (isMaskable() && hasMaskCharacter()))) { + setText(FIRST_CHARACTER_ZERO) + setSelection(AFTER_ZERO_SELECTION_INDEX) } else { if (text == null || text.toString() == FIRST_CHARACTER_ZERO) { setText(EMPTY) @@ -92,6 +99,18 @@ class PhoneNumberTextInputEditText : TextInputEditText { }) } + private fun hasMaskCharacter() = text?.toString()?.contains(viewState.maskCharacter) == true + + fun setMaskCharacter(maskCharacter: Char) { + viewState = viewState.copy(maskCharacter = maskCharacter) + } + + fun isMaskable() = viewState.maskable + + fun setMaskable(maskable: Boolean) { + viewState = viewState.copy(maskable = maskable) + } + override fun onTextContextMenuItem(id: Int): Boolean { when (id) { android.R.id.paste -> { @@ -134,7 +153,8 @@ class PhoneNumberTextInputEditText : TextInputEditText { } } - private fun parsePhoneNumber(text: String) = text.replace("[^\\d]".toRegex(), "") + private fun parsePhoneNumber(text: String) = + text.replace("[^\\d${viewState.maskCharacter}]".toRegex(), "") fun getPhoneNumber(): String { return parsePhoneNumber(text?.toString() ?: "") @@ -151,7 +171,29 @@ class PhoneNumberTextInputEditText : TextInputEditText { super.removeTextChangedListener(watcher) } + override fun onSaveInstanceState(): Parcelable? { + return bundleOf( + SUPER_STATE_KEY to super.onSaveInstanceState(), + STATE_KEY to viewState + ) + } + + override fun onRestoreInstanceState(state: Parcelable?) { + if (state is Bundle) { + state + .getParcelable(STATE_KEY) + ?.let { this.viewState = it } + + super.onRestoreInstanceState(state.getParcelable(SUPER_STATE_KEY)) + } else { + super.onRestoreInstanceState(state) + } + } + companion object { + val SUPER_STATE_KEY = "SUPER_STATE_KEY" + val STATE_KEY = "SUPER_STATE_KEY" + private const val FIRST_CHARACTER_ZERO = "0" private const val CLEAR_SELECTION_INDEX = 0 private const val AFTER_ZERO_SELECTION_INDEX = 1 @@ -173,4 +215,10 @@ class PhoneNumberTextInputEditText : TextInputEditText { return regex.replace(this, EMPTY) } } -} \ No newline at end of file +} + +@Parcelize +data class PhoneNumberTextInputEditTextViewState( + val maskable: Boolean, + val maskCharacter: Char +) : Parcelable diff --git a/sample/src/main/java/com/trendyol/uicomponents/PhoneNumberActivity.kt b/sample/src/main/java/com/trendyol/uicomponents/PhoneNumberActivity.kt index c172fe7e..ed53e242 100644 --- a/sample/src/main/java/com/trendyol/uicomponents/PhoneNumberActivity.kt +++ b/sample/src/main/java/com/trendyol/uicomponents/PhoneNumberActivity.kt @@ -2,10 +2,22 @@ package com.trendyol.uicomponents import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.databinding.DataBindingUtil +import com.trendyol.uicomponents.databinding.ActivityPhoneNumberBinding +import com.trendyol.uicomponents.phonenumber.PhoneNumberTextInputEditTextViewState -class PhoneNumberActivity: AppCompatActivity() { +class PhoneNumberActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_phone_number) + DataBindingUtil.setContentView( + this, + R.layout.activity_phone_number + ).apply { + viewState = PhoneNumberTextInputEditTextViewState(maskable = true, maskCharacter = '*') + executePendingBindings() + editTextPhoneDot.setText("0555●●●●●55") + editTextPhoneStar.setText("0555*****55") + + } } } \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_phone_number.xml b/sample/src/main/res/layout/activity_phone_number.xml index 749ec1f1..8423ab42 100644 --- a/sample/src/main/res/layout/activity_phone_number.xml +++ b/sample/src/main/res/layout/activity_phone_number.xml @@ -1,21 +1,46 @@ - + + - + + + + android:layout_height="match_parent" + android:orientation="vertical" + android:padding="8dp"> + + + + + + - - + android:layout_height="wrap_content"> + + + - \ No newline at end of file + + \ No newline at end of file