-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zdeněk Balák
committed
May 2, 2019
1 parent
20c5507
commit 1d2c929
Showing
7 changed files
with
145 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<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?) { | ||
|
@@ -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", "[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() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...rlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelDateTimeElement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters