From d0619f365ab35aa48e71b60a2dcce6f417040ceb Mon Sep 17 00:00:00 2001 From: balakz Date: Wed, 24 Feb 2021 01:35:18 +0100 Subject: [PATCH] FEAT: add note to switch element --- .../formbuilderproject/MainActivity.kt | 9 +++ .../element/LabelSwitchElement.kt | 69 ++++++++++++++++--- .../src/main/res/drawable/ic_help.xml | 9 +++ .../src/main/res/layout/form_note_symbol.xml | 7 ++ .../src/main/res/values/strings.xml | 1 + 5 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 formbuilderlibrary/src/main/res/drawable/ic_help.xml create mode 100644 formbuilderlibrary/src/main/res/layout/form_note_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 f242d72..81d8453 100644 --- a/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt +++ b/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt @@ -143,6 +143,15 @@ class MainActivity : AppCompatActivity() { LabelSwitchElement("LabelSwitchElement label", true, showToastCheckboxCallback), true ) + addElement( + LabelSwitchElement( + "LabelSwitchNoteElement label", + true, + showToastCheckboxCallback, + noteCallback = showToastActionCallback + ), + true + ) addElement( LabelSpinnerElement( "LabelSpinnerElement label", diff --git a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelSwitchElement.kt b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelSwitchElement.kt index 2591fcc..b6aead6 100644 --- a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelSwitchElement.kt +++ b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelSwitchElement.kt @@ -4,8 +4,10 @@ import android.content.Context import android.content.res.ColorStateList import android.graphics.Color 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.appcompat.widget.SwitchCompat import androidx.core.content.ContextCompat @@ -14,6 +16,7 @@ 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.CheckboxCallback import cz.qase.android.formbuilderlibrary.element.generic.FormElementValid @@ -23,8 +26,10 @@ class LabelSwitchElement( 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 noteSymbolComponent: Int = R.layout.form_note_symbol, private val switchComponent: Int = R.layout.form_inline_switch, private val groupComponentEnd: Int = R.layout.form_group_item_inline_end, + private val noteCallback: ActionCallback? = null, private val formStyleBundle: FormStyleBundle? = null ) : FormElementValid() { @@ -51,19 +56,50 @@ class LabelSwitchElement( inflater, context, this.formStyleBundle ?: formStyleBundle, view ) - val textView = prepareSwitch( + val switchView = prepareSwitch( inflater, context, this.formStyleBundle ?: formStyleBundle, groupEnd ) view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor) view.addView(headerView) - groupEnd.addView(textView) + noteCallback?.let { actionCallback -> + val noteView = inflater.inflate(noteSymbolComponent, groupEnd, false) as ImageView + groupEnd.addView(noteView) + val color = ContextCompat.getColor(context, formStyleBundle.dividerColor) + noteView.setColorFilter(color) + noteView.setOnClickListener { actionCallback.callback() } + noteView.setOnTouchListener { _, event -> + when (event.action) { + MotionEvent.ACTION_UP -> { + val color = ContextCompat.getColor(context, formStyleBundle.dividerColor) + noteView.setColorFilter(color) + noteView.performClick() + } + MotionEvent.ACTION_CANCEL -> { + val color = ContextCompat.getColor(context, formStyleBundle.dividerColor) + noteView.setColorFilter(color) + } + MotionEvent.ACTION_DOWN -> { + val color = + ContextCompat.getColor(context, formStyleBundle.primaryTextColor) + noteView.setColorFilter(color) + } + } + true + } + } + groupEnd.addView(switchView) view.addView(groupEnd) viewGroup = view return view } - private fun prepareSwitch(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): SwitchCompat { + private fun prepareSwitch( + inflater: LayoutInflater, + context: Context, + formStyleBundle: FormStyleBundle, + root: ViewGroup + ): SwitchCompat { val tmpSwitchView = inflater.inflate(switchComponent, root, false) as SwitchCompat tmpSwitchView.isChecked = initialValue tmpSwitchView.setColor(formStyleBundle.primaryTextColor, context) @@ -75,7 +111,12 @@ class LabelSwitchElement( return tmpSwitchView } - private fun prepareHeader(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView { + 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.text = label @@ -85,21 +126,27 @@ class LabelSwitchElement( private fun SwitchCompat.setColor(colorRes: Int, context: Context) { // trackColor is the thumbColor with 30% transparency (77) - val color = ContextCompat.getColor(context, colorRes) - val trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color)) // setting the thumb color - DrawableCompat.setTintList(thumbDrawable, ColorStateList( + DrawableCompat.setTintList( + thumbDrawable, ColorStateList( arrayOf(intArrayOf(android.R.attr.state_checked), intArrayOf()), - intArrayOf(color, Color.WHITE))) + intArrayOf(color, Color.WHITE) + ) + ) // setting the track color - DrawableCompat.setTintList(trackDrawable, ColorStateList( + DrawableCompat.setTintList( + trackDrawable, ColorStateList( arrayOf(intArrayOf(android.R.attr.state_checked), intArrayOf()), - intArrayOf(trackColor, Color.parseColor("#4D000000") // full black with 30% transparency (4D) - ))) + intArrayOf( + trackColor, + Color.parseColor("#4D000000") // full black with 30% transparency (4D) + ) + ) + ) } public override fun enable() { diff --git a/formbuilderlibrary/src/main/res/drawable/ic_help.xml b/formbuilderlibrary/src/main/res/drawable/ic_help.xml new file mode 100644 index 0000000..12a991f --- /dev/null +++ b/formbuilderlibrary/src/main/res/drawable/ic_help.xml @@ -0,0 +1,9 @@ + + + diff --git a/formbuilderlibrary/src/main/res/layout/form_note_symbol.xml b/formbuilderlibrary/src/main/res/layout/form_note_symbol.xml new file mode 100644 index 0000000..97c26d9 --- /dev/null +++ b/formbuilderlibrary/src/main/res/layout/form_note_symbol.xml @@ -0,0 +1,7 @@ + + diff --git a/formbuilderlibrary/src/main/res/values/strings.xml b/formbuilderlibrary/src/main/res/values/strings.xml index 3daedb2..50d1a85 100644 --- a/formbuilderlibrary/src/main/res/values/strings.xml +++ b/formbuilderlibrary/src/main/res/values/strings.xml @@ -2,4 +2,5 @@ Form Builder Library Navigace Otevřít + Note