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 c97284e7f..1a4f89617 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt @@ -5,12 +5,15 @@ import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.adapters.EventTypeAdapter import com.simplemobiletools.calendar.dialogs.EventTypeDialog import com.simplemobiletools.calendar.helpers.DBHelper +import com.simplemobiletools.calendar.interfaces.DeleteItemsListener import com.simplemobiletools.calendar.models.EventType +import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.views.RecyclerViewDivider import kotlinx.android.synthetic.main.activity_manage_event_types.* +import java.util.* -class ManageEventTypesActivity : SimpleActivity(), EventTypeAdapter.DeleteEventTypeListener { +class ManageEventTypesActivity : SimpleActivity(), DeleteItemsListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_manage_event_types) @@ -48,7 +51,13 @@ class ManageEventTypesActivity : SimpleActivity(), EventTypeAdapter.DeleteEventT } } - override fun eventTypeDeleted() { - getEventTypes() + override fun deleteItems(ids: ArrayList) { + DBHelper.newInstance(applicationContext).deleteEventTypes(ids) { + if (it > 0) { + getEventTypes() + } else { + toast(R.string.unknown_error_occurred) + } + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt index 316cf2482..f346c14fe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt @@ -10,7 +10,7 @@ import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.helpers.Formatter -import com.simplemobiletools.calendar.interfaces.DeleteEventsListener +import com.simplemobiletools.calendar.interfaces.DeleteItemsListener import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.extensions.beInvisible @@ -18,7 +18,7 @@ import com.simplemobiletools.commons.extensions.beInvisibleIf import kotlinx.android.synthetic.main.event_item_day_view.view.* import java.util.* -class DayEventsAdapter(val activity: SimpleActivity, val mItems: List, val listener: DeleteEventsListener?, val itemClick: (Event) -> Unit) : +class DayEventsAdapter(val activity: SimpleActivity, val mItems: List, val listener: DeleteItemsListener?, val itemClick: (Event) -> Unit) : RecyclerView.Adapter() { val multiSelector = MultiSelector() val views = ArrayList() @@ -82,7 +82,7 @@ class DayEventsAdapter(val activity: SimpleActivity, val mItems: List, va val selections = multiSelector.selectedPositions val ids = ArrayList(selections.size) selections.forEach { ids.add(mItems[it].id) } - listener?.deleteEvents(ids) + listener?.deleteItems(ids) } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListAdapter.kt index cad954527..42850b56c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListAdapter.kt @@ -11,7 +11,7 @@ import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.helpers.Formatter -import com.simplemobiletools.calendar.interfaces.DeleteEventsListener +import com.simplemobiletools.calendar.interfaces.DeleteItemsListener import com.simplemobiletools.calendar.models.ListEvent import com.simplemobiletools.calendar.models.ListItem import com.simplemobiletools.calendar.models.ListSection @@ -21,7 +21,7 @@ import com.simplemobiletools.commons.extensions.beInvisibleIf import kotlinx.android.synthetic.main.event_list_item.view.* import java.util.* -class EventListAdapter(val activity: SimpleActivity, val mItems: List, val listener: DeleteEventsListener?, val itemClick: (Int) -> Unit) : +class EventListAdapter(val activity: SimpleActivity, val mItems: List, val listener: DeleteItemsListener?, val itemClick: (Int) -> Unit) : RecyclerView.Adapter() { val multiSelector = MultiSelector() val views = ArrayList() @@ -98,7 +98,7 @@ class EventListAdapter(val activity: SimpleActivity, val mItems: List, val selections = multiSelector.selectedPositions val ids = ArrayList(selections.size) selections.forEach { ids.add((mItems[it] as ListEvent).id) } - listener?.deleteEvents(ids) + listener?.deleteItems(ids) } override fun getItemViewType(position: Int) = if (mItems[position] is ListEvent) ITEM_EVENT else ITEM_HEADER diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt index 7314c2cf4..c16f63cd6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt @@ -9,13 +9,14 @@ import com.bignerdranch.android.multiselector.SwappingHolder import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.extensions.config +import com.simplemobiletools.calendar.interfaces.DeleteItemsListener import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.extensions.setBackgroundWithStroke import kotlinx.android.synthetic.main.event_type_view.view.* import java.util.* -class EventTypeAdapter(val activity: SimpleActivity, val mItems: List, val listener: DeleteEventTypeListener?, val itemClick: (EventType) -> Unit) : +class EventTypeAdapter(val activity: SimpleActivity, val mItems: List, val listener: DeleteItemsListener?, val itemClick: (EventType) -> Unit) : RecyclerView.Adapter() { val multiSelector = MultiSelector() val views = ArrayList() @@ -74,7 +75,10 @@ class EventTypeAdapter(val activity: SimpleActivity, val mItems: List } private fun deleteEventTypes() { - listener?.eventTypeDeleted() + val selections = multiSelector.selectedPositions + val ids = ArrayList(selections.size) + selections.forEach { ids.add((mItems[it]).id) } + listener?.deleteItems(ids) } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { @@ -132,8 +136,4 @@ class EventTypeAdapter(val activity: SimpleActivity, val mItems: List } } } - - interface DeleteEventTypeListener { - fun eventTypeDeleted() - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt index e665bb31f..1088881a4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -20,7 +20,7 @@ import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.getAppropriateTheme import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.Formatter -import com.simplemobiletools.calendar.interfaces.DeleteEventsListener +import com.simplemobiletools.calendar.interfaces.DeleteItemsListener import com.simplemobiletools.calendar.interfaces.NavigationListener import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.commons.extensions.setupDialogStuff @@ -31,7 +31,7 @@ import org.joda.time.DateTime import java.util.* import kotlin.comparisons.compareBy -class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEventsListener, DeleteEventsListener { +class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEventsListener, DeleteItemsListener { private var mTextColor = 0 private var mDayCode = "" private var mListener: NavigationListener? = null @@ -137,7 +137,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents } } - override fun deleteEvents(ids: ArrayList) { + override fun deleteItems(ids: ArrayList) { val eventIDs = Array(ids.size, { i -> (ids[i].toString()) }) DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt index cb69e5ac7..f06e4d51f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt @@ -15,7 +15,7 @@ import com.simplemobiletools.calendar.extensions.seconds import com.simplemobiletools.calendar.helpers.DBHelper import com.simplemobiletools.calendar.helpers.EVENT_ID import com.simplemobiletools.calendar.helpers.Formatter -import com.simplemobiletools.calendar.interfaces.DeleteEventsListener +import com.simplemobiletools.calendar.interfaces.DeleteItemsListener import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.ListEvent import com.simplemobiletools.calendar.models.ListItem @@ -27,7 +27,7 @@ import org.joda.time.DateTime import java.util.* import kotlin.comparisons.compareBy -class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.EventUpdateListener, DeleteEventsListener { +class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.EventUpdateListener, DeleteItemsListener { var mAllEvents: MutableList = ArrayList() var prevEventsHash = 0 lateinit var mView: View @@ -99,7 +99,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event } } - override fun deleteEvents(ids: ArrayList) { + override fun deleteItems(ids: ArrayList) { val eventIDs = Array(ids.size, { i -> (ids[i].toString()) }) DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs) } 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 8f0a14e25..6671a1e62 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -236,6 +236,12 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont mEventsListener?.eventsDeleted(ids.size) } + fun deleteEventTypes(ids: ArrayList, callback: (deletedCnt: Int) -> Unit) { + val args = TextUtils.join(", ", ids) + val selection = "$COL_TYPE_ID IN ($args)" + callback.invoke(mDb.delete(TYPES_TABLE_NAME, selection, null)) + } + fun getImportIds(): ArrayList { val ids = ArrayList() val columns = arrayOf(COL_IMPORT_ID) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/DeleteEventsListener.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/DeleteEventsListener.kt deleted file mode 100644 index db59a4ddd..000000000 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/DeleteEventsListener.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.simplemobiletools.calendar.interfaces - -import java.util.* - -interface DeleteEventsListener { - fun deleteEvents(ids: ArrayList) -} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/DeleteItemsListener.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/DeleteItemsListener.kt new file mode 100644 index 000000000..63ec06a5e --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/DeleteItemsListener.kt @@ -0,0 +1,7 @@ +package com.simplemobiletools.calendar.interfaces + +import java.util.* + +interface DeleteItemsListener { + fun deleteItems(ids: ArrayList) +}