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
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v4.view.ViewPager
import android.view.View
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.MyDayPagerAdapter
import com.simplemobiletools.calendar.extensions.getNewEventTimestampFromCode
import com.simplemobiletools.calendar.fragments.DayFragment
import com.simplemobiletools.calendar.helpers.DAY_CODE
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.NEW_EVENT_START_TS
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.commons.extensions.updateTextColors
import kotlinx.android.synthetic.main.activity_day.*
import org.joda.time.DateTime
import java.util.*
class DayActivity : SimpleActivity(), DayFragment.DeleteListener, ViewPager.OnPageChangeListener {
class DayActivity : SimpleActivity(), NavigationListener, ViewPager.OnPageChangeListener {
private val PREFILLED_DAYS = 121
private var mDayCode = ""
private var mSnackbar: Snackbar? = null
private var mPagerDays: MutableList<String>? = null
private var mPagerPos = 0
@ -40,11 +36,6 @@ class DayActivity : SimpleActivity(), DayFragment.DeleteListener, ViewPager.OnPa
updateTextColors(day_coordinator)
}
override fun onPause() {
super.onPause()
checkDeleteEvents()
}
private fun fillViewPager(targetDay: String) {
getDays(targetDay)
val daysAdapter = MyDayPagerAdapter(supportFragmentManager, mPagerDays!!, this)
@ -71,26 +62,8 @@ class DayActivity : SimpleActivity(), DayFragment.DeleteListener, ViewPager.OnPa
}
}
private fun checkDeleteEvents() {
if (mSnackbar != null && mSnackbar!!.isShown) {
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)
}
fun recheckEvents() {
(view_pager.adapter as MyDayPagerAdapter).checkDayEvents(mPagerPos)
}
override fun onPageScrollStateChanged(state: Int) {
@ -102,32 +75,18 @@ class DayActivity : SimpleActivity(), DayFragment.DeleteListener, ViewPager.OnPa
}
override fun onPageSelected(position: Int) {
checkDeleteEvents()
mPagerPos = position
}
override fun goLeft() {
checkDeleteEvents()
view_pager.currentItem = view_pager.currentItem - 1
}
override fun goRight() {
checkDeleteEvents()
view_pager.currentItem = view_pager.currentItem + 1
}
override fun goToDateTime(dateTime: DateTime) {
checkDeleteEvents()
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() {
ConfirmationDialog(activity) {
actMode?.finish()
prepareForDeleting()
deleteEvents()
}
}
private fun prepareForDeleting() {
private fun deleteEvents() {
val selections = multiSelector.selectedPositions
val ids = ArrayList<Int>(selections.size)
selections.forEach { ids.add(mItems[it].id) }
listener?.prepareForDeleting(ids)
listener?.deleteIds(ids)
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
@ -164,6 +164,6 @@ class DayEventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, va
}
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 com.simplemobiletools.calendar.fragments.DayFragment
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) {
var fragments: SparseArray<DayFragment> = SparseArray(10)
@ -29,11 +30,8 @@ class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, p
return fragment
}
fun deleteItems(pos: Int) {
fragments.get(pos)?.deleteEvents()
}
fun undoDeletion(pos: Int) {
fragments.get(pos)?.undoDeletion()
fun checkDayEvents(pos: Int) {
fragments.get(pos - 1)?.checkEvents()
fragments.get(pos + 1)?.checkEvents()
}
}

View File

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