use the helper functions for removing recyclerview items
This commit is contained in:
parent
aa04c74ae0
commit
0e7def3932
7 changed files with 54 additions and 35 deletions
|
@ -47,7 +47,7 @@ ext {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.simplemobiletools:commons:3.0.5'
|
||||
implementation 'com.simplemobiletools:commons:3.0.7'
|
||||
implementation 'joda-time:joda-time:2.9.9'
|
||||
implementation 'com.facebook.stetho:stetho:1.5.0'
|
||||
implementation 'com.android.support:multidex:1.0.2'
|
||||
|
|
|
@ -7,7 +7,6 @@ import com.simplemobiletools.calendar.R
|
|||
import com.simplemobiletools.calendar.adapters.ManageEventTypesAdapter
|
||||
import com.simplemobiletools.calendar.dialogs.UpdateEventTypeDialog
|
||||
import com.simplemobiletools.calendar.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.helpers.DBHelper
|
||||
import com.simplemobiletools.calendar.interfaces.DeleteEventTypesListener
|
||||
import com.simplemobiletools.calendar.models.EventType
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
|
@ -56,18 +55,12 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteEventTypesListener {
|
|||
}
|
||||
|
||||
override fun deleteEventTypes(eventTypes: ArrayList<EventType>, deleteEvents: Boolean) {
|
||||
if (eventTypes.map { it.id }.contains(DBHelper.REGULAR_EVENT_TYPE_ID)) {
|
||||
toast(R.string.cannot_delete_default_type)
|
||||
}
|
||||
|
||||
if (eventTypes.any { it.caldavCalendarId != 0 }) {
|
||||
toast(R.string.unsync_caldav_calendar)
|
||||
}
|
||||
|
||||
dbHelper.deleteEventTypes(eventTypes, deleteEvents) {
|
||||
if (it > 0) {
|
||||
getEventTypes()
|
||||
} else {
|
||||
if (it == 0) {
|
||||
toast(R.string.unknown_error_occurred)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,16 +18,12 @@ import com.simplemobiletools.commons.extensions.beInvisibleIf
|
|||
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||
import kotlinx.android.synthetic.main.event_item_day_view.view.*
|
||||
|
||||
class DayEventsAdapter(activity: SimpleActivity, val events: List<Event>, val listener: DeleteEventsListener?, recyclerView: MyRecyclerView,
|
||||
class DayEventsAdapter(activity: SimpleActivity, val events: ArrayList<Event>, val listener: DeleteEventsListener?, recyclerView: MyRecyclerView,
|
||||
itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
|
||||
|
||||
private var allDayString = resources.getString(R.string.all_day)
|
||||
private var replaceDescriptionWithLocation = activity.config.replaceDescription
|
||||
|
||||
init {
|
||||
selectableItemCount = events.count()
|
||||
}
|
||||
|
||||
override fun getActionMenuId() = R.menu.cab_day
|
||||
|
||||
override fun prepareActionMode(menu: Menu) {}
|
||||
|
@ -45,6 +41,8 @@ class DayEventsAdapter(activity: SimpleActivity, val events: List<Event>, val li
|
|||
}
|
||||
}
|
||||
|
||||
override fun getSelectableItemCount() = events.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = createViewHolder(R.layout.event_item_day_view, parent)
|
||||
|
||||
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
|
||||
|
@ -107,12 +105,19 @@ class DayEventsAdapter(activity: SimpleActivity, val events: List<Event>, val li
|
|||
}
|
||||
|
||||
DeleteEventDialog(activity, eventIds) {
|
||||
val eventsToDelete = ArrayList<Event>(selectedPositions.size)
|
||||
selectedPositions.sortedDescending().forEach {
|
||||
val event = events[it]
|
||||
eventsToDelete.add(event)
|
||||
}
|
||||
events.removeAll(eventsToDelete)
|
||||
|
||||
if (it) {
|
||||
listener?.deleteItems(eventIds)
|
||||
} else {
|
||||
listener?.addEventRepeatException(eventIds, timestamps)
|
||||
}
|
||||
finishActMode()
|
||||
removeSelectedItems()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ import com.simplemobiletools.commons.views.MyRecyclerView
|
|||
import kotlinx.android.synthetic.main.event_list_item.view.*
|
||||
import java.util.*
|
||||
|
||||
class EventListAdapter(activity: SimpleActivity, val listItems: List<ListItem>, val allowLongClick: Boolean, val listener: DeleteEventsListener?,
|
||||
class EventListAdapter(activity: SimpleActivity, val listItems: ArrayList<ListItem>, val allowLongClick: Boolean, val listener: DeleteEventsListener?,
|
||||
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
|
||||
|
||||
private val ITEM_EVENT = 0
|
||||
|
@ -34,10 +34,6 @@ class EventListAdapter(activity: SimpleActivity, val listItems: List<ListItem>,
|
|||
private val now = (System.currentTimeMillis() / 1000).toInt()
|
||||
private val todayDate = Formatter.getDayTitle(activity, Formatter.getDayCodeFromTS(now))
|
||||
|
||||
init {
|
||||
selectableItemCount = listItems.filter { it is ListEvent }.size
|
||||
}
|
||||
|
||||
override fun getActionMenuId() = R.menu.cab_event_list
|
||||
|
||||
override fun prepareActionMode(menu: Menu) {}
|
||||
|
@ -55,6 +51,8 @@ class EventListAdapter(activity: SimpleActivity, val listItems: List<ListItem>,
|
|||
}
|
||||
}
|
||||
|
||||
override fun getSelectableItemCount() = listItems.filter { it is ListEvent }.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyRecyclerViewAdapter.ViewHolder {
|
||||
val layoutId = if (viewType == ITEM_EVENT) R.layout.event_list_item else R.layout.event_list_section
|
||||
return createViewHolder(layoutId, parent)
|
||||
|
@ -150,12 +148,19 @@ class EventListAdapter(activity: SimpleActivity, val listItems: List<ListItem>,
|
|||
}
|
||||
|
||||
DeleteEventDialog(activity, eventIds) {
|
||||
val listItemsToDelete = ArrayList<ListItem>(selectedPositions.size)
|
||||
selectedPositions.sortedDescending().forEach {
|
||||
val listItem = listItems[it]
|
||||
listItemsToDelete.add(listItem)
|
||||
}
|
||||
listItems.removeAll(listItemsToDelete)
|
||||
|
||||
if (it) {
|
||||
listener?.deleteItems(eventIds)
|
||||
} else {
|
||||
listener?.addEventRepeatException(eventIds, timestamps)
|
||||
}
|
||||
finishActMode()
|
||||
removeSelectedItems()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,24 +7,22 @@ import com.simplemobiletools.calendar.R
|
|||
import com.simplemobiletools.calendar.activities.SimpleActivity
|
||||
import com.simplemobiletools.calendar.extensions.config
|
||||
import com.simplemobiletools.calendar.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.helpers.DBHelper
|
||||
import com.simplemobiletools.calendar.interfaces.DeleteEventTypesListener
|
||||
import com.simplemobiletools.calendar.models.EventType
|
||||
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
||||
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
|
||||
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
|
||||
import com.simplemobiletools.commons.extensions.setBackgroundWithStroke
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import com.simplemobiletools.commons.models.RadioItem
|
||||
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||
import kotlinx.android.synthetic.main.item_event_type.view.*
|
||||
import java.util.*
|
||||
|
||||
class ManageEventTypesAdapter(activity: SimpleActivity, val eventTypes: List<EventType>, val listener: DeleteEventTypesListener?, recyclerView: MyRecyclerView,
|
||||
class ManageEventTypesAdapter(activity: SimpleActivity, val eventTypes: ArrayList<EventType>, val listener: DeleteEventTypesListener?, recyclerView: MyRecyclerView,
|
||||
itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
|
||||
|
||||
init {
|
||||
selectableItemCount = eventTypes.size
|
||||
}
|
||||
|
||||
override fun getActionMenuId() = R.menu.cab_event_type
|
||||
|
||||
override fun prepareActionMode(menu: Menu) {}
|
||||
|
@ -41,6 +39,8 @@ class ManageEventTypesAdapter(activity: SimpleActivity, val eventTypes: List<Eve
|
|||
}
|
||||
}
|
||||
|
||||
override fun getSelectableItemCount() = eventTypes.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = createViewHolder(R.layout.item_event_type, parent)
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
|
@ -74,18 +74,34 @@ class ManageEventTypesAdapter(activity: SimpleActivity, val eventTypes: List<Eve
|
|||
add(RadioItem(DELETE_EVENTS, res.getString(R.string.remove_affected_events)))
|
||||
}
|
||||
RadioGroupDialog(activity, items, -1) {
|
||||
finishActMode()
|
||||
deleteEventTypes(it == DELETE_EVENTS, eventTypes)
|
||||
deleteEventTypes(it == DELETE_EVENTS)
|
||||
}
|
||||
} else {
|
||||
ConfirmationDialog(activity) {
|
||||
deleteEventTypes(true, eventTypes)
|
||||
deleteEventTypes(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun deleteEventTypes(deleteEvents: Boolean, eventTypes: ArrayList<EventType>) {
|
||||
listener?.deleteEventTypes(eventTypes, deleteEvents)
|
||||
finishActMode()
|
||||
private fun deleteEventTypes(deleteEvents: Boolean) {
|
||||
val eventTypesToDelete = ArrayList<EventType>(selectedPositions.size)
|
||||
|
||||
for (pos in selectedPositions) {
|
||||
if (eventTypes[pos].id == DBHelper.REGULAR_EVENT_TYPE_ID) {
|
||||
activity.toast(R.string.cannot_delete_default_type)
|
||||
selectedPositions.remove(pos)
|
||||
toggleItemSelection(false, pos)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
selectedPositions.sortedDescending().forEach {
|
||||
val eventType = eventTypes[it]
|
||||
eventTypesToDelete.add(eventType)
|
||||
}
|
||||
|
||||
eventTypes.removeAll(eventTypesToDelete)
|
||||
listener?.deleteEventTypes(eventTypesToDelete, deleteEvents)
|
||||
removeSelectedItems()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -133,7 +133,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListen
|
|||
}
|
||||
}
|
||||
|
||||
private fun updateEvents(events: List<Event>) {
|
||||
private fun updateEvents(events: ArrayList<Event>) {
|
||||
if (activity == null)
|
||||
return
|
||||
|
||||
|
@ -156,7 +156,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListen
|
|||
|
||||
override fun deleteItems(ids: ArrayList<Int>) {
|
||||
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) })
|
||||
DBHelper.newInstance(context!!, this).deleteEvents(eventIDs, true)
|
||||
context!!.dbHelper.deleteEvents(eventIDs, true)
|
||||
}
|
||||
|
||||
override fun addEventRepeatException(parentIds: ArrayList<Int>, timestamps: ArrayList<Int>) {
|
||||
|
|
|
@ -122,7 +122,7 @@ class EventListFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEvents
|
|||
|
||||
override fun deleteItems(ids: ArrayList<Int>) {
|
||||
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) })
|
||||
DBHelper.newInstance(context!!, this).deleteEvents(eventIDs, true)
|
||||
context!!.dbHelper.deleteEvents(eventIDs, true)
|
||||
}
|
||||
|
||||
override fun addEventRepeatException(parentIds: ArrayList<Int>, timestamps: ArrayList<Int>) {
|
||||
|
|
Loading…
Reference in a new issue