Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.idea/codeStyles/Project.xml
  • Loading branch information
MertNYuksel committed Dec 19, 2019
2 parents 2ce6645 + 3b16c01 commit f4da3d7
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 25 deletions.
6 changes: 3 additions & 3 deletions libraries/dialogs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'kotlin-kapt'
apply plugin: 'com.github.dcendents.android-maven'

group="com.trendyol.ui-components"
version="1.0.3"
version="1.0.4"

android {
compileSdkVersion 29
Expand All @@ -14,8 +14,8 @@ android {
defaultConfig {
minSdkVersion 17
targetSdkVersion 29
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.0.4"
vectorDrawables.useSupportLibrary = true

consumerProguardFiles 'consumer-rules.pro'
Expand Down
4 changes: 4 additions & 0 deletions libraries/phonenumber/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ android {
dataBinding {
enabled true
}

androidExtensions {
experimental = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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() ?: "")
Expand All @@ -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<PhoneNumberTextInputEditTextViewState>(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
Expand All @@ -173,4 +215,10 @@ class PhoneNumberTextInputEditText : TextInputEditText {
return regex.replace(this, EMPTY)
}
}
}
}

@Parcelize
data class PhoneNumberTextInputEditTextViewState(
val maskable: Boolean,
val maskCharacter: Char
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActivityPhoneNumberBinding>(
this,
R.layout.activity_phone_number
).apply {
viewState = PhoneNumberTextInputEditTextViewState(maskable = true, maskCharacter = '*')
executePendingBindings()
editTextPhoneDot.setText("0555●●●●●55")
editTextPhoneStar.setText("0555*****55")

}
}
}
51 changes: 38 additions & 13 deletions sample/src/main/res/layout/activity_phone_number.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<com.google.android.material.textfield.TextInputLayout
<variable
name="viewState"
type="com.trendyol.uicomponents.phonenumber.PhoneNumberTextInputEditTextViewState" />
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">


<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.trendyol.uicomponents.phonenumber.PhoneNumberTextInputEditText
android:id="@+id/editTextPhoneDot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="17"
app:maskCharacter="@{'●'}"
app:maskable="@{true}" />
</com.google.android.material.textfield.TextInputLayout>

<com.trendyol.uicomponents.phonenumbertextinputedittext.PhoneNumberTextInputEditText
android:id="@+id/editTextPhone"
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="17"/>
</com.google.android.material.textfield.TextInputLayout>
android:layout_height="wrap_content">

<com.trendyol.uicomponents.phonenumber.PhoneNumberTextInputEditText
android:id="@+id/editTextPhoneStar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="17"
app:viewState="@{viewState}" />
</com.google.android.material.textfield.TextInputLayout>


</LinearLayout>
</LinearLayout>
</layout>

0 comments on commit f4da3d7

Please sign in to comment.