show proper toast message when updating individual repeatable instances

This commit is contained in:
tibbi 2017-06-13 23:21:32 +02:00
parent 1c14ccc8f3
commit 4b2ebcafd3
3 changed files with 47 additions and 32 deletions

View File

@ -459,7 +459,14 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
} }
if (mEvent.id == 0) { 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 { } else {
if (mRepeatInterval > 0 && wasRepeatable) { if (mRepeatInterval > 0 && wasRepeatable) {
EditRepeatingEventDialog(this) { EditRepeatingEventDialog(this) {
@ -469,7 +476,10 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
dbHelper.addEventRepeatException(mEvent.id, mEventOccurrenceTS) dbHelper.addEventRepeatException(mEvent.id, mEventOccurrenceTS)
mEvent.parentId = mEvent.id mEvent.parentId = mEvent.id
mEvent.id = 0 mEvent.id = 0
dbHelper.insert(mEvent) dbHelper.insert(mEvent) {
toast(R.string.event_updated)
finish()
}
} }
} }
} else { } else {
@ -615,12 +625,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
} }
override fun eventInserted(event: Event) { 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) { override fun eventUpdated(event: Event) {

View File

@ -160,9 +160,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
addEventType(eventType, db) addEventType(eventType, db)
} }
fun insert(event: Event): Int { fun insert(event: Event, callback: (id: Int) -> Unit) {
if (event.startTS > event.endTS || event.title.trim().isEmpty()) if (event.startTS > event.endTS || event.title.trim().isEmpty()) {
return 0 callback(0)
return
}
val eventValues = fillEventValues(event) val eventValues = fillEventValues(event)
val id = mDb.insert(MAIN_TABLE_NAME, null, eventValues) 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.updateWidgets()
context.scheduleReminder(event, this) context.scheduleReminder(event, this)
mEventsListener?.eventInserted(event) callback(event.id)
return event.id
} }
fun update(event: Event) { 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 { private fun fillExceptionValues(parentEventId: Int, occurrenceTS: Int, callback: (values: ContentValues) -> Unit) {
val childEvent = getEventWithId(parentEventId) ?: return ContentValues() val childEvent = getEventWithId(parentEventId)
childEvent.id = 0 if (childEvent == null) {
childEvent.parentId = parentEventId callback(ContentValues())
childEvent.startTS = 0 return
childEvent.endTS = 0 }
val childId = insert(childEvent)
return ContentValues().apply { childEvent.apply {
id = 0
parentId = parentEventId
startTS = 0
endTS = 0
}
insert(childEvent) {
callback(ContentValues().apply {
put(COL_PARENT_EVENT_ID, parentEventId) put(COL_PARENT_EVENT_ID, parentEventId)
put(COL_OCCURRENCE_DAYCODE, Formatter.getDayCodeFromTS(occurrenceTS)) put(COL_OCCURRENCE_DAYCODE, Formatter.getDayCodeFromTS(occurrenceTS))
put(COL_CHILD_EVENT_ID, childId) 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) { fun addEventRepeatException(parentEventId: Int, occurrenceTS: Int) {
val values = fillExceptionValues(parentEventId, occurrenceTS) fillExceptionValues(parentEventId, occurrenceTS) {
mDb.insert(EXCEPTIONS_TABLE_NAME, null, values) mDb.insert(EXCEPTIONS_TABLE_NAME, null, it)
}
} }
fun deleteEventTypes(ids: ArrayList<Int>, deleteEvents: Boolean, callback: (deletedCnt: Int) -> Unit) { fun deleteEventTypes(ids: ArrayList<Int>, deleteEvents: Boolean, callback: (deletedCnt: Int) -> Unit) {

View File

@ -103,14 +103,14 @@ class IcsImporter {
event.endTS -= DAY event.endTS -= DAY
} }
val eventId = context.dbHelper.insert(event) context.dbHelper.insert(event) {
for (exceptionTS in curRepeatExceptions) { for (exceptionTS in curRepeatExceptions) {
context.dbHelper.addEventRepeatException(eventId, exceptionTS) context.dbHelper.addEventRepeatException(it, exceptionTS)
} }
eventsImported++ eventsImported++
resetValues() resetValues()
} }
}
prevLine = line prevLine = line
} }
} }