add a radio button for creating new event type

This commit is contained in:
tibbi 2017-02-11 21:29:54 +01:00
parent 31cff330a6
commit 7304d74e76

View File

@ -13,6 +13,8 @@ import kotlinx.android.synthetic.main.dialog_radio_group.view.*
import java.util.* import java.util.*
class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val callback: (checkedId: Int) -> Unit) : RadioGroup.OnCheckedChangeListener { class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val callback: (checkedId: Int) -> Unit) : RadioGroup.OnCheckedChangeListener {
val NEW_TYPE_ID = -2
val dialog: AlertDialog? val dialog: AlertDialog?
var wasInit = false var wasInit = false
var eventTypes = ArrayList<EventType>() var eventTypes = ArrayList<EventType>()
@ -27,8 +29,9 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
eventTypes = it eventTypes = it
activity.runOnUiThread { activity.runOnUiThread {
eventTypes.forEach { eventTypes.forEach {
addRadioButton(it) addRadioButton(it.title, it.id)
} }
addRadioButton(activity.getString(R.string.add_new_type), NEW_TYPE_ID)
wasInit = true wasInit = true
} }
} }
@ -39,17 +42,22 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
} }
} }
private fun addRadioButton(type: EventType) { private fun addRadioButton(title: String, typeId: Int) {
val radioButton = (activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton).apply { val radioButton = (activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton).apply {
text = type.title text = title
isChecked = type.id == currEventType isChecked = typeId == currEventType
id = type.id id = typeId
} }
radioGroup.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) radioGroup.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
} }
override fun onCheckedChanged(group: RadioGroup, checkedId: Int) { override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
if (wasInit) { if (!wasInit)
return
if (checkedId == NEW_TYPE_ID) {
} else {
callback.invoke(checkedId) callback.invoke(checkedId)
dialog?.dismiss() dialog?.dismiss()
} }