Skip to content

Commit

Permalink
FEAT: add note to switch element
Browse files Browse the repository at this point in the history
  • Loading branch information
balakz committed Feb 24, 2021
1 parent 627fc32 commit d0619f3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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<Boolean>() {

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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() {
Expand Down
9 changes: 9 additions & 0 deletions formbuilderlibrary/src/main/res/drawable/ic_help.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,19h-2v-2h2v2zM15.07,11.25l-0.9,0.92C13.45,12.9 13,13.5 13,15h-2v-0.5c0,-1.1 0.45,-2.1 1.17,-2.83l1.24,-1.26c0.37,-0.36 0.59,-0.86 0.59,-1.41 0,-1.1 -0.9,-2 -2,-2s-2,0.9 -2,2L8,9c0,-2.21 1.79,-4 4,-4s4,1.79 4,4c0,0.88 -0.36,1.68 -0.93,2.25z" />
</vector>
7 changes: 7 additions & 0 deletions formbuilderlibrary/src/main/res/layout/form_note_symbol.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/noteSymbol"
android:layout_width="30dp"
android:layout_height="30dp"
android:contentDescription="@string/note"
android:src="@drawable/ic_help" />
1 change: 1 addition & 0 deletions formbuilderlibrary/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<string name="app_name">Form Builder Library</string>
<string name="navigation">Navigace</string>
<string name="open">Otevřít</string>
<string name="note">Note</string>
</resources>

0 comments on commit d0619f3

Please sign in to comment.