create a helper extension function for deleting events

This commit is contained in:
tibbi 2018-06-13 20:10:44 +02:00
parent 11b97500ed
commit cf9fa3b685
3 changed files with 27 additions and 34 deletions

View File

@ -8,8 +8,10 @@ import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.dialogs.DeleteEventDialog import com.simplemobiletools.calendar.dialogs.DeleteEventDialog
import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.handleEventDeleting
import com.simplemobiletools.calendar.extensions.shareEvents import com.simplemobiletools.calendar.extensions.shareEvents
import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.LOW_ALPHA
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.adjustAlpha import com.simplemobiletools.commons.extensions.adjustAlpha
@ -126,22 +128,7 @@ class DayEventsAdapter(activity: SimpleActivity, val events: ArrayList<Event>, r
activity.dbHelper.deleteEvents(nonRepeatingEventIDs, true) activity.dbHelper.deleteEvents(nonRepeatingEventIDs, true)
val repeatingEventIDs = eventsToDelete.filter { it.repeatInterval != 0 }.map { it.id } val repeatingEventIDs = eventsToDelete.filter { it.repeatInterval != 0 }.map { it.id }
when (it) { activity.handleEventDeleting(repeatingEventIDs, timestamps, it)
DELETE_SELECTED_OCCURRENCE -> {
repeatingEventIDs.forEachIndexed { index, value ->
activity.dbHelper.addEventRepeatException(value, timestamps[index], true)
}
}
DELETE_FUTURE_OCCURRENCES -> {
repeatingEventIDs.forEachIndexed { index, value ->
activity.dbHelper.addEventRepeatLimit(value, timestamps[index])
}
}
DELETE_ALL_OCCURRENCES -> {
val eventIDs = Array(repeatingEventIDs.size, { i -> (repeatingEventIDs[i].toString()) })
activity.dbHelper.deleteEvents(eventIDs, true)
}
}
removeSelectedItems() removeSelectedItems()
} }
} }

View File

@ -8,9 +8,11 @@ import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.dialogs.DeleteEventDialog import com.simplemobiletools.calendar.dialogs.DeleteEventDialog
import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.handleEventDeleting
import com.simplemobiletools.calendar.extensions.shareEvents import com.simplemobiletools.calendar.extensions.shareEvents
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.LOW_ALPHA
import com.simplemobiletools.calendar.helpers.getNowSeconds
import com.simplemobiletools.calendar.models.ListEvent import com.simplemobiletools.calendar.models.ListEvent
import com.simplemobiletools.calendar.models.ListItem import com.simplemobiletools.calendar.models.ListItem
import com.simplemobiletools.calendar.models.ListSection import com.simplemobiletools.calendar.models.ListSection
@ -220,22 +222,7 @@ class EventListAdapter(activity: SimpleActivity, var listItems: ArrayList<ListIt
activity.dbHelper.deleteEvents(nonRepeatingEventIDs, true) activity.dbHelper.deleteEvents(nonRepeatingEventIDs, true)
val repeatingEventIDs = eventsToDelete.filter { it.isRepeatable }.map { it.id } val repeatingEventIDs = eventsToDelete.filter { it.isRepeatable }.map { it.id }
when (it) { activity.handleEventDeleting(repeatingEventIDs, timestamps, it)
DELETE_SELECTED_OCCURRENCE -> {
repeatingEventIDs.forEachIndexed { index, value ->
activity.dbHelper.addEventRepeatException(value, timestamps[index], true)
}
}
DELETE_FUTURE_OCCURRENCES -> {
repeatingEventIDs.forEachIndexed { index, value ->
activity.dbHelper.addEventRepeatLimit(value, timestamps[index])
}
}
DELETE_ALL_OCCURRENCES -> {
val eventIDs = Array(repeatingEventIDs.size, { i -> (repeatingEventIDs[i].toString()) })
activity.dbHelper.deleteEvents(eventIDs, true)
}
}
listener?.refreshItems() listener?.refreshItems()
finishActMode() finishActMode()
} }

View File

@ -426,3 +426,22 @@ fun Context.getEventListItems(events: List<Event>): ArrayList<ListItem> {
} }
return listItems return listItems
} }
fun Context.handleEventDeleting(eventIds: List<Int>, timestamps: List<Int>, action: Int) {
when (action) {
DELETE_SELECTED_OCCURRENCE -> {
eventIds.forEachIndexed { index, value ->
dbHelper.addEventRepeatException(value, timestamps[index], true)
}
}
DELETE_FUTURE_OCCURRENCES -> {
eventIds.forEachIndexed { index, value ->
dbHelper.addEventRepeatLimit(value, timestamps[index])
}
}
DELETE_ALL_OCCURRENCES -> {
val eventIDs = Array(eventIds.size, { i -> (eventIds[i].toString()) })
dbHelper.deleteEvents(eventIDs, true)
}
}
}