Skip to content

Commit

Permalink
Merge pull request #10 from edwnmrtnz/restore
Browse files Browse the repository at this point in the history
Restore
  • Loading branch information
edwnmrtnz authored May 13, 2022
2 parents 5a306bf + 523e4df commit 52143b9
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 127 deletions.
24 changes: 13 additions & 11 deletions app/src/main/java/com/github/edwnmrtnz/awesomeforms/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.edwnmrtnz.awesomeforms

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import android.widget.ArrayAdapter
import com.github.edwnmrtnz.awesomeforms.databinding.ActivityMainBinding

Expand All @@ -12,19 +13,20 @@ class MainActivity : AppCompatActivity() {

val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.etFirstName.setError("first name is required")
binding.etLastName.setError("last name is required")
binding.etGender.setError("this field is required")
val adapter = ArrayAdapter(
this,
R.layout.awesomeform_dropdown_popup_item,
resources.getStringArray(R.array.exposed_dropdown_content)
)

binding.etGender.setAdapter(adapter)

window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)

binding.etMobileNumber.removeError()
if(savedInstanceState == null) {
binding.etFirstName.setError("First name is required")
binding.etLastName.setError("Last name is required")
binding.etGender.setError("Tequired")
}

val adapter = ArrayAdapter(
this,
R.layout.awesomeform_dropdown_popup_item,
resources.getStringArray(R.array.exposed_dropdown_content)
)
binding.etGender.setAdapter(adapter)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.util.AttributeSet
import android.util.Log
import android.util.SparseArray
import android.view.View
import android.view.View.OnFocusChangeListener
Expand Down Expand Up @@ -50,65 +51,49 @@ class AwesomeFormNormalEditText(context: Context, attrs: AttributeSet) :
textChangeListener()
etField.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
if (isErrorEnabled) {
tvFieldLabel.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_color_error
)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_color_error
)
)
} else {
tvFieldLabel.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_focused_color
)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_focused_color
)
)
}
handleChangeWhenInFocus(context)
} else {
if (isErrorEnabled) {
tvFieldLabel.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_color_error
)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_color_error
)
)
} else {
tvFieldLabel.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_hintColor
)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_hintColor
)
)
}
handleChangeWhenInNotFocus(context)
}
}
}

private fun handleChangeWhenInFocus(context: Context) {
if (isErrorEnabled) {
tvFieldLabel.setTextColor(
ContextCompat.getColor(context, R.color.AwesomeForm_color_error)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(context, R.color.AwesomeForm_color_error)
)
} else {
tvFieldLabel.setTextColor(
ContextCompat.getColor(context, R.color.AwesomeForm_focused_color)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(context, R.color.AwesomeForm_focused_color)
)
}
}

private fun handleChangeWhenInNotFocus(context: Context) {
if (isErrorEnabled) {
tvFieldLabel.setTextColor(
ContextCompat.getColor(context, R.color.AwesomeForm_color_error)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(context, R.color.AwesomeForm_color_error)
)
} else {
tvFieldLabel.setTextColor(
ContextCompat.getColor(context, R.color.AwesomeForm_hintColor)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(context, R.color.AwesomeForm_hintColor)
)
}
}

private fun textChangeListener() {
etField.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
Expand Down Expand Up @@ -224,38 +209,34 @@ class AwesomeFormNormalEditText(context: Context, attrs: AttributeSet) :
isErrorEnabled = false

if(etField.hasFocus()) {
tvFieldLabel.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_focused_color
)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_focused_color
)
)
tvFieldLabel.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_focused_color))
tvAssistiveText.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_focused_color))
} else {
tvFieldLabel.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_hintColor))
tvAssistiveText.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_hintColor))
}

setAssistiveTextBasedOnCurrentState()

tlField.error = null

}

private fun setAssistiveTextBasedOnCurrentState() {
if (assistiveText != null) {
tvAssistiveText.visibility = View.VISIBLE
tvAssistiveText.text = assistiveText
} else {
tvAssistiveText.visibility = View.GONE
}
tlField.error = null
}

fun setError(errorMessage: String) {
isErrorEnabled = true

assistiveText = errorMessage
tvFieldLabel.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_color_error))
tvAssistiveText.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_color_error
)
)
tvAssistiveText.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_color_error))

tvAssistiveText.visibility = View.VISIBLE
tvAssistiveText.text = errorMessage
Expand All @@ -273,9 +254,11 @@ class AwesomeFormNormalEditText(context: Context, attrs: AttributeSet) :

fun getText() = etField.text.toString()

override fun onSaveInstanceState(): Parcelable? {
override fun onSaveInstanceState(): Parcelable {
return SavedState(super.onSaveInstanceState()).apply {
childrenStates = saveChildViewStates()
isErrorEnabled = if(this@AwesomeFormNormalEditText.isErrorEnabled) 1 else 0
assistiveText = this@AwesomeFormNormalEditText.assistiveText
}
}

Expand All @@ -284,9 +267,21 @@ class AwesomeFormNormalEditText(context: Context, attrs: AttributeSet) :
is SavedState -> {
super.onRestoreInstanceState(state.superState)
state.childrenStates?.let { restoreChildViewStates(it) }
this.isErrorEnabled = true.takeIf { state.isErrorEnabled == 1 } ?: false
this.assistiveText = state.assistiveText
}
else -> super.onRestoreInstanceState(state)
}
restore()
}

private fun restore() {
setAssistiveTextBasedOnCurrentState()
if(isErrorEnabled) {
setError(assistiveText ?: "")
} else {
removeError()
}
}

override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>) {
Expand All @@ -301,15 +296,22 @@ class AwesomeFormNormalEditText(context: Context, attrs: AttributeSet) :

internal var childrenStates: SparseArray<Parcelable>? = null

internal var isErrorEnabled = 0 // 0 false, 1 true
internal var assistiveText : String? = null

constructor(superState: Parcelable?) : super(superState)

constructor(source: Parcel) : super(source) {
childrenStates = source.readSparseArray(javaClass.classLoader)
isErrorEnabled = source.readInt()
assistiveText = source.readString()
}

override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeSparseArray(childrenStates)
out.writeInt(isErrorEnabled)
out.writeString(assistiveText)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,50 +234,48 @@ class AwesomeFormPasswordEditText(context: Context, attrs: AttributeSet) : Const
fun setIsClickable(isClickable: Boolean) {
this.etField.isClickable = isClickable
}

fun removeError() {
isErrorEnabled = false

if(etField.hasFocus()) {
tvFieldLabel.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_focused_color
)
)
tvAssistiveText.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_focused_color
)
)
tvFieldLabel.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_focused_color))
tvAssistiveText.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_focused_color))
} else {
tvFieldLabel.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_hintColor))
tvAssistiveText.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_hintColor))
}

setAssistiveTextBasedOnCurrentState()

tlField.error = null

}

private fun setAssistiveTextBasedOnCurrentState() {
if (assistiveText != null) {
tvAssistiveText.visibility = View.VISIBLE
tvAssistiveText.text = assistiveText
} else {
tvAssistiveText.visibility = View.GONE
}
tlField.error = null

}

fun setError(errorMessage: String) {
isErrorEnabled = true

assistiveText = errorMessage
tvFieldLabel.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_color_error))
tvAssistiveText.setTextColor(ContextCompat.getColor(context, R.color.AwesomeForm_color_error))

tvAssistiveText.visibility = View.VISIBLE
tvAssistiveText.setTextColor(
ContextCompat.getColor(
context,
R.color.AwesomeForm_color_error
)
)
tvAssistiveText.text = errorMessage

tlField.error = " "
tlField.getChildAt(1).visibility = View.GONE
tlField.errorIconDrawable = null
}


fun getTextInputLayout() = tlField

fun getEditText() = etField
Expand All @@ -287,9 +285,11 @@ class AwesomeFormPasswordEditText(context: Context, attrs: AttributeSet) : Const
fun getText() = etField.text.toString()


override fun onSaveInstanceState(): Parcelable? {
override fun onSaveInstanceState(): Parcelable {
return SavedState(super.onSaveInstanceState()).apply {
childrenStates = saveChildViewStates()
isErrorEnabled = if(this@AwesomeFormPasswordEditText.isErrorEnabled) 1 else 0
assistiveText = this@AwesomeFormPasswordEditText.assistiveText
}
}

Expand All @@ -298,11 +298,22 @@ class AwesomeFormPasswordEditText(context: Context, attrs: AttributeSet) : Const
is SavedState -> {
super.onRestoreInstanceState(state.superState)
state.childrenStates?.let { restoreChildViewStates(it) }
this.isErrorEnabled = true.takeIf { state.isErrorEnabled == 1 } ?: false
this.assistiveText = state.assistiveText
}
else -> super.onRestoreInstanceState(state)
}
restore()
}

private fun restore() {
setAssistiveTextBasedOnCurrentState()
if(isErrorEnabled) {
setError(assistiveText ?: "")
} else {
removeError()
}
}
override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>) {
super.dispatchFreezeSelfOnly(container)
}
Expand All @@ -314,16 +325,22 @@ class AwesomeFormPasswordEditText(context: Context, attrs: AttributeSet) : Const
internal class SavedState : BaseSavedState {

internal var childrenStates: SparseArray<Parcelable>? = null
internal var isErrorEnabled = 0 // 0 false, 1 true
internal var assistiveText : String? = null

constructor(superState: Parcelable?) : super(superState)

constructor(source: Parcel) : super(source) {
childrenStates = source.readSparseArray(javaClass.classLoader)
isErrorEnabled = source.readInt()
assistiveText = source.readString()
}

override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeSparseArray(childrenStates)
out.writeInt(isErrorEnabled)
out.writeString(assistiveText)
}

companion object {
Expand Down
Loading

0 comments on commit 52143b9

Please sign in to comment.