allow editing event types

This commit is contained in:
tibbi 2017-02-11 15:16:00 +01:00
parent 20a835f304
commit 8f560983dd
16 changed files with 59 additions and 20 deletions

View File

@ -23,8 +23,8 @@ class ManageEventTypesActivity : SimpleActivity(), EventTypeAdapter.DeleteEventT
updateTextColors(manage_event_types_coordinator)
}
private fun showEventTypeDialog() {
EventTypeDialog(this) {
private fun showEventTypeDialog(eventType: EventType? = null) {
EventTypeDialog(this, eventType) {
getEventTypes()
}
}
@ -39,7 +39,7 @@ class ManageEventTypesActivity : SimpleActivity(), EventTypeAdapter.DeleteEventT
private fun gotEventTypes(eventTypes: List<EventType>) {
val eventTypesAdapter = EventTypeAdapter(this, eventTypes, this) {
showEventTypeDialog(it)
}
manage_event_types_list.apply {

View File

@ -16,16 +16,19 @@ import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import kotlinx.android.synthetic.main.dialog_event_type.view.*
class EventTypeDialog(val activity: Activity, val callback: () -> Unit) : AlertDialog.Builder(activity) {
var currentColor = 0
class EventTypeDialog(val activity: Activity, var eventType: EventType? = null, val callback: () -> Unit) : AlertDialog.Builder(activity) {
var isNewEvent = eventType == null
init {
if (eventType == null)
eventType = EventType(0, "", activity.config.primaryColor)
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_event_type, null).apply {
currentColor = activity.config.primaryColor
setupColor(type_color)
type_title.setText(eventType!!.title)
type_color.setOnClickListener {
ColorPickerDialog(activity, currentColor) {
currentColor = it
ColorPickerDialog(activity, eventType!!.color) {
eventType!!.color = it
setupColor(type_color)
}
}
@ -37,19 +40,32 @@ class EventTypeDialog(val activity: Activity, val callback: () -> Unit) : AlertD
.setNegativeButton(R.string.cancel, null)
.create().apply {
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
activity.setupDialogStuff(view, this, R.string.add_new_type)
activity.setupDialogStuff(view, this, if (isNewEvent) R.string.add_new_type else R.string.edit_type)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val title = view.type_title.value
val eventIdWithTitle = db.getEventTypeIdWithTitle(title)
var isEventTypeTitleTaken = isNewEvent && eventIdWithTitle != -1
if (!isEventTypeTitleTaken)
isEventTypeTitleTaken = !isNewEvent && eventType!!.id != eventIdWithTitle && eventIdWithTitle != -1
if (title.isEmpty()) {
activity.toast(R.string.title_empty)
return@setOnClickListener
} else if (db.doesEventTypeExist(title)) {
} else if (isEventTypeTitleTaken) {
activity.toast(R.string.type_already_exists)
return@setOnClickListener
}
val eventType = EventType(0, title, currentColor)
if (db.addEventType(eventType)) {
eventType!!.title = title
val wasDbUpdated: Boolean
if (isNewEvent) {
wasDbUpdated = db.insertEventType(eventType!!)
} else {
wasDbUpdated = db.updateEventType(eventType!!)
}
if (wasDbUpdated) {
dismiss()
callback.invoke()
} else {
@ -60,6 +76,6 @@ class EventTypeDialog(val activity: Activity, val callback: () -> Unit) : AlertD
}
private fun setupColor(view: ImageView) {
view.setBackgroundWithStroke(currentColor, activity.config.backgroundColor)
view.setBackgroundWithStroke(eventType!!.color, activity.config.backgroundColor)
}
}

View File

@ -110,7 +110,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private fun addRegularEventType(db: SQLiteDatabase) {
val regularEvent = context.resources.getString(R.string.regular_event)
val eventType = EventType(1, regularEvent, context.config.primaryColor)
insertEventType(eventType, db)
addEventType(eventType, db)
}
fun insert(event: Event, insertListener: (event: Event) -> Unit) {
@ -185,15 +185,22 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
}
private fun insertEventType(eventType: EventType, db: SQLiteDatabase) {
addEventType(eventType, db)
private fun addEventType(eventType: EventType, db: SQLiteDatabase) {
insertEventType(eventType, db)
}
fun addEventType(eventType: EventType, db: SQLiteDatabase = mDb): Boolean {
fun insertEventType(eventType: EventType, db: SQLiteDatabase = mDb): Boolean {
val values = fillEventTypeValues(eventType)
return db.insert(TYPES_TABLE_NAME, null, values) != -1L
}
fun updateEventType(eventType: EventType): Boolean {
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
}
private fun fillEventTypeValues(eventType: EventType): ContentValues {
return ContentValues().apply {
put(COL_TYPE_TITLE, eventType.title)
@ -201,17 +208,20 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
}
fun doesEventTypeExist(title: String): Boolean {
fun getEventTypeIdWithTitle(title: String): Int {
val cols = arrayOf(COL_TYPE_ID)
val selection = "$COL_TYPE_TITLE = ?"
val selectionArgs = arrayOf(title)
var cursor: Cursor? = null
try {
cursor = mDb.query(TYPES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
return cursor.count == 1
if (cursor?.moveToFirst() == true) {
return cursor.getIntValue(COL_TYPE_ID)
}
} finally {
cursor?.close()
}
return -1
}
fun deleteEvents(ids: Array<String>) {
@ -365,7 +375,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val cols = arrayOf(COL_TYPE_ID, COL_TYPE_TITLE, COL_TYPE_COLOR)
var cursor: Cursor? = null
try {
cursor = mDb.query(TYPES_TABLE_NAME, cols, null, null, null, null, COL_TYPE_ID)
cursor = mDb.query(TYPES_TABLE_NAME, cols, null, null, null, null, "$COL_TYPE_TITLE ASC")
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(COL_TYPE_ID)

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Typy udalostí</string>
<string name="add_new_type">Pridať nový typ</string>
<string name="edit_type">Upraviť typ</string>
<string name="type_already_exists">Typ s daným názvom už existuje</string>
<string name="color">Farba</string>
<string name="regular_event">Bežná udalosť</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>

View File

@ -83,6 +83,7 @@
<!-- Event types -->
<string name="event_types">Event types</string>
<string name="add_new_type">Add a new type</string>
<string name="edit_type">Edit type</string>
<string name="type_already_exists">Type with this title already exists</string>
<string name="color">Color</string>
<string name="regular_event">Regular event</string>