add deleting event types

This commit is contained in:
tibbi 2017-02-11 15:38:29 +01:00
parent 8f560983dd
commit cb009345e4
9 changed files with 43 additions and 28 deletions

View File

@ -5,12 +5,15 @@ import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.EventTypeAdapter import com.simplemobiletools.calendar.adapters.EventTypeAdapter
import com.simplemobiletools.calendar.dialogs.EventTypeDialog import com.simplemobiletools.calendar.dialogs.EventTypeDialog
import com.simplemobiletools.calendar.helpers.DBHelper import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.interfaces.DeleteItemsListener
import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.views.RecyclerViewDivider import com.simplemobiletools.commons.views.RecyclerViewDivider
import kotlinx.android.synthetic.main.activity_manage_event_types.* 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?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_manage_event_types) setContentView(R.layout.activity_manage_event_types)
@ -48,7 +51,13 @@ class ManageEventTypesActivity : SimpleActivity(), EventTypeAdapter.DeleteEventT
} }
} }
override fun eventTypeDeleted() { override fun deleteItems(ids: ArrayList<Int>) {
getEventTypes() DBHelper.newInstance(applicationContext).deleteEventTypes(ids) {
if (it > 0) {
getEventTypes()
} else {
toast(R.string.unknown_error_occurred)
}
}
} }
} }

View File

@ -10,7 +10,7 @@ import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.Formatter 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.Event
import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.beInvisible 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 kotlinx.android.synthetic.main.event_item_day_view.view.*
import java.util.* import java.util.*
class DayEventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val listener: DeleteEventsListener?, val itemClick: (Event) -> Unit) : class DayEventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val listener: DeleteItemsListener?, val itemClick: (Event) -> Unit) :
RecyclerView.Adapter<DayEventsAdapter.ViewHolder>() { RecyclerView.Adapter<DayEventsAdapter.ViewHolder>() {
val multiSelector = MultiSelector() val multiSelector = MultiSelector()
val views = ArrayList<View>() val views = ArrayList<View>()
@ -82,7 +82,7 @@ class DayEventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, va
val selections = multiSelector.selectedPositions val selections = multiSelector.selectedPositions
val ids = ArrayList<Int>(selections.size) val ids = ArrayList<Int>(selections.size)
selections.forEach { ids.add(mItems[it].id) } selections.forEach { ids.add(mItems[it].id) }
listener?.deleteEvents(ids) listener?.deleteItems(ids)
} }
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {

View File

@ -11,7 +11,7 @@ import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.Formatter 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.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
@ -21,7 +21,7 @@ import com.simplemobiletools.commons.extensions.beInvisibleIf
import kotlinx.android.synthetic.main.event_list_item.view.* import kotlinx.android.synthetic.main.event_list_item.view.*
import java.util.* import java.util.*
class EventListAdapter(val activity: SimpleActivity, val mItems: List<ListItem>, val listener: DeleteEventsListener?, val itemClick: (Int) -> Unit) : class EventListAdapter(val activity: SimpleActivity, val mItems: List<ListItem>, val listener: DeleteItemsListener?, val itemClick: (Int) -> Unit) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() { RecyclerView.Adapter<RecyclerView.ViewHolder>() {
val multiSelector = MultiSelector() val multiSelector = MultiSelector()
val views = ArrayList<View>() val views = ArrayList<View>()
@ -98,7 +98,7 @@ class EventListAdapter(val activity: SimpleActivity, val mItems: List<ListItem>,
val selections = multiSelector.selectedPositions val selections = multiSelector.selectedPositions
val ids = ArrayList<Int>(selections.size) val ids = ArrayList<Int>(selections.size)
selections.forEach { ids.add((mItems[it] as ListEvent).id) } 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 override fun getItemViewType(position: Int) = if (mItems[position] is ListEvent) ITEM_EVENT else ITEM_HEADER

View File

@ -9,13 +9,14 @@ import com.bignerdranch.android.multiselector.SwappingHolder
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.interfaces.DeleteItemsListener
import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.setBackgroundWithStroke import com.simplemobiletools.commons.extensions.setBackgroundWithStroke
import kotlinx.android.synthetic.main.event_type_view.view.* import kotlinx.android.synthetic.main.event_type_view.view.*
import java.util.* import java.util.*
class EventTypeAdapter(val activity: SimpleActivity, val mItems: List<EventType>, val listener: DeleteEventTypeListener?, val itemClick: (EventType) -> Unit) : class EventTypeAdapter(val activity: SimpleActivity, val mItems: List<EventType>, val listener: DeleteItemsListener?, val itemClick: (EventType) -> Unit) :
RecyclerView.Adapter<EventTypeAdapter.ViewHolder>() { RecyclerView.Adapter<EventTypeAdapter.ViewHolder>() {
val multiSelector = MultiSelector() val multiSelector = MultiSelector()
val views = ArrayList<View>() val views = ArrayList<View>()
@ -74,7 +75,10 @@ class EventTypeAdapter(val activity: SimpleActivity, val mItems: List<EventType>
} }
private fun deleteEventTypes() { private fun deleteEventTypes() {
listener?.eventTypeDeleted() val selections = multiSelector.selectedPositions
val ids = ArrayList<Int>(selections.size)
selections.forEach { ids.add((mItems[it]).id) }
listener?.deleteItems(ids)
} }
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
@ -132,8 +136,4 @@ class EventTypeAdapter(val activity: SimpleActivity, val mItems: List<EventType>
} }
} }
} }
interface DeleteEventTypeListener {
fun eventTypeDeleted()
}
} }

View File

@ -20,7 +20,7 @@ import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.getAppropriateTheme import com.simplemobiletools.calendar.extensions.getAppropriateTheme
import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.helpers.Formatter 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.interfaces.NavigationListener
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.setupDialogStuff
@ -31,7 +31,7 @@ import org.joda.time.DateTime
import java.util.* import java.util.*
import kotlin.comparisons.compareBy 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 mTextColor = 0
private var mDayCode = "" private var mDayCode = ""
private var mListener: NavigationListener? = null private var mListener: NavigationListener? = null
@ -137,7 +137,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
} }
} }
override fun deleteEvents(ids: ArrayList<Int>) { override fun deleteItems(ids: ArrayList<Int>) {
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) }) val eventIDs = Array(ids.size, { i -> (ids[i].toString()) })
DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs) DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs)
} }

View File

@ -15,7 +15,7 @@ import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.DBHelper import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.EVENT_ID import com.simplemobiletools.calendar.helpers.EVENT_ID
import com.simplemobiletools.calendar.helpers.Formatter 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.Event
import com.simplemobiletools.calendar.models.ListEvent import com.simplemobiletools.calendar.models.ListEvent
import com.simplemobiletools.calendar.models.ListItem import com.simplemobiletools.calendar.models.ListItem
@ -27,7 +27,7 @@ import org.joda.time.DateTime
import java.util.* import java.util.*
import kotlin.comparisons.compareBy import kotlin.comparisons.compareBy
class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.EventUpdateListener, DeleteEventsListener { class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.EventUpdateListener, DeleteItemsListener {
var mAllEvents: MutableList<Event> = ArrayList() var mAllEvents: MutableList<Event> = ArrayList()
var prevEventsHash = 0 var prevEventsHash = 0
lateinit var mView: View lateinit var mView: View
@ -99,7 +99,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event
} }
} }
override fun deleteEvents(ids: ArrayList<Int>) { override fun deleteItems(ids: ArrayList<Int>) {
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) }) val eventIDs = Array(ids.size, { i -> (ids[i].toString()) })
DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs) DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs)
} }

View File

@ -236,6 +236,12 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
mEventsListener?.eventsDeleted(ids.size) mEventsListener?.eventsDeleted(ids.size)
} }
fun deleteEventTypes(ids: ArrayList<Int>, 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<String> { fun getImportIds(): ArrayList<String> {
val ids = ArrayList<String>() val ids = ArrayList<String>()
val columns = arrayOf(COL_IMPORT_ID) val columns = arrayOf(COL_IMPORT_ID)

View File

@ -1,7 +0,0 @@
package com.simplemobiletools.calendar.interfaces
import java.util.*
interface DeleteEventsListener {
fun deleteEvents(ids: ArrayList<Int>)
}

View File

@ -0,0 +1,7 @@
package com.simplemobiletools.calendar.interfaces
import java.util.*
interface DeleteItemsListener {
fun deleteItems(ids: ArrayList<Int>)
}