Skip to content

Instantly share code, notes, and snippets.

@exallium
Last active February 14, 2018 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exallium/dd7a646be48b6058c272cb7f73a5bdcc to your computer and use it in GitHub Desktop.
Save exallium/dd7a646be48b6058c272cb7f73a5bdcc to your computer and use it in GitHub Desktop.
Attempt at expressing form input a little more concisely
import android.databinding.BindingAdapter
import android.support.design.widget.TextInputEditText
import android.support.design.widget.TextInputLayout
data class FormData<out D, R>(
val data: D,
val reason: R?,
private val _reasonToString: (R?) -> String = { "$it" }
) {
val isValid = reason != null
val reasonToString by lazy { _reasonToString(reason) }
}
@BindingAdapter("formData")
fun <D, R> bindTextInputLayoutFormData(textInputLayout: TextInputLayout, formData: FormData<D, R>) {
textInputLayout.isErrorEnabled = formData.isValid
textInputLayout.error = formData.reasonToString
}
@BindingAdapter("formData")
fun <R> bindTextInputEditTextFormData(textInputEditText: TextInputEditText, formData: FormData<String, R>) {
textInputEditText.setText(formData.data)
}
// BindingAdapter <insert view here> -- takes data and applies to whatever
fun Collection<FormData<Any, out Any>>.isFormValid() = all { it.isValid }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment