From cb7d83a696b1a59f0ccefdb5de9fa857e06dc78b Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 23 Nov 2016 20:11:11 +0100 Subject: [PATCH] allow clicking and selecting events --- .../calendar/adapters/EventsAdapter.kt | 44 +++++++++++++++++-- .../calendar/fragments/DayFragment.kt | 8 +--- .../calendar/fragments/EventListFragment.kt | 2 +- .../menu/{menu_day_cab.xml => cab_day.xml} | 0 4 files changed, 43 insertions(+), 11 deletions(-) rename app/src/main/res/menu/{menu_day_cab.xml => cab_day.xml} (100%) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventsAdapter.kt index 497dfb76d..4d738dc75 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventsAdapter.kt @@ -23,7 +23,13 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List, val i val markedItems = HashSet() fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) { + if (pos == -1) + return + if (select) + markedItems.add(pos) + else + markedItems.remove(pos) } } @@ -37,16 +43,16 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List, val i override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean { super.onCreateActionMode(actionMode, menu) actMode = actionMode - activity.menuInflater.inflate(R.menu.menu_day_cab, menu) + activity.menuInflater.inflate(R.menu.cab_day, menu) return true } - override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean { - return true - } + override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu) = true override fun onDestroyActionMode(actionMode: ActionMode?) { super.onDestroyActionMode(actionMode) + views.forEach { toggleItemSelection(it, false) } + markedItems.clear() } } @@ -75,9 +81,39 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List, val i event_item_end.text = Formatter.getTime(event.endTS) event_item_end.visibility = View.VISIBLE } + + setOnClickListener { viewClicked(multiSelector, event, pos) } + setOnLongClickListener { + if (!multiSelector.isSelectable) { + activity.startSupportActionMode(multiSelectorCallback) + multiSelector.setSelected(this@ViewHolder, true) + actMode?.title = multiSelector.selectedPositions.size.toString() + toggleItemSelection(itemView, true, pos) + actMode?.invalidate() + } + true + } } return itemView } + + fun viewClicked(multiSelector: MultiSelector, event: Event, pos: Int) { + if (multiSelector.isSelectable) { + val isSelected = multiSelector.selectedPositions.contains(layoutPosition) + multiSelector.setSelected(this, !isSelected) + toggleItemSelection(itemView, !isSelected, pos) + + val selectedCnt = multiSelector.selectedPositions.size + if (selectedCnt == 0) { + actMode?.finish() + } else { + actMode?.title = selectedCnt.toString() + } + actMode?.invalidate() + } else { + itemClick(event) + } + } } } 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 3b40ad017..ba852ba8d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -128,7 +128,7 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoice return val eventsAdapter = EventsAdapter(activity as SimpleActivity, eventsToShow) { - + editEvent(it.id) } mHolder.day_events.apply { this@apply.adapter = eventsAdapter @@ -195,7 +195,7 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoice override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { val inflater = mode.menuInflater - inflater.inflate(R.menu.menu_day_cab, menu) + inflater.inflate(R.menu.cab_day, menu) return true } @@ -214,10 +214,6 @@ class DayFragment : Fragment(), DBHelper.EventsListener, AbsListView.MultiChoice mode.invalidate() } - /*override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) { - editEvent(getEventsToShow(mEvents!!)[position].id) - }*/ - override fun eventInserted(event: Event) { } 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 5048a16a4..582df2bcc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt @@ -163,7 +163,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.On override fun onCreateActionMode(mode: ActionMode, menu: Menu?): Boolean { val inflater = mode.menuInflater - inflater.inflate(R.menu.menu_day_cab, menu) + inflater.inflate(R.menu.cab_day, menu) return true } diff --git a/app/src/main/res/menu/menu_day_cab.xml b/app/src/main/res/menu/cab_day.xml similarity index 100% rename from app/src/main/res/menu/menu_day_cab.xml rename to app/src/main/res/menu/cab_day.xml