diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt index 37195d287..c68987217 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt @@ -12,10 +12,7 @@ class ManageEventTypesActivity : SimpleActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_manage_event_types) - DBHelper.newInstance(applicationContext).getEventTypes { - - } - + getEventTypes() manage_event_types_fab.setOnClickListener { showEventTypeDialog() } @@ -25,6 +22,12 @@ class ManageEventTypesActivity : SimpleActivity() { private fun showEventTypeDialog() { EventTypeDialog(this) { + getEventTypes() + } + } + + private fun getEventTypes() { + DBHelper.newInstance(applicationContext).getEventTypes { } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/EventTypeDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/EventTypeDialog.kt index f718d31f4..f6df27bca 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/EventTypeDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/EventTypeDialog.kt @@ -4,19 +4,34 @@ import android.app.Activity import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager +import android.widget.ImageView import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.extensions.config +import com.simplemobiletools.calendar.helpers.DBHelper +import com.simplemobiletools.calendar.models.EventType +import com.simplemobiletools.commons.dialogs.ColorPickerDialog import com.simplemobiletools.commons.extensions.setBackgroundWithStroke import com.simplemobiletools.commons.extensions.setupDialogStuff +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 init { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_event_type, null).apply { - type_color.setBackgroundWithStroke(activity.config.primaryColor, activity.config.backgroundColor) + currentColor = activity.config.primaryColor + setupColor(type_color) + type_color.setOnClickListener { + ColorPickerDialog(activity, currentColor) { + currentColor = it + setupColor(type_color) + } + } } + val db = DBHelper.newInstance(activity) AlertDialog.Builder(activity) .setPositiveButton(R.string.ok, null) .setNegativeButton(R.string.cancel, null) @@ -24,8 +39,27 @@ class EventTypeDialog(val activity: Activity, val callback: () -> Unit) : AlertD window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) activity.setupDialogStuff(view, this, R.string.add_new_type) getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({ + val title = view.type_title.value + if (title.isEmpty()) { + activity.toast(R.string.title_empty) + return@setOnClickListener + } else if (db.doesEventTypeExist(title)) { + activity.toast(R.string.type_already_exists) + return@setOnClickListener + } + val eventType = EventType(0, title, currentColor) + if (db.addEventType(eventType)) { + dismiss() + callback.invoke() + } else { + activity.toast(R.string.unknown_error_occurred) + } }) } } + + private fun setupColor(view: ImageView) { + view.setBackgroundWithStroke(currentColor, activity.config.backgroundColor) + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index 54db362e3..a2dcd61ac 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -186,18 +186,34 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } private fun insertEventType(eventType: EventType, db: SQLiteDatabase) { + addEventType(eventType, db) + } + + fun addEventType(eventType: EventType, db: SQLiteDatabase = mDb): Boolean { val values = fillEventTypeValues(eventType) - db.insert(TYPES_TABLE_NAME, null, values) + return db.insert(TYPES_TABLE_NAME, null, values) != -1L } private fun fillEventTypeValues(eventType: EventType): ContentValues { return ContentValues().apply { - put(COL_TYPE_ID, eventType.id) put(COL_TYPE_TITLE, eventType.title) put(COL_TYPE_COLOR, eventType.color) } } + fun doesEventTypeExist(title: String): Boolean { + 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 + } finally { + cursor?.close() + } + } + fun deleteEvents(ids: Array) { val args = TextUtils.join(", ", ids) val selection = "$COL_ID IN ($args)" diff --git a/app/src/main/res/layout/dialog_event_type.xml b/app/src/main/res/layout/dialog_event_type.xml index 3799bbf17..661e3f412 100644 --- a/app/src/main/res/layout/dialog_event_type.xml +++ b/app/src/main/res/layout/dialog_event_type.xml @@ -22,6 +22,7 @@ android:layout_marginBottom="@dimen/activity_margin" android:layout_marginLeft="@dimen/small_margin" android:layout_marginStart="@dimen/small_margin" + android:inputType="textCapSentences" android:maxLength="50" android:singleLine="true" android:textCursorDrawable="@null"/>