update event deleting, prepare for deleting future occurrences

This commit is contained in:
tibbi 2018-06-13 12:03:50 +02:00
parent 0c6b749621
commit 41cf2f5744
6 changed files with 50 additions and 26 deletions

View File

@ -563,10 +563,12 @@ class EventActivity : SimpleActivity() {
private fun deleteEvent() {
DeleteEventDialog(this, arrayListOf(mEvent.id)) {
if (it) {
dbHelper.deleteEvents(arrayOf(mEvent.id.toString()), true)
} else {
dbHelper.addEventRepeatException(mEvent.id, mEventOccurrenceTS, true)
when (it) {
DELETE_SELECTED_OCCURRENCE -> dbHelper.addEventRepeatException(mEvent.id, mEventOccurrenceTS, true)
DELETE_FUTURE_OCCURRENCES -> {
}
DELETE_ALL_OCCURRENCES -> dbHelper.deleteEvents(arrayOf(mEvent.id.toString()), true)
}
finish()
}

View File

@ -9,8 +9,7 @@ import com.simplemobiletools.calendar.dialogs.DeleteEventDialog
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.shareEvents
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.LOW_ALPHA
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.adjustAlpha
@ -123,12 +122,18 @@ class DayEventsAdapter(activity: SimpleActivity, val events: ArrayList<Event>, r
}
events.removeAll(eventsToDelete)
if (it) {
val eventIDs = Array(eventIds.size, { i -> (eventIds[i].toString()) })
activity.dbHelper.deleteEvents(eventIDs, true)
} else {
eventIds.forEachIndexed { index, value ->
activity.dbHelper.addEventRepeatException(value, timestamps[index], true)
when (it) {
DELETE_SELECTED_OCCURRENCE -> {
eventIds.forEachIndexed { index, value ->
activity.dbHelper.addEventRepeatException(value, timestamps[index], true)
}
}
DELETE_FUTURE_OCCURRENCES -> {
}
DELETE_ALL_OCCURRENCES -> {
val eventIDs = Array(eventIds.size, { i -> (eventIds[i].toString()) })
activity.dbHelper.deleteEvents(eventIDs, true)
}
}
removeSelectedItems()

View File

@ -9,9 +9,8 @@ import com.simplemobiletools.calendar.dialogs.DeleteEventDialog
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
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.helpers.getNowSeconds
import com.simplemobiletools.calendar.models.ListEvent
import com.simplemobiletools.calendar.models.ListItem
import com.simplemobiletools.calendar.models.ListSection
@ -219,12 +218,18 @@ class EventListAdapter(activity: SimpleActivity, var listItems: ArrayList<ListIt
}
listItems.removeAll(listItemsToDelete)
if (it) {
val eventIDs = Array(eventIds.size, { i -> (eventIds[i].toString()) })
activity.dbHelper.deleteEvents(eventIDs, true)
} else {
eventIds.forEachIndexed { index, value ->
activity.dbHelper.addEventRepeatException(value, timestamps[index], true)
when (it) {
DELETE_SELECTED_OCCURRENCE -> {
eventIds.forEachIndexed { index, value ->
activity.dbHelper.addEventRepeatException(value, timestamps[index], true)
}
}
DELETE_FUTURE_OCCURRENCES -> {
}
DELETE_ALL_OCCURRENCES -> {
val eventIDs = Array(eventIds.size, { i -> (eventIds[i].toString()) })
activity.dbHelper.deleteEvents(eventIDs, true)
}
}
listener?.refreshItems()

View File

@ -5,11 +5,14 @@ import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.helpers.DELETE_ALL_OCCURRENCES
import com.simplemobiletools.calendar.helpers.DELETE_FUTURE_OCCURRENCES
import com.simplemobiletools.calendar.helpers.DELETE_SELECTED_OCCURRENCE
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_delete_event.view.*
class DeleteEventDialog(val activity: Activity, eventIds: List<Int>, val callback: (allOccurrences: Boolean) -> Unit) {
class DeleteEventDialog(val activity: Activity, eventIds: List<Int>, val callback: (deleteRule: Int) -> Unit) {
val dialog: AlertDialog?
init {
@ -26,16 +29,20 @@ class DeleteEventDialog(val activity: Activity, eventIds: List<Int>, val callbac
}
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.yes, { dialog, which -> dialogConfirmed(view as ViewGroup, hasRepeatableEvent) })
.setPositiveButton(R.string.yes, { dialog, which -> dialogConfirmed(view as ViewGroup) })
.setNegativeButton(R.string.no, null)
.create().apply {
activity.setupDialogStuff(view, this)
}
}
private fun dialogConfirmed(view: ViewGroup, hasRepeatableEvent: Boolean) {
val deleteAllOccurrences = !hasRepeatableEvent || view.delete_event_radio_view.checkedRadioButtonId == R.id.delete_event_all
private fun dialogConfirmed(view: ViewGroup) {
val deleteRule = when (view.delete_event_radio_view.checkedRadioButtonId) {
R.id.delete_event_all -> DELETE_ALL_OCCURRENCES
R.id.delete_event_future -> DELETE_FUTURE_OCCURRENCES
else -> DELETE_SELECTED_OCCURRENCE
}
dialog?.dismiss()
callback(deleteAllOccurrences)
callback(deleteRule)
}
}

View File

@ -125,4 +125,8 @@ const val SOURCE_IMPORTED_ICS = "imported-ics"
const val SOURCE_CONTACT_BIRTHDAY = "contact-birthday"
const val SOURCE_CONTACT_ANNIVERSARY = "contact-anniversary"
const val DELETE_SELECTED_OCCURRENCE = 0
const val DELETE_FUTURE_OCCURRENCES = 1
const val DELETE_ALL_OCCURRENCES = 2
fun getNowSeconds() = (System.currentTimeMillis() / 1000).toInt()

View File

@ -559,8 +559,9 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val deletedSet = HashSet<String>()
deleteIds.map { deletedSet.add(it.toString()) }
context.config.removeDisplayEventTypes(deletedSet)
if (deleteIds.isEmpty())
if (deleteIds.isEmpty()) {
return
}
for (eventTypeId in deleteIds) {
if (deleteEvents) {