mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-06-05 21:59:17 +02:00
allow creating new event types from Event screen too
This commit is contained in:
@@ -16,7 +16,7 @@ import com.simplemobiletools.commons.extensions.toast
|
|||||||
import com.simplemobiletools.commons.extensions.value
|
import com.simplemobiletools.commons.extensions.value
|
||||||
import kotlinx.android.synthetic.main.dialog_event_type.view.*
|
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
|
var isNewEvent = eventType == null
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@@ -58,16 +58,16 @@ class NewEventTypeDialog(val activity: Activity, var eventType: EventType? = nul
|
|||||||
|
|
||||||
eventType!!.title = title
|
eventType!!.title = title
|
||||||
|
|
||||||
val wasDbUpdated: Boolean
|
val eventTypeId: Int
|
||||||
if (isNewEvent) {
|
if (isNewEvent) {
|
||||||
wasDbUpdated = db.insertEventType(eventType!!)
|
eventTypeId = db.insertEventType(eventType!!)
|
||||||
} else {
|
} else {
|
||||||
wasDbUpdated = db.updateEventType(eventType!!)
|
eventTypeId = db.updateEventType(eventType!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wasDbUpdated) {
|
if (eventTypeId != -1) {
|
||||||
dismiss()
|
dismiss()
|
||||||
callback.invoke()
|
callback.invoke(eventTypeId)
|
||||||
} else {
|
} else {
|
||||||
activity.toast(R.string.unknown_error_occurred)
|
activity.toast(R.string.unknown_error_occurred)
|
||||||
}
|
}
|
||||||
|
@@ -8,7 +8,9 @@ import android.widget.RadioGroup
|
|||||||
import com.simplemobiletools.calendar.R
|
import com.simplemobiletools.calendar.R
|
||||||
import com.simplemobiletools.calendar.helpers.DBHelper
|
import com.simplemobiletools.calendar.helpers.DBHelper
|
||||||
import com.simplemobiletools.calendar.models.EventType
|
import com.simplemobiletools.calendar.models.EventType
|
||||||
|
import com.simplemobiletools.commons.extensions.hideKeyboard
|
||||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||||
|
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||||
import kotlinx.android.synthetic.main.dialog_radio_group.view.*
|
import kotlinx.android.synthetic.main.dialog_radio_group.view.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@@ -21,7 +23,7 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
|
|||||||
var radioGroup: RadioGroup
|
var radioGroup: RadioGroup
|
||||||
|
|
||||||
init {
|
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 = view.dialog_radio_group
|
||||||
radioGroup.setOnCheckedChangeListener(this)
|
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)
|
addRadioButton(activity.getString(R.string.add_new_type), NEW_TYPE_ID)
|
||||||
wasInit = true
|
wasInit = true
|
||||||
|
activity.updateTextColors(view.dialog_radio_holder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +59,11 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
|
|||||||
return
|
return
|
||||||
|
|
||||||
if (checkedId == NEW_TYPE_ID) {
|
if (checkedId == NEW_TYPE_ID) {
|
||||||
|
NewEventTypeDialog(activity) {
|
||||||
|
callback.invoke(it)
|
||||||
|
activity.hideKeyboard()
|
||||||
|
dialog?.dismiss()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
callback.invoke(checkedId)
|
callback.invoke(checkedId)
|
||||||
dialog?.dismiss()
|
dialog?.dismiss()
|
||||||
|
@@ -192,16 +192,16 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||||||
insertEventType(eventType, db)
|
insertEventType(eventType, db)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun insertEventType(eventType: EventType, db: SQLiteDatabase = mDb): Boolean {
|
fun insertEventType(eventType: EventType, db: SQLiteDatabase = mDb): Int {
|
||||||
val values = fillEventTypeValues(eventType)
|
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 selectionArgs = arrayOf(eventType.id.toString())
|
||||||
val values = fillEventTypeValues(eventType)
|
val values = fillEventTypeValues(eventType)
|
||||||
val selection = "$COL_TYPE_ID = ?"
|
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 {
|
private fun fillEventTypeValues(eventType: EventType): ContentValues {
|
||||||
|
Reference in New Issue
Block a user