From c79f23c9b6d2bfdbb3540ec010bab2783c1605b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Bal=C3=A1k?= Date: Thu, 9 Jul 2020 13:27:53 +0200 Subject: [PATCH] FEAT: NavigationWithErrorElement --- .../formbuilderproject/MainActivity.kt | 7 ++ formbuilderlibrary/build.gradle | 2 +- .../element/NavigationWithErrorElement.kt | 110 ++++++++++++++++++ .../src/main/res/drawable/ic_error.xml | 9 ++ .../src/main/res/layout/form_error_symbol.xml | 14 +++ .../res/layout/form_navigation_symbol.xml | 2 +- .../src/main/res/values/colors.xml | 1 + 7 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/NavigationWithErrorElement.kt create mode 100644 formbuilderlibrary/src/main/res/drawable/ic_error.xml create mode 100644 formbuilderlibrary/src/main/res/layout/form_error_symbol.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 d91b8a2..2f89bd5 100644 --- a/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt +++ b/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt @@ -17,6 +17,7 @@ import cz.qase.android.formbuilderlibrary.element.LabelSpinnerElement import cz.qase.android.formbuilderlibrary.element.LabelSwitchElement import cz.qase.android.formbuilderlibrary.element.LabelTextElement import cz.qase.android.formbuilderlibrary.element.NavigationElement +import cz.qase.android.formbuilderlibrary.element.NavigationWithErrorElement import cz.qase.android.formbuilderlibrary.element.OpenableHeaderTextElement import cz.qase.android.formbuilderlibrary.element.TextAreaElement import cz.qase.android.formbuilderlibrary.element.TextElement @@ -161,6 +162,12 @@ class MainActivity : AppCompatActivity() { addElement(ActionElement(showToastActionCallback, "ActionElement label"), true) addElement(ActionTextElement(showToastActionCallback, "Action", "Click me"), true) addElement(NavigationElement(showToastActionCallback, "NavigationElement label"), true) + addElement( + NavigationWithErrorElement( + showToastActionCallback, + "NavigationWithErrorElement label" + ), true + ) addElement(ActionElement(validateActionCallback, "Validate all elements")) addSpace() diff --git a/formbuilderlibrary/build.gradle b/formbuilderlibrary/build.gradle index f6ab9a2..53ceeef 100644 --- a/formbuilderlibrary/build.gradle +++ b/formbuilderlibrary/build.gradle @@ -9,7 +9,7 @@ android { minSdkVersion 21 targetSdkVersion 28 versionCode 1 - versionName "7.5" + versionName "8.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/NavigationWithErrorElement.kt b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/NavigationWithErrorElement.kt new file mode 100644 index 0000000..c03a2c9 --- /dev/null +++ b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/NavigationWithErrorElement.kt @@ -0,0 +1,110 @@ +package cz.qase.android.formbuilderlibrary.element + +import android.content.Context +import android.view.LayoutInflater +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import androidx.core.content.ContextCompat +import cz.qase.android.formbuilderlibrary.FormStyleBundle +import cz.qase.android.formbuilderlibrary.R +import cz.qase.android.formbuilderlibrary.common.setBackgroundColorResourceId +import cz.qase.android.formbuilderlibrary.common.setTextColorResourceId +import cz.qase.android.formbuilderlibrary.element.generic.ActionCallback +import cz.qase.android.formbuilderlibrary.element.generic.FormElementNoValue + +open class NavigationWithErrorElement( + private val actionCallback: ActionCallback, + private val label: String, + private val groupComponent: Int = R.layout.form_group_item_inline, + private val headerComponent: Int = R.layout.form_inline_label, + private val warningSymbolComponent: Int = R.layout.form_error_symbol, + private val symbolComponent: Int = R.layout.form_navigation_symbol, + private val formStyleBundle: FormStyleBundle? = null +) : FormElementNoValue() { + + private var viewGroup: ViewGroup? = null + + override fun hide() { + viewGroup?.visibility = View.GONE + } + + override fun show() { + viewGroup?.visibility = View.VISIBLE + } + + 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 = prepareLabel( + inflater, context, this.formStyleBundle + ?: formStyleBundle, view + ) + val errorView = inflater.inflate(warningSymbolComponent, view, false) as ImageView + val symbolView = prepareSymbol( + inflater, context, this.formStyleBundle + ?: formStyleBundle, view + ) + view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor) + view.addView(headerView) + view.addView(errorView) + view.addView(symbolView) + + view.isClickable = true + view.setOnClickListener { + actionCallback.callback() + } + view.setOnTouchListener { _, event -> + when (event.action) { + MotionEvent.ACTION_UP -> { + view.setBackgroundColorResourceId( + context, + formStyleBundle.secondaryBackgroundColor + ) + view.performClick() + } + MotionEvent.ACTION_CANCEL -> { + view.setBackgroundColorResourceId( + context, + formStyleBundle.secondaryBackgroundColor + ) + } + MotionEvent.ACTION_DOWN -> { + view.setBackgroundColorResourceId( + context, + formStyleBundle.primaryBackgroundColor + ) + } + } + true + } + viewGroup = view + return view + } + + private fun prepareSymbol( + inflater: LayoutInflater, + context: Context, + formStyleBundle: FormStyleBundle, + root: ViewGroup + ): ImageView { + val symbolView = inflater.inflate(symbolComponent, root, false) as ImageView + val color = ContextCompat.getColor(context, formStyleBundle.primaryTextColor) + symbolView.setColorFilter(color) + return symbolView + } + + 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) + headerView.text = label + return headerView + } +} \ No newline at end of file diff --git a/formbuilderlibrary/src/main/res/drawable/ic_error.xml b/formbuilderlibrary/src/main/res/drawable/ic_error.xml new file mode 100644 index 0000000..64c46ba --- /dev/null +++ b/formbuilderlibrary/src/main/res/drawable/ic_error.xml @@ -0,0 +1,9 @@ + + + diff --git a/formbuilderlibrary/src/main/res/layout/form_error_symbol.xml b/formbuilderlibrary/src/main/res/layout/form_error_symbol.xml new file mode 100644 index 0000000..4ee3454 --- /dev/null +++ b/formbuilderlibrary/src/main/res/layout/form_error_symbol.xml @@ -0,0 +1,14 @@ + + diff --git a/formbuilderlibrary/src/main/res/layout/form_navigation_symbol.xml b/formbuilderlibrary/src/main/res/layout/form_navigation_symbol.xml index 198cd00..eae9de8 100644 --- a/formbuilderlibrary/src/main/res/layout/form_navigation_symbol.xml +++ b/formbuilderlibrary/src/main/res/layout/form_navigation_symbol.xml @@ -1,7 +1,7 @@ #ffffff #2ca123 #ffbc03 + #F44336