add some event deletion related code

This commit is contained in:
tibbi 2016-09-18 12:02:26 +02:00
parent 92048e5342
commit 78bd16252e
3 changed files with 36 additions and 20 deletions

View File

@ -1,26 +1,25 @@
package com.simplemobiletools.calendar.activities package com.simplemobiletools.calendar.activities
import android.content.Intent import android.content.Intent
import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.support.design.widget.Snackbar import android.support.design.widget.Snackbar
import android.view.View import android.view.View
import com.simplemobiletools.calendar.Constants import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.Formatter import com.simplemobiletools.calendar.Formatter
import com.simplemobiletools.calendar.NavigationListener
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.MyDayPagerAdapter import com.simplemobiletools.calendar.adapters.MyDayPagerAdapter
import com.simplemobiletools.calendar.fragments.DayFragment
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
import kotlinx.android.synthetic.main.activity_day.* import kotlinx.android.synthetic.main.activity_day.*
import org.joda.time.DateTime import org.joda.time.DateTime
import java.util.* import java.util.*
class DayActivity : SimpleActivity(), NavigationListener { class DayActivity : SimpleActivity(), DayFragment.DeleteListener {
private val PREFILLED_DAYS = 61 private val PREFILLED_DAYS = 61
private var mDayCode: String? = null private var mDayCode: String? = null
private var mEvents: MutableList<Event>? = null private var mEvents: MutableList<Event>? = null
private var mSnackbar: Snackbar? = null private var mSnackbar: Snackbar? = null
private var mToBeDeleted: MutableList<Int>? = null
private var mPagerDays: MutableList<String>? = null private var mPagerDays: MutableList<String>? = null
companion object { companion object {
@ -39,8 +38,6 @@ class DayActivity : SimpleActivity(), NavigationListener {
fillViewPager(mDayCode!!) fillViewPager(mDayCode!!)
day_fab.setOnClickListener { addNewEvent() } day_fab.setOnClickListener { addNewEvent() }
//mToBeDeleted = ArrayList<Int>()
} }
override fun onPause() { override fun onPause() {
@ -123,7 +120,7 @@ class DayActivity : SimpleActivity(), NavigationListener {
private fun undoDeletion() { private fun undoDeletion() {
if (mSnackbar != null) { if (mSnackbar != null) {
mToBeDeleted!!.clear() //mToBeDeleted!!.clear()
mSnackbar!!.dismiss() mSnackbar!!.dismiss()
//updateEvents(mEvents) //updateEvents(mEvents)
} }
@ -162,4 +159,12 @@ class DayActivity : SimpleActivity(), NavigationListener {
override fun goToDateTime(dateTime: DateTime) { override fun goToDateTime(dateTime: DateTime) {
fillViewPager(Formatter.getDayCodeFromDateTime(dateTime)) fillViewPager(Formatter.getDayCodeFromDateTime(dateTime))
} }
override fun notifyDeletion(cnt: Int) {
val msg = resources.getQuantityString(R.plurals.events_deleted, cnt, cnt)
mSnackbar = Snackbar.make(day_coordinator, msg, Snackbar.LENGTH_INDEFINITE)
mSnackbar!!.setAction(resources.getString(R.string.undo), undoDeletion)
mSnackbar!!.setActionTextColor(Color.WHITE)
mSnackbar!!.show()
}
} }

View File

@ -5,10 +5,9 @@ import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter import android.support.v4.app.FragmentStatePagerAdapter
import com.simplemobiletools.calendar.Constants import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.NavigationListener
import com.simplemobiletools.calendar.fragments.DayFragment import com.simplemobiletools.calendar.fragments.DayFragment
class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, private val mListener: NavigationListener) : class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, private val mListener: DayFragment.DeleteListener) :
FragmentStatePagerAdapter(fm) { FragmentStatePagerAdapter(fm) {
override fun getCount(): Int { override fun getCount(): Int {

View File

@ -21,19 +21,19 @@ import kotlinx.android.synthetic.main.day_fragment.view.*
import kotlinx.android.synthetic.main.top_navigation.view.* import kotlinx.android.synthetic.main.top_navigation.view.*
import java.util.* import java.util.*
class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnItemClickListener, class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener {
AbsListView.MultiChoiceModeListener {
private val EDIT_EVENT = 1 private val EDIT_EVENT = 1
private var mTextColor: Int = 0 private var mTextColor: Int = 0
private var mSelectedItemsCnt: Int = 0 private var mSelectedItemsCnt: Int = 0
private var mDayCode: String = "" private var mDayCode: String = ""
private var mEvents: MutableList<Event>? = null private var mEvents: MutableList<Event>? = null
private var mListener: NavigationListener? = null private var mListener: DeleteListener? = null
lateinit var mRes: Resources lateinit var mRes: Resources
lateinit var mHolder: RelativeLayout lateinit var mHolder: RelativeLayout
lateinit var mConfig: Config lateinit var mConfig: Config
lateinit var mToBeDeleted: MutableList<Int>
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.day_fragment, container, false) val view = inflater!!.inflate(R.layout.day_fragment, container, false)
@ -53,6 +53,7 @@ class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnIte
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
mToBeDeleted = ArrayList<Int>()
checkEvents() checkEvents()
} }
@ -76,7 +77,7 @@ class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnIte
} }
} }
fun setListener(listener: NavigationListener) { fun setListener(listener: DeleteListener) {
mListener = listener mListener = listener
} }
@ -128,13 +129,20 @@ class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnIte
} }
private fun getEventsToShow(events: MutableList<Event>): List<Event> { private fun getEventsToShow(events: MutableList<Event>): List<Event> {
/*val cnt = events.size return events.filter { !mToBeDeleted.contains(it.id) }
for (i in cnt - 1 downTo 0) {
if (mToBeDeleted!!.contains(events[i].id)) {
events.removeAt(i)
} }
}*/
return events private fun prepareDeleteEvents() {
val checked = mHolder.day_events.checkedItemPositions
for (i in mEvents!!.indices) {
if (checked.get(i)) {
val event = mEvents!![i]
mToBeDeleted.add(event.id)
}
}
mListener?.notifyDeletion(mToBeDeleted.size)
updateEvents(mEvents!!)
} }
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean { override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
@ -144,7 +152,7 @@ class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnIte
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.delete -> { R.id.delete -> {
//prepareDeleteEvents() prepareDeleteEvents()
mode.finish() mode.finish()
return true return true
} }
@ -193,4 +201,8 @@ class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnIte
override fun gotEvents(events: MutableList<Event>) { override fun gotEvents(events: MutableList<Event>) {
updateEvents(events) updateEvents(events)
} }
interface DeleteListener : NavigationListener {
fun notifyDeletion(cnt: Int)
}
} }