implement event deletion in the yearly view

This commit is contained in:
tibbi 2016-10-23 20:46:22 +02:00
parent 5b15389f4e
commit 9db5933255
4 changed files with 135 additions and 22 deletions

View File

@ -1,14 +1,15 @@
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.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import com.simplemobiletools.calendar.Constants import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.Formatter import com.simplemobiletools.calendar.Formatter
import com.simplemobiletools.calendar.NavigationListener
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.MyMonthPagerAdapter import com.simplemobiletools.calendar.adapters.MyMonthPagerAdapter
import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter
@ -20,11 +21,13 @@ import org.joda.time.DateTime
import org.joda.time.DateTimeZone import org.joda.time.DateTimeZone
import java.util.* import java.util.*
class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.ChangeViewListener { class MainActivity : SimpleActivity(), EventListFragment.DeleteListener, ChangeViewDialog.ChangeViewListener {
private val PREFILLED_MONTHS = 73 private val PREFILLED_MONTHS = 73
private val PREFILLED_YEARS = 21 private val PREFILLED_YEARS = 21
private var mIsMonthSelected = false private var mIsMonthSelected = false
private var mSnackbar: Snackbar? = null
private var mEventListFragment: EventListFragment? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -39,6 +42,11 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan
updateWidget() updateWidget()
} }
override fun onPause() {
super.onPause()
checkDeleteEvents()
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
mConfig.isFirstRun = false mConfig.isFirstRun = false
@ -46,7 +54,6 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu) menuInflater.inflate(R.menu.menu, menu)
return true return true
} }
@ -112,7 +119,7 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan
val codes = getMonths(targetDay) val codes = getMonths(targetDay)
val monthlyAdapter = MyMonthPagerAdapter(supportFragmentManager, codes, this) val monthlyAdapter = MyMonthPagerAdapter(supportFragmentManager, codes, this)
view_pager.apply { main_view_pager.apply {
clearOnPageChangeListeners() clearOnPageChangeListeners()
adapter = monthlyAdapter adapter = monthlyAdapter
currentItem = codes.size / 2 currentItem = codes.size / 2
@ -137,7 +144,7 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan
val years = getYears(targetYear) val years = getYears(targetYear)
val yearlyAdapter = MyYearPagerAdapter(supportFragmentManager, years, this) val yearlyAdapter = MyYearPagerAdapter(supportFragmentManager, years, this)
view_pager.apply { main_view_pager.apply {
adapter = yearlyAdapter adapter = yearlyAdapter
currentItem = years.size / 2 currentItem = years.size / 2
addOnPageChangeListener(object : ViewPager.OnPageChangeListener { addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
@ -166,24 +173,63 @@ class MainActivity : SimpleActivity(), NavigationListener, ChangeViewDialog.Chan
return years 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() { private fun fillEventsList() {
title = getString(R.string.app_launcher_name) title = getString(R.string.app_launcher_name)
view_pager.adapter = null main_view_pager.adapter = null
view_pager.visibility = View.GONE main_view_pager.visibility = View.GONE
calendar_event_list_holder.visibility = View.VISIBLE 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() { override fun goLeft() {
view_pager.currentItem = view_pager.currentItem - 1 main_view_pager.currentItem = main_view_pager.currentItem - 1
} }
override fun goRight() { override fun goRight() {
view_pager.currentItem = view_pager.currentItem + 1 main_view_pager.currentItem = main_view_pager.currentItem + 1
} }
override fun goToDateTime(dateTime: DateTime) { override fun goToDateTime(dateTime: DateTime) {
fillMonthlyViewPager(Formatter.getDayCodeFromDateTime(dateTime)) fillMonthlyViewPager(Formatter.getDayCodeFromDateTime(dateTime))
mIsMonthSelected = true 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()
}
} }

View File

@ -53,16 +53,12 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AdapterView.OnItemClick
val day = Formatter.getEventDate(activity.applicationContext, mDayCode) val day = Formatter.getEventDate(activity.applicationContext, mDayCode)
mHolder.top_value.text = day mHolder.top_value.text = day
mHolder.top_value.setOnClickListener { pickDay() } mHolder.top_value.setOnClickListener { pickDay() }
mToBeDeleted = ArrayList<Int>()
setupButtons() setupButtons()
return view return view
} }
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mToBeDeleted = ArrayList<Int>()
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
checkEvents() checkEvents()

View File

@ -1,17 +1,18 @@
package com.simplemobiletools.calendar.fragments package com.simplemobiletools.calendar.fragments
import android.app.Activity
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.view.* import android.view.*
import android.widget.AbsListView import android.widget.AbsListView
import android.widget.AdapterView import android.widget.AdapterView
import com.simplemobiletools.calendar.Constants import com.simplemobiletools.calendar.*
import com.simplemobiletools.calendar.DBHelper
import com.simplemobiletools.calendar.Formatter import com.simplemobiletools.calendar.Formatter
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.EventActivity import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.activities.MainActivity
import com.simplemobiletools.calendar.adapters.EventsListAdapter import com.simplemobiletools.calendar.adapters.EventsListAdapter
import com.simplemobiletools.calendar.extensions.updateWidget
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.models.ListEvent import com.simplemobiletools.calendar.models.ListEvent
import com.simplemobiletools.calendar.models.ListItem import com.simplemobiletools.calendar.models.ListItem
@ -21,28 +22,35 @@ import org.joda.time.DateTime
import java.util.* import java.util.*
import kotlin.comparisons.compareBy 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 private val EDIT_EVENT = 1
var mSelectedItemsCnt = 0 var mSelectedItemsCnt = 0
var mListItems: ArrayList<ListItem> = ArrayList() var mListItems: ArrayList<ListItem> = ArrayList()
lateinit var mToBeDeleted: MutableList<Int>
lateinit var mView: View lateinit var mView: View
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mView = inflater!!.inflate(R.layout.fragment_event_list, container, false) mView = inflater!!.inflate(R.layout.fragment_event_list, container, false)
mToBeDeleted = ArrayList<Int>()
return mView return mView
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
checkEvents()
}
private fun checkEvents() {
val fromTS = (DateTime().millis / 1000).toInt() val fromTS = (DateTime().millis / 1000).toInt()
val toTS = (DateTime().plusYears(1).millis / 1000).toInt() val toTS = (DateTime().plusYears(1).millis / 1000).toInt()
DBHelper(context).getEvents(fromTS, toTS, this) DBHelper(context).getEvents(fromTS, toTS, this)
} }
override fun gotEvents(events: MutableList<Event>) { override fun gotEvents(events: MutableList<Event>) {
mListItems = ArrayList<ListItem>(events.size) val filtered = getEventsToShow(events)
val sorted = events.sortedWith(compareBy({ it.startTS }, { it.endTS })) mListItems = ArrayList<ListItem>(filtered.size)
val sorted = filtered.sortedWith(compareBy({ it.startTS }, { it.endTS }))
var prevCode = "" var prevCode = ""
sorted.forEach { sorted.forEach {
val code = Formatter.getDayCodeFromTS(it.startTS) val code = Formatter.getDayCodeFromTS(it.startTS)
@ -64,7 +72,24 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On
} }
} }
private fun getEventsToShow(events: MutableList<Event>): List<Event> {
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) { override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
(activity as MainActivity).checkDeleteEvents()
editEvent((mListItems[position] as ListEvent).id) editEvent((mListItems[position] as ListEvent).id)
} }
@ -74,6 +99,33 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On
startActivityForResult(intent, EDIT_EVENT) 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 { override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true return true
} }
@ -81,6 +133,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
R.id.delete -> { R.id.delete -> {
prepareDeleteEvents()
mode.finish() mode.finish()
true true
} }
@ -108,4 +161,22 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On
mode.title = mSelectedItemsCnt.toString() mode.title = mSelectedItemsCnt.toString()
mode.invalidate() 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)
}
} }

View File

@ -7,7 +7,7 @@
android:layout_height="match_parent"> android:layout_height="match_parent">
<com.simplemobiletools.calendar.views.MyViewPager <com.simplemobiletools.calendar.views.MyViewPager
android:id="@+id/view_pager" android:id="@+id/main_view_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>