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 2a4b4eba4..dff895f91 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt @@ -1,26 +1,25 @@ 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.view.View import com.simplemobiletools.calendar.Constants import com.simplemobiletools.calendar.Formatter -import com.simplemobiletools.calendar.NavigationListener import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.adapters.MyDayPagerAdapter +import com.simplemobiletools.calendar.fragments.DayFragment import com.simplemobiletools.calendar.models.Event import kotlinx.android.synthetic.main.activity_day.* import org.joda.time.DateTime import java.util.* -class DayActivity : SimpleActivity(), NavigationListener { - +class DayActivity : SimpleActivity(), DayFragment.DeleteListener { private val PREFILLED_DAYS = 61 private var mDayCode: String? = null private var mEvents: MutableList? = null private var mSnackbar: Snackbar? = null - private var mToBeDeleted: MutableList? = null private var mPagerDays: MutableList? = null companion object { @@ -39,8 +38,6 @@ class DayActivity : SimpleActivity(), NavigationListener { fillViewPager(mDayCode!!) day_fab.setOnClickListener { addNewEvent() } - - //mToBeDeleted = ArrayList() } override fun onPause() { @@ -123,7 +120,7 @@ class DayActivity : SimpleActivity(), NavigationListener { private fun undoDeletion() { if (mSnackbar != null) { - mToBeDeleted!!.clear() + //mToBeDeleted!!.clear() mSnackbar!!.dismiss() //updateEvents(mEvents) } @@ -162,4 +159,12 @@ class DayActivity : SimpleActivity(), NavigationListener { override fun goToDateTime(dateTime: 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() + } } 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 99da903ed..6d8ce4c86 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt @@ -5,10 +5,9 @@ import android.support.v4.app.Fragment import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentStatePagerAdapter import com.simplemobiletools.calendar.Constants -import com.simplemobiletools.calendar.NavigationListener import com.simplemobiletools.calendar.fragments.DayFragment -class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List, private val mListener: NavigationListener) : +class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List, private val mListener: DayFragment.DeleteListener) : FragmentStatePagerAdapter(fm) { override fun getCount(): Int { 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 1c92bf912..c80478894 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -21,19 +21,19 @@ import kotlinx.android.synthetic.main.day_fragment.view.* import kotlinx.android.synthetic.main.top_navigation.view.* import java.util.* -class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnItemClickListener, - AbsListView.MultiChoiceModeListener { +class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener { private val EDIT_EVENT = 1 private var mTextColor: Int = 0 private var mSelectedItemsCnt: Int = 0 private var mDayCode: String = "" private var mEvents: MutableList? = null - private var mListener: NavigationListener? = null + private var mListener: DeleteListener? = 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.day_fragment, container, false) @@ -53,6 +53,7 @@ class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnIte override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + mToBeDeleted = ArrayList() checkEvents() } @@ -76,7 +77,7 @@ class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnIte } } - fun setListener(listener: NavigationListener) { + fun setListener(listener: DeleteListener) { mListener = listener } @@ -128,13 +129,20 @@ class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnIte } private fun getEventsToShow(events: MutableList): List { - /*val cnt = events.size - for (i in cnt - 1 downTo 0) { - if (mToBeDeleted!!.contains(events[i].id)) { - events.removeAt(i) + return events.filter { !mToBeDeleted.contains(it.id) } + } + + 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) } - }*/ - return events + } + + mListener?.notifyDeletion(mToBeDeleted.size) + updateEvents(mEvents!!) } 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 { when (item.itemId) { R.id.delete -> { - //prepareDeleteEvents() + prepareDeleteEvents() mode.finish() return true } @@ -193,4 +201,8 @@ class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnIte override fun gotEvents(events: MutableList) { updateEvents(events) } + + interface DeleteListener : NavigationListener { + fun notifyDeletion(cnt: Int) + } }