From 3a44b002c48a4afae615d0f29e38fa092b033c84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Bal=C3=A1k?= Date: Fri, 3 May 2019 11:43:30 +0200 Subject: [PATCH] FEAT: Text area element --- .../formbuilderproject/MainActivity.kt | 23 ++- .../element/EditTextElement.kt | 1 - .../element/TextAreaElement.kt | 146 ++++++++++++++++++ .../res/layout/form_footer_text_right.xml | 9 ++ .../main/res/layout/form_textarea_item.xml | 5 + 5 files changed, 177 insertions(+), 7 deletions(-) create mode 100644 formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/TextAreaElement.kt create mode 100644 formbuilderlibrary/src/main/res/layout/form_footer_text_right.xml create mode 100644 formbuilderlibrary/src/main/res/layout/form_textarea_item.xml diff --git a/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt b/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt index 42290b6..b425e40 100644 --- a/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt +++ b/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt @@ -33,11 +33,11 @@ class MainActivity : AppCompatActivity() { private val validateActionCallback = object : ActionCallback, Form.Callback { override fun successCallback() { - Toast.makeText(this@MainActivity, "Validate OK", Toast.LENGTH_LONG).show() + Toast.makeText(this@MainActivity, "Validate OK", Toast.LENGTH_SHORT).show() } override fun errorCallback(message: String?) { - Toast.makeText(this@MainActivity, "Validate not ok $message", Toast.LENGTH_LONG).show() + Toast.makeText(this@MainActivity, "Validate not ok $message", Toast.LENGTH_SHORT).show() } override fun callback() { @@ -47,24 +47,33 @@ class MainActivity : AppCompatActivity() { private val showToastActionCallback = object : ActionCallback { override fun callback() { - Toast.makeText(this@MainActivity, "Action performed", Toast.LENGTH_LONG).show() + Toast.makeText(this@MainActivity, "Action performed", Toast.LENGTH_SHORT).show() } } private val showToastCheckboxCallback = object : CheckboxCallback { + private var toast: Toast? = null override fun callback(checked: Boolean) { - Toast.makeText(this@MainActivity, "Selected: $checked", Toast.LENGTH_LONG).show() + toast?.cancel() + toast = Toast.makeText(this@MainActivity, "Selected: $checked", Toast.LENGTH_SHORT) + toast?.show() } } private val showToastStringValueCallback = object : ValueCallback { + private var toast: Toast? = null override fun callback(value: String) { - Toast.makeText(this@MainActivity, value, Toast.LENGTH_LONG).show() + toast?.cancel() + toast = Toast.makeText(this@MainActivity, value, Toast.LENGTH_SHORT) + toast?.show() } } private val showToastDateTimeValueCallback = object : ValueCallback { + private var toast: Toast? = null override fun callback(value: DateTime) { - Toast.makeText(this@MainActivity, SimpleDateFormat("dd.MM.yyyy - HH:mm", Locale.getDefault()).format(value.toDate()), Toast.LENGTH_LONG).show() + toast?.cancel() + toast = Toast.makeText(this@MainActivity, SimpleDateFormat("dd.MM.yyyy - HH:mm", Locale.getDefault()).format(value.toDate()), Toast.LENGTH_SHORT) + toast?.show() } } @@ -99,6 +108,8 @@ class MainActivity : AppCompatActivity() { addElement(LabelCheckboxElement("LabelCheckboxElement label", true, showToastCheckboxCallback), true) addElement(LabelDateTimeElement("Date picker", "Vyberte datum...", supportFragmentManager, showToastDateTimeValueCallback, formValidators = arrayListOf(NotInPastValidator("Date cannot be in past")))) addSpace() + addElement(TextAreaElement("TextArea", null, null, showToastStringValueCallback, maxLength = 320)) + addSpace() //input validable elements diff --git a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/EditTextElement.kt b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/EditTextElement.kt index f8ba613..13ba436 100644 --- a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/EditTextElement.kt +++ b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/EditTextElement.kt @@ -40,7 +40,6 @@ open class EditTextElement( textInputLayout?.isErrorEnabled = true textInputLayout?.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor) textInputLayout?.setPadding(10, 0, 10, 0) - editText = TextInputEditText(context) editText?.setTextColorResourceId(context, formStyleBundle.secondaryTextColor) textInputLayout?.addView(editText) diff --git a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/TextAreaElement.kt b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/TextAreaElement.kt new file mode 100644 index 0000000..c259248 --- /dev/null +++ b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/TextAreaElement.kt @@ -0,0 +1,146 @@ +package cz.qase.android.formbuilderlibrary.element + +import android.content.Context +import android.support.design.widget.TextInputEditText +import android.support.design.widget.TextInputLayout +import android.text.Editable +import android.text.TextWatcher +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import cz.qase.android.formbuilderlibrary.FormStyleBundle +import cz.qase.android.formbuilderlibrary.R +import cz.qase.android.formbuilderlibrary.ValidationException +import cz.qase.android.formbuilderlibrary.common.setBackgroundColorResourceId +import cz.qase.android.formbuilderlibrary.common.setTextColorResourceId +import cz.qase.android.formbuilderlibrary.element.generic.FormElementValidatable +import cz.qase.android.formbuilderlibrary.element.generic.ValueCallback +import cz.qase.android.formbuilderlibrary.validator.FormValidator +import android.text.InputFilter + +class TextAreaElement( + private val title: String, + private val hint: String?, + private var value: String? = null, + private val valueChangeListener: ValueCallback?, + private val numberOfLines: Int = 3, + private val maxLength: Int? = null, + formValidators: MutableList> = ArrayList(), + private val groupComponent: Int = R.layout.form_group_item, + private val headerComponent: Int = R.layout.form_header_item, + private val textAreaComponent: Int = R.layout.form_textarea_item, + private val footerComponent: Int = R.layout.form_footer_text_right, + private var formStyleBundle: FormStyleBundle? = null) : FormElementValidatable(formValidators) { + + override fun getVal(): String? { + return textInputEditText?.text.toString() + } + + private var headerView: TextView? = null + private var footerView: TextView? = null + private var textInputEditText: TextInputEditText? = null + private var textInputLayout: TextInputLayout? = 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 + val headerView = prepareHeader(inflater, context, this.formStyleBundle + ?: formStyleBundle, view) + val inputView = prepareText(inflater, context, this.formStyleBundle + ?: formStyleBundle, view) + val footerView = prepareFooter(inflater, context, this.formStyleBundle + ?: formStyleBundle, view) + view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor) + view.addView(headerView) + view.addView(inputView) + view.addView(footerView) + return view + } + + private fun prepareText(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextInputLayout { + val textInputLayout = inflater.inflate(textAreaComponent, root, false) as TextInputLayout + textInputLayout.hint = hint + textInputEditText = TextInputEditText(context) + textInputLayout.addView(textInputEditText) + textInputEditText?.minLines = numberOfLines + + if (maxLength != null) { + textInputEditText?.filters = arrayOf(InputFilter.LengthFilter(maxLength)) + } + + textInputEditText?.setTextColorResourceId(context, formStyleBundle.secondaryTextColor) + textInputEditText?.setText(value) + textInputEditText?.addTextChangedListener( + object : TextWatcher { + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { + val length = getVal()?.length?:0 + if (maxLength != null) { + footerView?.text = "$length/$maxLength" + } else { + footerView?.text = "$length" + } + positiveValidation() + valueChangeListener?.callback(s.toString()) + + } + + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { + } + + override fun afterTextChanged(s: Editable) { + } + } + ) + this.textInputLayout = textInputLayout + return textInputLayout + } + + private fun prepareFooter(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView { + val footerView = inflater.inflate(footerComponent, root, false) as TextView + if (maxLength != null) { + footerView.text = "0/$maxLength" + } else { + footerView.text = "0" + } + footerView.setTextColorResourceId(context, formStyleBundle.secondaryTextColor) + this.footerView = footerView + return footerView + + } + + fun updateTitle(label: String) { + headerView?.text = label + } + + fun updateText(text: String) { + textInputEditText?.setText(text) + } + + private fun positiveValidation() { + try { + super.validate() + textInputLayout?.error = null + } catch (e: ValidationException) { + } + } + + override fun validate() { + try { + super.validate() + textInputLayout?.error = null + } catch (e: ValidationException) { + textInputLayout?.error = e.message + throw e + } + } + + private fun prepareHeader(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView { + val headerView = inflater.inflate(headerComponent, root, false) as TextView + headerView.setTextColorResourceId(context, formStyleBundle.primaryTextColor) + headerView.setBackgroundColorResourceId(context, formStyleBundle.primaryBackgroundColor) + headerView.text = title + this.headerView = headerView + return headerView + } +} \ No newline at end of file diff --git a/formbuilderlibrary/src/main/res/layout/form_footer_text_right.xml b/formbuilderlibrary/src/main/res/layout/form_footer_text_right.xml new file mode 100644 index 0000000..81653af --- /dev/null +++ b/formbuilderlibrary/src/main/res/layout/form_footer_text_right.xml @@ -0,0 +1,9 @@ + + diff --git a/formbuilderlibrary/src/main/res/layout/form_textarea_item.xml b/formbuilderlibrary/src/main/res/layout/form_textarea_item.xml new file mode 100644 index 0000000..e089f3c --- /dev/null +++ b/formbuilderlibrary/src/main/res/layout/form_textarea_item.xml @@ -0,0 +1,5 @@ + + + \ No newline at end of file