diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt index 213cbe8f5..c48388001 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt @@ -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? = 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() - } - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt index 355420664..bbac22e76 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt @@ -73,15 +73,15 @@ class DayEventsAdapter(val activity: SimpleActivity, val mItems: List, va private fun askConfirmDelete() { ConfirmationDialog(activity) { actMode?.finish() - prepareForDeleting() + deleteEvents() } } - private fun prepareForDeleting() { + private fun deleteEvents() { val selections = multiSelector.selectedPositions val ids = ArrayList(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, va } interface ItemOperationsListener { - fun prepareForDeleting(ids: ArrayList) + fun deleteIds(ids: ArrayList) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt index eac363894..93f375aab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt @@ -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, private val mListener: DayFragment.DeleteListener) : +class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List, private val mListener: NavigationListener) : FragmentStatePagerAdapter(fm) { var fragments: SparseArray = SparseArray(10) @@ -29,11 +30,8 @@ class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List, 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() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt index b926feea7..6242fe09a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -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? = 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 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() + 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) { - 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) = events.filter { !mToBeDeleted.contains(it.id) } - - override fun prepareForDeleting(ids: ArrayList) { - 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) { + 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) { @@ -176,8 +158,4 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents updateEvents(sorted) } } - - interface DeleteListener : NavigationListener { - fun notifyDeletion(cnt: Int) - } }