Skip to content

Commit

Permalink
FEAT: LabelDateTimeElement
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdeněk Balák committed May 2, 2019
1 parent 20c5507 commit 1d2c929
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':formbuilderlibrary')

// Jodatime
implementation 'net.danlew:android.joda:2.9.9'

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cz.qase.android.formbuilderproject

import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import cz.qase.android.formbuilderlibrary.Form
import cz.qase.android.formbuilderlibrary.FormBuilder
Expand All @@ -13,9 +14,12 @@ import cz.qase.android.formbuilderlibrary.element.generic.ValueCallback
import cz.qase.android.formbuilderlibrary.validator.MaxLengthFormValidator
import cz.qase.android.formbuilderlibrary.validator.NotBlankFormValidator
import kotlinx.android.synthetic.main.activity_main.*
import org.joda.time.DateTime
import java.text.SimpleDateFormat
import java.util.Locale
import kotlin.random.Random

class MainActivity : Activity() {
class MainActivity : AppCompatActivity() {

private lateinit var form: Form

Expand Down Expand Up @@ -57,6 +61,11 @@ class MainActivity : Activity() {
Toast.makeText(this@MainActivity, value, Toast.LENGTH_LONG).show()
}
}
private val showToastDateTimeValueCallback = object : ValueCallback<DateTime> {
override fun callback(value: DateTime) {
Toast.makeText(this@MainActivity, SimpleDateFormat("dd.MM.yyyy - HH:mm", Locale.getDefault()).format(value.toDate()), Toast.LENGTH_LONG).show()
}
}


override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -86,7 +95,8 @@ class MainActivity : Activity() {
addElement(HeaderElement("Input elements"), true)
addElement(LabelSwitchElement("LabelSwitchElement label", true, showToastCheckboxCallback), true)
addElement(LabelSpinnerElement("LabelSpinnerElement label", "Option one", stringValues, showToastStringValueCallback), true)
addElement(LabelCheckboxElement("LabelCheckboxElement label", true, showToastCheckboxCallback))
addElement(LabelCheckboxElement("LabelCheckboxElement label", true, showToastCheckboxCallback), true)
addElement(LabelDateTimeElement("Date picker", "Vyberte datum...", supportFragmentManager, valueChangeListener = showToastDateTimeValueCallback))
addSpace()


Expand All @@ -99,7 +109,7 @@ class MainActivity : Activity() {

}, arrayListOf(maxLengthValidator, notEmptyValidator)))
addDivider()
addElement(LabelInputElement("Email", "[email protected]","", object : ValueCallback<String> {
addElement(LabelInputElement("Email", "[email protected]", "", object : ValueCallback<String> {
override fun callback(value: String) {
Toast.makeText(applicationContext, value, Toast.LENGTH_SHORT).show()
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/cz/qase/android/formbuilderproject/MyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package cz.qase.android.formbuilderproject

import android.app.Application
import com.facebook.stetho.Stetho
import net.danlew.android.joda.JodaTimeAndroid

class MyApp : Application() {


override fun onCreate() {
super.onCreate()
JodaTimeAndroid.init(applicationContext)
Stetho.initializeWithDefaults(this)
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorAccent">@color/threeBackgroundPrimary</color>
</resources>
5 changes: 5 additions & 0 deletions formbuilderlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

// Jodatime
implementation 'net.danlew:android.joda:2.9.9'
implementation 'com.github.Qase:AndroidDateTimePicker:1.2.0'

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package cz.qase.android.formbuilderlibrary.element

import android.content.Context
import android.support.v4.app.FragmentManager
import android.view.LayoutInflater
import android.view.MotionEvent
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.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 org.joda.time.DateTime
import wtf.qase.datetimepicker.DateTimePickerDialog
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class LabelDateTimeElement(private val label: String,
private var hint: String,
private var supportFragmentManager: FragmentManager,
private val valueChangeListener: ValueCallback<DateTime>,
private var value: DateTime? = null,
private var sdf: SimpleDateFormat = SimpleDateFormat("dd.MM.yyyy - HH:mm", Locale.getDefault()),
formValidators: MutableList<FormValidator<DateTime>> = ArrayList(),
private val groupComponent: Int = R.layout.form_group_item_inline,
private val headerComponent: Int = R.layout.form_inline_label,
private val textComponent: Int = R.layout.form_inline_text,
private val formStyleBundle: FormStyleBundle? = null) : FormElementValidatable<DateTime>(formValidators) {

override fun getVal(): DateTime? {
return value
}

private var textView: TextView? = null
private var labelView: TextView? = 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 = prepareLabel(inflater, context, this.formStyleBundle
?: formStyleBundle, view)
val textView = prepareText(inflater, context, this.formStyleBundle ?: formStyleBundle, view)
view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor)

view.addView(headerView)
view.addView(textView)
view.setOnTouchListener { _, event ->
when (event.action) {
MotionEvent.ACTION_UP -> {
view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor)
textView.performClick()
}
MotionEvent.ACTION_CANCEL -> {
view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor)
}
MotionEvent.ACTION_DOWN -> {
view.setBackgroundColorResourceId(context, formStyleBundle.primaryBackgroundColor)
}
}
true
}
return view
}

private fun prepareText(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView {
val textView = inflater.inflate(textComponent, root, false) as TextView
textView.setTextColorResourceId(context, formStyleBundle.secondaryTextColor)
textView.text = if (value != null) {
sdf.format(value)
} else {
hint
}
val callback: (date: Date) -> Unit = { newDate ->
val dateTime = DateTime(newDate)
value = dateTime
textView.text = sdf.format(newDate)
valueChangeListener.callback(dateTime)
}
textView.setOnClickListener {
DateTimePickerDialog.show(
supportFragmentManager,
"fragment_datepicker", //tag for fragment manager
callback, //calback with selected date
Date(), //current date
DateTimePickerDialog.TIME_DATE //choose one - DATE_TIME, TIME_ONLY, DATE_ONLY, TIME_DATE
)
}

this.textView = textView
return textView
}

fun updateLabel(label: String) {
textView?.text = label
}

fun updateText(text: String) {
textView?.text = text
}

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
labelView = headerView
return headerView
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class LabelInputElement(private val label: String,
object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
value = s.toString()
validate()
positiveValidation()
if (!invalid) {
valueChangeListener.callback(s.toString())
}
Expand All @@ -83,6 +83,13 @@ class LabelInputElement(private val label: String,
textInputEditText?.setText(text)
}

private fun positiveValidation(){
super.validate()
if (!invalid) {
textInputLayout?.error = null
}
}

override fun validate() {
super.validate()
if (invalid) {
Expand Down

0 comments on commit 1d2c929

Please sign in to comment.