remove the UNDO button after deleting events on daily view

This commit is contained in:
tibbi 2017-02-07 22:54:25 +01:00
parent bf55fd7318
commit 0572e61594
4 changed files with 26 additions and 91 deletions

View File

@ -1,27 +1,23 @@
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.v4.view.ViewPager import android.support.v4.view.ViewPager
import android.view.View
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.extensions.getNewEventTimestampFromCode import com.simplemobiletools.calendar.extensions.getNewEventTimestampFromCode
import com.simplemobiletools.calendar.fragments.DayFragment
import com.simplemobiletools.calendar.helpers.DAY_CODE import com.simplemobiletools.calendar.helpers.DAY_CODE
import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.NEW_EVENT_START_TS import com.simplemobiletools.calendar.helpers.NEW_EVENT_START_TS
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.extensions.updateTextColors
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(), DayFragment.DeleteListener, ViewPager.OnPageChangeListener { class DayActivity : SimpleActivity(), NavigationListener, ViewPager.OnPageChangeListener {
private val PREFILLED_DAYS = 121 private val PREFILLED_DAYS = 121
private var mDayCode = "" private var mDayCode = ""
private var mSnackbar: Snackbar? = null
private var mPagerDays: MutableList<String>? = null private var mPagerDays: MutableList<String>? = null
private var mPagerPos = 0 private var mPagerPos = 0
@ -40,11 +36,6 @@ class DayActivity : SimpleActivity(), DayFragment.DeleteListener, ViewPager.OnPa
updateTextColors(day_coordinator) updateTextColors(day_coordinator)
} }
override fun onPause() {
super.onPause()
checkDeleteEvents()
}
private fun fillViewPager(targetDay: String) { private fun fillViewPager(targetDay: String) {
getDays(targetDay) getDays(targetDay)
val daysAdapter = MyDayPagerAdapter(supportFragmentManager, mPagerDays!!, this) val daysAdapter = MyDayPagerAdapter(supportFragmentManager, mPagerDays!!, this)
@ -71,26 +62,8 @@ class DayActivity : SimpleActivity(), DayFragment.DeleteListener, ViewPager.OnPa
} }
} }
private fun checkDeleteEvents() { fun recheckEvents() {
if (mSnackbar != null && mSnackbar!!.isShown) { (view_pager.adapter as MyDayPagerAdapter).checkDayEvents(mPagerPos)
deleteEvents()
} else {
undoDeletion()
}
}
private fun deleteEvents() {
mSnackbar!!.dismiss()
(view_pager.adapter as MyDayPagerAdapter).deleteItems(mPagerPos)
}
private val undoDeletion = View.OnClickListener { undoDeletion() }
private fun undoDeletion() {
if (mSnackbar != null) {
mSnackbar!!.dismiss()
(view_pager.adapter as MyDayPagerAdapter).undoDeletion(view_pager.currentItem)
}
} }
override fun onPageScrollStateChanged(state: Int) { override fun onPageScrollStateChanged(state: Int) {
@ -102,32 +75,18 @@ class DayActivity : SimpleActivity(), DayFragment.DeleteListener, ViewPager.OnPa
} }
override fun onPageSelected(position: Int) { override fun onPageSelected(position: Int) {
checkDeleteEvents()
mPagerPos = position mPagerPos = position
} }
override fun goLeft() { override fun goLeft() {
checkDeleteEvents()
view_pager.currentItem = view_pager.currentItem - 1 view_pager.currentItem = view_pager.currentItem - 1
} }
override fun goRight() { override fun goRight() {
checkDeleteEvents()
view_pager.currentItem = view_pager.currentItem + 1 view_pager.currentItem = view_pager.currentItem + 1
} }
override fun goToDateTime(dateTime: DateTime) { override fun goToDateTime(dateTime: DateTime) {
checkDeleteEvents()
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!!.apply {
setAction(resources.getString(R.string.undo), undoDeletion)
setActionTextColor(Color.WHITE)
show()
}
}
} }

View File

@ -73,15 +73,15 @@ class DayEventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, va
private fun askConfirmDelete() { private fun askConfirmDelete() {
ConfirmationDialog(activity) { ConfirmationDialog(activity) {
actMode?.finish() actMode?.finish()
prepareForDeleting() deleteEvents()
} }
} }
private fun prepareForDeleting() { private fun deleteEvents() {
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?.prepareForDeleting(ids) listener?.deleteIds(ids)
} }
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
@ -164,6 +164,6 @@ class DayEventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, va
} }
interface ItemOperationsListener { interface ItemOperationsListener {
fun prepareForDeleting(ids: ArrayList<Int>) fun deleteIds(ids: ArrayList<Int>)
} }
} }

View File

@ -7,8 +7,9 @@ import android.support.v4.app.FragmentStatePagerAdapter
import android.util.SparseArray import android.util.SparseArray
import com.simplemobiletools.calendar.fragments.DayFragment import com.simplemobiletools.calendar.fragments.DayFragment
import com.simplemobiletools.calendar.helpers.DAY_CODE import com.simplemobiletools.calendar.helpers.DAY_CODE
import com.simplemobiletools.calendar.interfaces.NavigationListener
class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, private val mListener: DayFragment.DeleteListener) : class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, private val mListener: NavigationListener) :
FragmentStatePagerAdapter(fm) { FragmentStatePagerAdapter(fm) {
var fragments: SparseArray<DayFragment> = SparseArray(10) var fragments: SparseArray<DayFragment> = SparseArray(10)
@ -29,11 +30,8 @@ class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, p
return fragment return fragment
} }
fun deleteItems(pos: Int) { fun checkDayEvents(pos: Int) {
fragments.get(pos)?.deleteEvents() fragments.get(pos - 1)?.checkEvents()
} fragments.get(pos + 1)?.checkEvents()
fun undoDeletion(pos: Int) {
fragments.get(pos)?.undoDeletion()
} }
} }

View File

@ -12,6 +12,7 @@ import android.view.ViewGroup
import android.widget.DatePicker import android.widget.DatePicker
import android.widget.RelativeLayout import android.widget.RelativeLayout
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.DayActivity
import com.simplemobiletools.calendar.activities.EventActivity import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.adapters.DayEventsAdapter import com.simplemobiletools.calendar.adapters.DayEventsAdapter
@ -32,13 +33,11 @@ import kotlin.comparisons.compareBy
class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEventsListener, DayEventsAdapter.ItemOperationsListener { class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEventsListener, DayEventsAdapter.ItemOperationsListener {
private var mTextColor = 0 private var mTextColor = 0
private var mDayCode = "" private var mDayCode = ""
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.fragment_day, container, false) val view = inflater.inflate(R.layout.fragment_day, container, false)
@ -49,10 +48,11 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
mDayCode = arguments.getString(DAY_CODE) mDayCode = arguments.getString(DAY_CODE)
val day = Formatter.getDayTitle(activity.applicationContext, mDayCode) val day = Formatter.getDayTitle(activity.applicationContext, mDayCode)
mHolder.top_value.text = day mHolder.top_value.apply {
mHolder.top_value.setOnClickListener { pickDay() } text = day
mHolder.top_value.setTextColor(mConfig.textColor) setOnClickListener { pickDay() }
mToBeDeleted = ArrayList<Int>() setTextColor(mConfig.textColor)
}
setupButtons() setupButtons()
return view return view
@ -82,7 +82,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
} }
} }
fun setListener(listener: DeleteListener) { fun setListener(listener: NavigationListener) {
mListener = listener mListener = listener
} }
@ -110,19 +110,17 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
mListener?.goToDateTime(newDateTime) mListener?.goToDateTime(newDateTime)
} }
private fun checkEvents() { fun checkEvents() {
val startTS = Formatter.getDayStartTS(mDayCode) val startTS = Formatter.getDayStartTS(mDayCode)
val endTS = Formatter.getDayEndTS(mDayCode) val endTS = Formatter.getDayEndTS(mDayCode)
DBHelper(context, this).getEvents(startTS, endTS, this) DBHelper(context, this).getEvents(startTS, endTS, this)
} }
private fun updateEvents(events: MutableList<Event>) { private fun updateEvents(events: MutableList<Event>) {
mEvents = ArrayList(events)
val eventsToShow = getEventsToShow(events)
if (activity == null) if (activity == null)
return return
val eventsAdapter = DayEventsAdapter(activity as SimpleActivity, eventsToShow, this) { val eventsAdapter = DayEventsAdapter(activity as SimpleActivity, events, this) {
editEvent(it.id) editEvent(it.id)
} }
mHolder.day_events.apply { mHolder.day_events.apply {
@ -138,28 +136,11 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
} }
} }
private fun getEventsToShow(events: MutableList<Event>) = events.filter { !mToBeDeleted.contains(it.id) } override fun deleteIds(ids: ArrayList<Int>) {
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) })
override fun prepareForDeleting(ids: ArrayList<Int>) {
mToBeDeleted = ids
notifyDeletion()
}
private fun notifyDeletion() {
mListener?.notifyDeletion(mToBeDeleted.size)
updateEvents(mEvents!!)
}
fun deleteEvents() {
val eventIDs = Array(mToBeDeleted.size, { i -> (mToBeDeleted[i].toString()) })
DBHelper(activity.applicationContext, this).deleteEvents(eventIDs) DBHelper(activity.applicationContext, this).deleteEvents(eventIDs)
} }
fun undoDeletion() {
mToBeDeleted.clear()
updateEvents(mEvents!!)
}
override fun eventInserted(event: Event) { override fun eventInserted(event: Event) {
} }
@ -168,6 +149,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
override fun eventsDeleted(cnt: Int) { override fun eventsDeleted(cnt: Int) {
checkEvents() checkEvents()
(activity as DayActivity).recheckEvents()
} }
override fun gotEvents(events: MutableList<Event>) { override fun gotEvents(events: MutableList<Event>) {
@ -176,8 +158,4 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
updateEvents(sorted) updateEvents(sorted)
} }
} }
interface DeleteListener : NavigationListener {
fun notifyDeletion(cnt: Int)
}
} }