diff --git a/app/build.gradle b/app/build.gradle index 859c5a0..b180584 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' + } \ No newline at end of file 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 3089087..f468817 100644 --- a/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt +++ b/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt @@ -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 @@ -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 @@ -57,6 +61,11 @@ class MainActivity : Activity() { Toast.makeText(this@MainActivity, value, Toast.LENGTH_LONG).show() } } + private val showToastDateTimeValueCallback = object : ValueCallback { + 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?) { @@ -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() @@ -99,7 +109,7 @@ class MainActivity : Activity() { }, arrayListOf(maxLengthValidator, notEmptyValidator))) addDivider() - addElement(LabelInputElement("Email", "test@test.cz","", object : ValueCallback { + addElement(LabelInputElement("Email", "test@test.cz", "", object : ValueCallback { override fun callback(value: String) { Toast.makeText(applicationContext, value, Toast.LENGTH_SHORT).show() } diff --git a/app/src/main/java/cz/qase/android/formbuilderproject/MyApp.kt b/app/src/main/java/cz/qase/android/formbuilderproject/MyApp.kt index b4aa408..3b79a1f 100644 --- a/app/src/main/java/cz/qase/android/formbuilderproject/MyApp.kt +++ b/app/src/main/java/cz/qase/android/formbuilderproject/MyApp.kt @@ -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) } } \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3ab3e9c..6d3aeb7 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -2,5 +2,5 @@ #3F51B5 #303F9F - #FF4081 + @color/threeBackgroundPrimary diff --git a/formbuilderlibrary/build.gradle b/formbuilderlibrary/build.gradle index 11e7b98..36a54fb 100644 --- a/formbuilderlibrary/build.gradle +++ b/formbuilderlibrary/build.gradle @@ -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' + } \ No newline at end of file diff --git a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelDateTimeElement.kt b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelDateTimeElement.kt new file mode 100644 index 0000000..bc13b53 --- /dev/null +++ b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelDateTimeElement.kt @@ -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, + private var value: DateTime? = null, + private var sdf: SimpleDateFormat = SimpleDateFormat("dd.MM.yyyy - HH:mm", Locale.getDefault()), + formValidators: MutableList> = 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(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 + } +} \ No newline at end of file diff --git a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelInputElement.kt b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelInputElement.kt index 8e049d3..f4a0cd1 100644 --- a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelInputElement.kt +++ b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelInputElement.kt @@ -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()) } @@ -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) {