From 4b2ebcafd302c44b537b90d8c461e757e9bcdd91 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 13 Jun 2017 23:21:32 +0200 Subject: [PATCH] show proper toast message when updating individual repeatable instances --- .../calendar/activities/EventActivity.kt | 21 +++++---- .../calendar/helpers/DBHelper.kt | 46 +++++++++++-------- .../calendar/helpers/IcsImporter.kt | 12 ++--- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt index 06c23a5e6..0084404a7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt @@ -459,7 +459,14 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener { } if (mEvent.id == 0) { - dbHelper.insert(mEvent) + dbHelper.insert(mEvent) { + if (DateTime.now().isAfter(mEventStartDateTime.millis)) { + toast(R.string.past_event_added) + } else { + toast(R.string.event_added) + } + finish() + } } else { if (mRepeatInterval > 0 && wasRepeatable) { EditRepeatingEventDialog(this) { @@ -469,7 +476,10 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener { dbHelper.addEventRepeatException(mEvent.id, mEventOccurrenceTS) mEvent.parentId = mEvent.id mEvent.id = 0 - dbHelper.insert(mEvent) + dbHelper.insert(mEvent) { + toast(R.string.event_updated) + finish() + } } } } else { @@ -615,12 +625,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener { } override fun eventInserted(event: Event) { - if (DateTime.now().isAfter(mEventStartDateTime.millis)) { - toast(R.string.past_event_added) - } else { - toast(R.string.event_added) - } - finish() + } override fun eventUpdated(event: Event) { 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 ad5e1f480..8852b33ad 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -160,9 +160,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont addEventType(eventType, db) } - fun insert(event: Event): Int { - if (event.startTS > event.endTS || event.title.trim().isEmpty()) - return 0 + fun insert(event: Event, callback: (id: Int) -> Unit) { + if (event.startTS > event.endTS || event.title.trim().isEmpty()) { + callback(0) + return + } val eventValues = fillEventValues(event) val id = mDb.insert(MAIN_TABLE_NAME, null, eventValues) @@ -175,8 +177,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont context.updateWidgets() context.scheduleReminder(event, this) - mEventsListener?.eventInserted(event) - return event.id + callback(event.id) } fun update(event: Event) { @@ -251,18 +252,26 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } } - private fun fillExceptionValues(parentEventId: Int, occurrenceTS: Int): ContentValues { - val childEvent = getEventWithId(parentEventId) ?: return ContentValues() - childEvent.id = 0 - childEvent.parentId = parentEventId - childEvent.startTS = 0 - childEvent.endTS = 0 - val childId = insert(childEvent) + private fun fillExceptionValues(parentEventId: Int, occurrenceTS: Int, callback: (values: ContentValues) -> Unit) { + val childEvent = getEventWithId(parentEventId) + if (childEvent == null) { + callback(ContentValues()) + return + } - return ContentValues().apply { - put(COL_PARENT_EVENT_ID, parentEventId) - put(COL_OCCURRENCE_DAYCODE, Formatter.getDayCodeFromTS(occurrenceTS)) - put(COL_CHILD_EVENT_ID, childId) + childEvent.apply { + id = 0 + parentId = parentEventId + startTS = 0 + endTS = 0 + } + + insert(childEvent) { + callback(ContentValues().apply { + put(COL_PARENT_EVENT_ID, parentEventId) + put(COL_OCCURRENCE_DAYCODE, Formatter.getDayCodeFromTS(occurrenceTS)) + put(COL_CHILD_EVENT_ID, it) + }) } } @@ -320,8 +329,9 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } fun addEventRepeatException(parentEventId: Int, occurrenceTS: Int) { - val values = fillExceptionValues(parentEventId, occurrenceTS) - mDb.insert(EXCEPTIONS_TABLE_NAME, null, values) + fillExceptionValues(parentEventId, occurrenceTS) { + mDb.insert(EXCEPTIONS_TABLE_NAME, null, it) + } } fun deleteEventTypes(ids: ArrayList, deleteEvents: Boolean, callback: (deletedCnt: Int) -> Unit) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt index aa7296e2e..6a4acd2a0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt @@ -103,13 +103,13 @@ class IcsImporter { event.endTS -= DAY } - val eventId = context.dbHelper.insert(event) - - for (exceptionTS in curRepeatExceptions) { - context.dbHelper.addEventRepeatException(eventId, exceptionTS) + context.dbHelper.insert(event) { + for (exceptionTS in curRepeatExceptions) { + context.dbHelper.addEventRepeatException(it, exceptionTS) + } + eventsImported++ + resetValues() } - eventsImported++ - resetValues() } prevLine = line }