add event Deletion

This commit is contained in:
tibbi 2016-11-23 20:48:32 +01:00
parent bd033389f0
commit a7f6fda5e8
8 changed files with 46 additions and 59 deletions

View File

@ -37,6 +37,7 @@ dependencies {
compile 'joda-time:joda-time:2.9.1'
compile 'com.github.yukuku:ambilwarna:2.0.1'
compile 'com.facebook.stetho:stetho:1.4.1'
compile 'com.simplemobiletools:filepicker:1.5.4@aar'
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

View File

@ -196,7 +196,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventsListener {
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_event, menu)
val item = menu.findItem(R.id.delete)
val item = menu.findItem(R.id.cab_delete)
if (mEvent.id == 0) {
item.isVisible = false
}
@ -205,7 +205,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventsListener {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.delete -> {
R.id.cab_delete -> {
deleteEvent()
true
}

View File

@ -10,10 +10,11 @@ import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.filepicker.dialogs.ConfirmationDialog
import kotlinx.android.synthetic.main.event_item.view.*
import java.util.*
class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val itemClick: (Event) -> Unit) :
class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val listener: ItemOperationsListener?, val itemClick: (Event) -> Unit) :
RecyclerView.Adapter<EventsAdapter.ViewHolder>() {
val multiSelector = MultiSelector()
val views = ArrayList<View>()
@ -37,6 +38,10 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val i
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
return when (item.itemId) {
R.id.cab_delete -> {
askConfirmDelete()
true
}
else -> false
}
}
@ -57,6 +62,20 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val i
}
}
private fun askConfirmDelete() {
ConfirmationDialog(activity) {
actMode?.finish()
prepareForDeleting()
}
}
private fun prepareForDeleting() {
val selections = multiSelector.selectedPositions
val ids = ArrayList<Int>(selections.size)
selections.forEach { ids.add(mItems[it].id) }
listener?.prepareForDeleting(ids)
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent?.context).inflate(R.layout.event_item, parent, false)
return ViewHolder(activity, view, itemClick)
@ -117,4 +136,8 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val i
}
}
}
interface ItemOperationsListener {
fun prepareForDeleting(ids: ArrayList<Int>)
}
}

View File

@ -8,8 +8,9 @@ import android.graphics.PorterDuff
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AlertDialog
import android.view.*
import android.widget.AbsListView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.DatePicker
import android.widget.RelativeLayout
import com.simplemobiletools.calendar.R
@ -27,8 +28,7 @@ import kotlinx.android.synthetic.main.top_navigation.view.*
import java.util.*
import kotlin.comparisons.compareBy
class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoiceModeListener, DBHelper.GetEventsListener {
class DayFragment : Fragment(), DBHelper.EventsListener, DBHelper.GetEventsListener, EventsAdapter.ItemOperationsListener {
private val EDIT_EVENT = 1
private var mTextColor = 0
@ -128,7 +128,7 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoice
if (activity == null)
return
val eventsAdapter = EventsAdapter(activity as SimpleActivity, eventsToShow) {
val eventsAdapter = EventsAdapter(activity as SimpleActivity, eventsToShow, this) {
editEvent(it.id)
}
mHolder.day_events.apply {
@ -157,14 +157,9 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoice
private fun getEventsToShow(events: MutableList<Event>) = events.filter { !mToBeDeleted.contains(it.id) }
private fun prepareDeleteEvents() {
/*val checked = mHolder.day_events.checkedItemPositions
mEvents!!.indices
.filter { checked.get(it) }
.map { mEvents!![it] }
.forEach { mToBeDeleted.add(it.id) }
notifyDeletion()*/
override fun prepareForDeleting(ids: ArrayList<Int>) {
mToBeDeleted = ids
notifyDeletion()
}
private fun notifyDeletion() {
@ -182,40 +177,6 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoice
updateEvents(mEvents!!)
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu) = true
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
return when (item.itemId) {
R.id.delete -> {
prepareDeleteEvents()
mode.finish()
true
}
else -> false
}
}
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
val inflater = mode.menuInflater
inflater.inflate(R.menu.cab_day, menu)
return true
}
override fun onDestroyActionMode(mode: ActionMode) {
mSelectedItemsCnt = 0
}
override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long, checked: Boolean) {
if (checked) {
mSelectedItemsCnt++
} else {
mSelectedItemsCnt--
}
mode.title = mSelectedItemsCnt.toString()
mode.invalidate()
}
override fun eventInserted(event: Event) {
}

View File

@ -152,7 +152,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
return when (item.itemId) {
R.id.delete -> {
R.id.cab_delete -> {
prepareDeleteEvents()
mode.finish()
true

View File

@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/day_coordinator"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/day_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/activity_margin">
android:layout_height="match_parent">
<com.simplemobiletools.calendar.views.MyViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent"
android:layout_marginTop="@dimen/activity_margin"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/day_fab"

View File

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/delete"
android:id="@+id/cab_delete"
android:icon="@mipmap/delete"
android:title="@string/delete"/>
android:title="@string/delete"
app:showAsAction="ifRoom"/>
</menu>

View File

@ -2,7 +2,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/delete"
android:id="@+id/cab_delete"
android:icon="@mipmap/delete"
android:title="@string/delete"
app:showAsAction="ifRoom"/>