allow creating new event types from Event screen too
This commit is contained in:
parent
7304d74e76
commit
763b6d8109
|
@ -16,7 +16,7 @@ import com.simplemobiletools.commons.extensions.toast
|
|||
import com.simplemobiletools.commons.extensions.value
|
||||
import kotlinx.android.synthetic.main.dialog_event_type.view.*
|
||||
|
||||
class NewEventTypeDialog(val activity: Activity, var eventType: EventType? = null, val callback: () -> Unit) : AlertDialog.Builder(activity) {
|
||||
class NewEventTypeDialog(val activity: Activity, var eventType: EventType? = null, val callback: (eventTypeId: Int) -> Unit) : AlertDialog.Builder(activity) {
|
||||
var isNewEvent = eventType == null
|
||||
|
||||
init {
|
||||
|
@ -58,16 +58,16 @@ class NewEventTypeDialog(val activity: Activity, var eventType: EventType? = nul
|
|||
|
||||
eventType!!.title = title
|
||||
|
||||
val wasDbUpdated: Boolean
|
||||
val eventTypeId: Int
|
||||
if (isNewEvent) {
|
||||
wasDbUpdated = db.insertEventType(eventType!!)
|
||||
eventTypeId = db.insertEventType(eventType!!)
|
||||
} else {
|
||||
wasDbUpdated = db.updateEventType(eventType!!)
|
||||
eventTypeId = db.updateEventType(eventType!!)
|
||||
}
|
||||
|
||||
if (wasDbUpdated) {
|
||||
if (eventTypeId != -1) {
|
||||
dismiss()
|
||||
callback.invoke()
|
||||
callback.invoke(eventTypeId)
|
||||
} else {
|
||||
activity.toast(R.string.unknown_error_occurred)
|
||||
}
|
||||
|
|
|
@ -8,7 +8,9 @@ import android.widget.RadioGroup
|
|||
import com.simplemobiletools.calendar.R
|
||||
import com.simplemobiletools.calendar.helpers.DBHelper
|
||||
import com.simplemobiletools.calendar.models.EventType
|
||||
import com.simplemobiletools.commons.extensions.hideKeyboard
|
||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||
import kotlinx.android.synthetic.main.dialog_radio_group.view.*
|
||||
import java.util.*
|
||||
|
||||
|
@ -21,7 +23,7 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
|
|||
var radioGroup: RadioGroup
|
||||
|
||||
init {
|
||||
val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null)
|
||||
val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null) as ViewGroup
|
||||
radioGroup = view.dialog_radio_group
|
||||
radioGroup.setOnCheckedChangeListener(this)
|
||||
|
||||
|
@ -33,6 +35,7 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
|
|||
}
|
||||
addRadioButton(activity.getString(R.string.add_new_type), NEW_TYPE_ID)
|
||||
wasInit = true
|
||||
activity.updateTextColors(view.dialog_radio_holder)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,7 +59,11 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
|
|||
return
|
||||
|
||||
if (checkedId == NEW_TYPE_ID) {
|
||||
|
||||
NewEventTypeDialog(activity) {
|
||||
callback.invoke(it)
|
||||
activity.hideKeyboard()
|
||||
dialog?.dismiss()
|
||||
}
|
||||
} else {
|
||||
callback.invoke(checkedId)
|
||||
dialog?.dismiss()
|
||||
|
|
|
@ -192,16 +192,16 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||
insertEventType(eventType, db)
|
||||
}
|
||||
|
||||
fun insertEventType(eventType: EventType, db: SQLiteDatabase = mDb): Boolean {
|
||||
fun insertEventType(eventType: EventType, db: SQLiteDatabase = mDb): Int {
|
||||
val values = fillEventTypeValues(eventType)
|
||||
return db.insert(TYPES_TABLE_NAME, null, values) != -1L
|
||||
return db.insert(TYPES_TABLE_NAME, null, values).toInt()
|
||||
}
|
||||
|
||||
fun updateEventType(eventType: EventType): Boolean {
|
||||
fun updateEventType(eventType: EventType): Int {
|
||||
val selectionArgs = arrayOf(eventType.id.toString())
|
||||
val values = fillEventTypeValues(eventType)
|
||||
val selection = "$COL_TYPE_ID = ?"
|
||||
return mDb.update(TYPES_TABLE_NAME, values, selection, selectionArgs) == 1
|
||||
return mDb.update(TYPES_TABLE_NAME, values, selection, selectionArgs)
|
||||
}
|
||||
|
||||
private fun fillEventTypeValues(eventType: EventType): ContentValues {
|
||||
|
|
Loading…
Reference in New Issue