Skip to content

Commit

Permalink
FEAT: NavigationWithErrorElement
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdeněk Balák committed Jul 9, 2020
1 parent d54a970 commit c79f23c
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion formbuilderlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "7.5"
versionName "8.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
9 changes: 9 additions & 0 deletions formbuilderlibrary/src/main/res/drawable/ic_error.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,17h-2v-2h2v2zM13,13h-2L11,7h2v6z" />
</vector>
14 changes: 14 additions & 0 deletions formbuilderlibrary/src/main/res/layout/form_error_symbol.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/errorSymbol"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:tint="@color/colorRed"
android:contentDescription="@string/navigation"
android:src="@drawable/ic_error"
android:textAlignment="gravity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="@+id/navigationSymbol"
app:layout_constraintTop_toTopOf="parent" />
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imageValue"
android:id="@+id/navigationSymbol"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
Expand Down
1 change: 1 addition & 0 deletions formbuilderlibrary/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<color name="colorBackground">#ffffff</color>
<color name="colorGreen">#2ca123</color>
<color name="colorOrange">#ffbc03</color>
<color name="colorRed">#F44336</color>



Expand Down

0 comments on commit c79f23c

Please sign in to comment.