Last active
February 14, 2018 16:41
-
-
Save exallium/dd7a646be48b6058c272cb7f73a5bdcc to your computer and use it in GitHub Desktop.
Attempt at expressing form input a little more concisely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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