Skip to content

Commit

Permalink
FEAT: disabling form elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdeněk Balák committed Jun 10, 2019
1 parent 23b6401 commit ddb86fe
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ open class EditTextElement(
}
}

public override fun enable() {
super.disable()
editText?.isEnabled = true
}

public override fun disable() {
super.disable()
editText?.isEnabled = false
}
override fun getVal(): String? = editText?.text.toString()

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cz.qase.android.formbuilderlibrary.element

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.support.v4.widget.CompoundButtonCompat
import android.view.LayoutInflater
Expand All @@ -27,10 +26,10 @@ class LabelCheckboxElement(
private val formStyleBundle: FormStyleBundle? = null
) : FormElementValid<Boolean>() {

private lateinit var checkbox: CheckBox
private var checkbox: CheckBox? = null

override fun getVal(): Boolean? {
return checkbox.isChecked
return checkbox?.isChecked
}


Expand All @@ -53,7 +52,7 @@ class LabelCheckboxElement(
}

private fun prepareCheckbox(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): CheckBox {
checkbox = inflater.inflate(checkboxComponent, root, false) as CheckBox
val checkbox = inflater.inflate(checkboxComponent, root, false) as CheckBox


val color = ContextCompat.getColor(context,formStyleBundle.primaryBackgroundColor)
Expand All @@ -63,9 +62,17 @@ class LabelCheckboxElement(
checkbox.setOnClickListener {
checkboxCallback.callback(checkbox.isChecked)
}
this.checkbox = checkbox
return checkbox
}

public override fun enable() {
super.disable()
checkbox?.isEnabled = true
}


public override fun disable() {
super.disable()
checkbox?.isEnabled = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class LabelDateTimeElement(private val label: String,

private var textView: TextView? = null
private var labelView: TextView? = null
private var viewGroup: ViewGroup? = null
override fun createView(context: Context, formStyleBundle: FormStyleBundle): View {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(groupComponent, null) as ViewGroup
Expand All @@ -65,6 +66,7 @@ class LabelDateTimeElement(private val label: String,
}
true
}
viewGroup = view
return view
}

Expand Down Expand Up @@ -141,6 +143,18 @@ class LabelDateTimeElement(private val label: String,
}
}

public override fun enable() {
super.enable()
viewGroup?.isEnabled = true
textView?.isEnabled = true
}

public override fun disable() {
super.disable()
viewGroup?.isEnabled = false
textView?.isEnabled = false
}

private fun prepareLabel(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView {
val headerView = inflater.inflate(headerComponent, root, false) as TextView
headerView.setTextColorResourceId(context, formStyleBundle.primaryTextColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ class LabelInputElement(private val label: String,
}
}

public override fun enable() {
super.enable()
textInputLayout?.isEnabled = true
}

public override fun disable() {
super.disable()
textInputLayout?.isEnabled = false
}

private fun prepareLabel(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView {
val headerView = inflater.inflate(headerComponent, root, false) as TextView
headerView.setTextColorResourceId(context, formStyleBundle.primaryTextColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ class LabelSpinnerElement<T>(private val label: String,
}
}

public override fun enable() {
super.enable()
spinner?.isEnabled = true
spinner?.isClickable = true
spinner?.isFocusable = true
}

public override fun disable() {
super.disable()
spinner?.isEnabled = false
spinner?.isClickable = false
spinner?.isFocusable = false
}

override fun getVal(): T? {
return spinner?.selectedIndex?.let { availableValues.get(it) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cz.qase.android.formbuilderlibrary.element

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.support.v4.graphics.drawable.DrawableCompat
import android.support.v7.widget.SwitchCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -11,22 +16,17 @@ import cz.qase.android.formbuilderlibrary.common.setBackgroundColorResourceId
import cz.qase.android.formbuilderlibrary.common.setTextColorResourceId
import cz.qase.android.formbuilderlibrary.element.generic.CheckboxCallback
import cz.qase.android.formbuilderlibrary.element.generic.FormElementValid
import android.content.res.ColorStateList
import android.support.v4.graphics.drawable.DrawableCompat
import android.support.v7.widget.SwitchCompat
import android.graphics.Color
import android.support.v4.content.ContextCompat


class LabelSwitchElement(private val label: String,
private var checked: Boolean,
private var initialValue: Boolean,
private val checkboxCallback: CheckboxCallback,
private val groupComponent: Int = R.layout.form_group_item_inline,
private val headerComponent: Int = R.layout.form_inline_label,
private val switchComponent: Int = R.layout.form_inline_switch,
private val formStyleBundle: FormStyleBundle? = null) : FormElementValid<Boolean>() {
private var switchView: SwitchCompat? = null
override fun getVal(): Boolean {
return checked
return switchView?.isChecked == true
}

override fun createView(context: Context, formStyleBundle: FormStyleBundle): View {
Expand All @@ -42,17 +42,16 @@ class LabelSwitchElement(private val label: String,
return view
}


private fun prepareSwitch(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): SwitchCompat {
val switchView = inflater.inflate(switchComponent, root, false) as SwitchCompat
switchView.isChecked = checked
switchView.setColor(formStyleBundle.primaryTextColor, context)
switchView.setTextColorResourceId(context, formStyleBundle.secondaryTextColor)
switchView.setOnCheckedChangeListener { _, isChecked ->
checked = isChecked
checkboxCallback.callback(checked)
val tmpSwitchView = inflater.inflate(switchComponent, root, false) as SwitchCompat
tmpSwitchView.isChecked = initialValue
tmpSwitchView.setColor(formStyleBundle.primaryTextColor, context)
tmpSwitchView.setTextColorResourceId(context, formStyleBundle.secondaryTextColor)
tmpSwitchView.setOnCheckedChangeListener { _, isChecked ->
checkboxCallback.callback(isChecked)
}
return switchView
switchView = tmpSwitchView
return tmpSwitchView
}

private fun prepareHeader(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView {
Expand All @@ -63,7 +62,7 @@ class LabelSwitchElement(private val label: String,
}


private fun SwitchCompat.setColor(colorRes: Int, context: Context){
private fun SwitchCompat.setColor(colorRes: Int, context: Context) {
// trackColor is the thumbColor with 30% transparency (77)

val color = ContextCompat.getColor(context, colorRes)
Expand All @@ -81,4 +80,14 @@ class LabelSwitchElement(private val label: String,
intArrayOf(trackColor, Color.parseColor("#4D000000") // full black with 30% transparency (4D)
)))
}

public override fun enable() {
super.enable()
switchView?.isEnabled = true
}

public override fun disable() {
super.disable()
switchView?.isEnabled = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,14 @@ class TextAreaElement(
throw e
}
}

public override fun enable() {
super.disable()
textInputEditText?.isEnabled = true
}

public override fun disable() {
super.disable()
textInputEditText?.isEnabled = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import cz.qase.android.formbuilderlibrary.ValidationException

abstract class FormElement<T>() {

protected var enabled = true

protected open fun enable() {
enabled = true
}

protected open fun disable() {
enabled = false
}

abstract fun createView(context: Context, formStyleBundle: FormStyleBundle): View

@Throws(ValidationException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ abstract class FormElementValidatable<T>(val formValidators: MutableList<FormVal

@Throws(ValidationException::class)
override fun validate() {
for (formValidator in formValidators) {
formValidator.validate(getVal())
if (enabled) {
for (formValidator in formValidators) {
formValidator.validate(getVal())
}
}
}
}

0 comments on commit ddb86fe

Please sign in to comment.