diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index 12abd2b6f..3f1dc06a7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -1,14 +1,15 @@ 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.Menu import android.view.MenuItem 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.MyMonthPagerAdapter import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter @@ -20,11 +21,13 @@ import org.joda.time.DateTime import org.joda.time.DateTimeZone import java.util.* -class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.ChangeViewListener { +class MainActivity : SimpleActivity(), EventListFragment.DeleteListener, ChangeViewDialog.ChangeViewListener { private val PREFILLED_MONTHS = 73 private val PREFILLED_YEARS = 21 private var mIsMonthSelected = false + private var mSnackbar: Snackbar? = null + private var mEventListFragment: EventListFragment? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -39,6 +42,11 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan updateWidget() } + override fun onPause() { + super.onPause() + checkDeleteEvents() + } + override fun onDestroy() { super.onDestroy() mConfig.isFirstRun = false @@ -46,7 +54,6 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu, menu) - return true } @@ -112,7 +119,7 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan val codes = getMonths(targetDay) val monthlyAdapter = MyMonthPagerAdapter(supportFragmentManager, codes, this) - view_pager.apply { + main_view_pager.apply { clearOnPageChangeListeners() adapter = monthlyAdapter currentItem = codes.size / 2 @@ -137,7 +144,7 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan val years = getYears(targetYear) val yearlyAdapter = MyYearPagerAdapter(supportFragmentManager, years, this) - view_pager.apply { + main_view_pager.apply { adapter = yearlyAdapter currentItem = years.size / 2 addOnPageChangeListener(object : ViewPager.OnPageChangeListener { @@ -166,24 +173,63 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan return years } + fun checkDeleteEvents() { + if (mSnackbar != null && mSnackbar!!.isShown) { + deleteEvents() + } else { + undoDeletion() + } + } + + private fun deleteEvents() { + mSnackbar!!.dismiss() + mEventListFragment?.deleteEvents() + } + + private val undoDeletion = View.OnClickListener { undoDeletion() } + + private fun undoDeletion() { + if (mSnackbar != null) { + mSnackbar!!.dismiss() + mEventListFragment?.undoDeletion() + } + } + private fun fillEventsList() { title = getString(R.string.app_launcher_name) - view_pager.adapter = null - view_pager.visibility = View.GONE + main_view_pager.adapter = null + main_view_pager.visibility = View.GONE calendar_event_list_holder.visibility = View.VISIBLE - supportFragmentManager.beginTransaction().replace(R.id.calendar_event_list_holder, EventListFragment(), "").commit() + + if (mEventListFragment == null) + mEventListFragment = EventListFragment() + supportFragmentManager.beginTransaction().replace(R.id.calendar_event_list_holder, mEventListFragment, "").commit() } override fun goLeft() { - view_pager.currentItem = view_pager.currentItem - 1 + main_view_pager.currentItem = main_view_pager.currentItem - 1 } override fun goRight() { - view_pager.currentItem = view_pager.currentItem + 1 + main_view_pager.currentItem = main_view_pager.currentItem + 1 } override fun goToDateTime(dateTime: DateTime) { fillMonthlyViewPager(Formatter.getDayCodeFromDateTime(dateTime)) mIsMonthSelected = true } + + override fun notifyDeletion(cnt: Int) { + val msg = resources.getQuantityString(R.plurals.events_deleted, cnt, cnt) + mSnackbar = Snackbar.make(calendar_coordinator, msg, Snackbar.LENGTH_LONG) + mSnackbar!!.setCallback(object: Snackbar.Callback() { + override fun onDismissed(snackbar: Snackbar?, event: Int) { + super.onDismissed(snackbar, event) + mEventListFragment?.deleteEvents() + } + }) + mSnackbar!!.setAction(resources.getString(R.string.undo), undoDeletion) + mSnackbar!!.setActionTextColor(Color.WHITE) + mSnackbar!!.show() + } } 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 31ccfcbde..dceb05f94 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -53,16 +53,12 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AdapterView.OnItemClick val day = Formatter.getEventDate(activity.applicationContext, mDayCode) mHolder.top_value.text = day mHolder.top_value.setOnClickListener { pickDay() } + mToBeDeleted = ArrayList() setupButtons() return view } - override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - mToBeDeleted = ArrayList() - } - override fun onResume() { super.onResume() checkEvents() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt index 0cfc60fb9..fb1bbd638 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt @@ -1,17 +1,18 @@ package com.simplemobiletools.calendar.fragments +import android.app.Activity import android.content.Intent import android.os.Bundle import android.support.v4.app.Fragment import android.view.* import android.widget.AbsListView import android.widget.AdapterView -import com.simplemobiletools.calendar.Constants -import com.simplemobiletools.calendar.DBHelper +import com.simplemobiletools.calendar.* import com.simplemobiletools.calendar.Formatter -import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.activities.EventActivity +import com.simplemobiletools.calendar.activities.MainActivity import com.simplemobiletools.calendar.adapters.EventsListAdapter +import com.simplemobiletools.calendar.extensions.updateWidget import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.ListEvent import com.simplemobiletools.calendar.models.ListItem @@ -21,28 +22,35 @@ import org.joda.time.DateTime import java.util.* import kotlin.comparisons.compareBy -class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener { +class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener, DBHelper.EventsListener { private val EDIT_EVENT = 1 var mSelectedItemsCnt = 0 var mListItems: ArrayList = ArrayList() + lateinit var mToBeDeleted: MutableList lateinit var mView: View override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { mView = inflater!!.inflate(R.layout.fragment_event_list, container, false) + mToBeDeleted = ArrayList() return mView } override fun onResume() { super.onResume() + checkEvents() + } + + private fun checkEvents() { val fromTS = (DateTime().millis / 1000).toInt() val toTS = (DateTime().plusYears(1).millis / 1000).toInt() DBHelper(context).getEvents(fromTS, toTS, this) } override fun gotEvents(events: MutableList) { - mListItems = ArrayList(events.size) - val sorted = events.sortedWith(compareBy({ it.startTS }, { it.endTS })) + val filtered = getEventsToShow(events) + mListItems = ArrayList(filtered.size) + val sorted = filtered.sortedWith(compareBy({ it.startTS }, { it.endTS })) var prevCode = "" sorted.forEach { val code = Formatter.getDayCodeFromTS(it.startTS) @@ -64,7 +72,24 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On } } + private fun getEventsToShow(events: MutableList): List { + return events.filter { !mToBeDeleted.contains(it.id) } + } + + private fun prepareDeleteEvents() { + val checked = mView.calendar_events_list.checkedItemPositions + for (i in mListItems.indices) { + if (checked.get(i)) { + val event = mListItems[i] + mToBeDeleted.add((event as ListEvent).id) + } + } + + notifyDeletion() + } + override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { + (activity as MainActivity).checkDeleteEvents() editEvent((mListItems[position] as ListEvent).id) } @@ -74,6 +99,33 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On startActivityForResult(intent, EDIT_EVENT) } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (requestCode == EDIT_EVENT && resultCode == Activity.RESULT_OK && data != null) { + val deletedId = data.getIntExtra(DayFragment.DELETED_ID, -1) + if (deletedId != -1) { + mToBeDeleted.clear() + mToBeDeleted.add(deletedId) + notifyDeletion() + } + } + } + + private fun notifyDeletion() { + (activity as MainActivity).notifyDeletion(mToBeDeleted.size) + checkEvents() + } + + fun deleteEvents() { + val eventIDs = Array(mToBeDeleted.size, { i -> (mToBeDeleted[i].toString()) }) + DBHelper(activity.applicationContext, this).deleteEvents(eventIDs) + mToBeDeleted.clear() + } + + fun undoDeletion() { + mToBeDeleted.clear() + checkEvents() + } + override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean { return true } @@ -81,6 +133,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { return when (item.itemId) { R.id.delete -> { + prepareDeleteEvents() mode.finish() true } @@ -108,4 +161,22 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On mode.title = mSelectedItemsCnt.toString() mode.invalidate() } + + override fun eventInserted(event: Event) { + checkEvents() + context.updateWidget() + } + + override fun eventUpdated(event: Event) { + checkEvents() + context.updateWidget() + } + + override fun eventsDeleted(cnt: Int) { + context.updateWidget() + } + + interface DeleteListener : NavigationListener { + fun notifyDeletion(cnt: Int) + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index df189c6bd..b2209010f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,7 +7,7 @@ android:layout_height="match_parent">