From 20a835f30403871541f39f53fe743c9dd3599199 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 11 Feb 2017 13:55:23 +0100 Subject: [PATCH] fill the list of event types --- .../activities/ManageEventTypesActivity.kt | 24 ++- .../calendar/adapters/EventTypeAdapter.kt | 139 ++++++++++++++++++ .../layout/activity_manage_event_types.xml | 3 +- app/src/main/res/layout/event_type_view.xml | 37 +++++ app/src/main/res/menu/cab_event_type.xml | 9 ++ 5 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt create mode 100644 app/src/main/res/layout/event_type_view.xml create mode 100644 app/src/main/res/menu/cab_event_type.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt index c68987217..fd2052907 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt @@ -2,12 +2,15 @@ package com.simplemobiletools.calendar.activities import android.os.Bundle import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.adapters.EventTypeAdapter import com.simplemobiletools.calendar.dialogs.EventTypeDialog import com.simplemobiletools.calendar.helpers.DBHelper +import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.commons.extensions.updateTextColors +import com.simplemobiletools.commons.views.RecyclerViewDivider import kotlinx.android.synthetic.main.activity_manage_event_types.* -class ManageEventTypesActivity : SimpleActivity() { +class ManageEventTypesActivity : SimpleActivity(), EventTypeAdapter.DeleteEventTypeListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_manage_event_types) @@ -28,7 +31,24 @@ class ManageEventTypesActivity : SimpleActivity() { private fun getEventTypes() { DBHelper.newInstance(applicationContext).getEventTypes { - + runOnUiThread { + gotEventTypes(it) + } } } + + private fun gotEventTypes(eventTypes: List) { + val eventTypesAdapter = EventTypeAdapter(this, eventTypes, this) { + + } + + manage_event_types_list.apply { + this@apply.adapter = eventTypesAdapter + addItemDecoration(RecyclerViewDivider(context)) + } + } + + override fun eventTypeDeleted() { + getEventTypes() + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt new file mode 100644 index 000000000..7314c2cf4 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt @@ -0,0 +1,139 @@ +package com.simplemobiletools.calendar.adapters + +import android.support.v7.view.ActionMode +import android.support.v7.widget.RecyclerView +import android.view.* +import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback +import com.bignerdranch.android.multiselector.MultiSelector +import com.bignerdranch.android.multiselector.SwappingHolder +import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.activities.SimpleActivity +import com.simplemobiletools.calendar.extensions.config +import com.simplemobiletools.calendar.models.EventType +import com.simplemobiletools.commons.dialogs.ConfirmationDialog +import com.simplemobiletools.commons.extensions.setBackgroundWithStroke +import kotlinx.android.synthetic.main.event_type_view.view.* +import java.util.* + +class EventTypeAdapter(val activity: SimpleActivity, val mItems: List, val listener: DeleteEventTypeListener?, val itemClick: (EventType) -> Unit) : + RecyclerView.Adapter() { + val multiSelector = MultiSelector() + val views = ArrayList() + + companion object { + var actMode: ActionMode? = null + val markedItems = HashSet() + var textColor = 0 + + fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) { + itemView.event_item_frame.isSelected = select + if (pos == -1) + return + + if (select) + markedItems.add(pos) + else + markedItems.remove(pos) + } + } + + init { + textColor = activity.config.textColor + } + + val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { + override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { + when (item.itemId) { + R.id.cab_delete -> askConfirmDelete() + else -> return false + } + return true + } + + override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean { + super.onCreateActionMode(actionMode, menu) + actMode = actionMode + activity.menuInflater.inflate(R.menu.cab_event_type, menu) + 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() + } + } + + private fun askConfirmDelete() { + ConfirmationDialog(activity) { + actMode?.finish() + deleteEventTypes() + } + } + + private fun deleteEventTypes() { + listener?.eventTypeDeleted() + } + + override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { + val view = LayoutInflater.from(parent?.context).inflate(R.layout.event_type_view, parent, false) + return ViewHolder(activity, view, itemClick) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + views.add(holder.bindView(multiSelectorMode, multiSelector, mItems[position], position)) + } + + override fun getItemCount() = mItems.size + + class ViewHolder(val activity: SimpleActivity, view: View, val itemClick: (EventType) -> (Unit)) : SwappingHolder(view, MultiSelector()) { + fun bindView(multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, eventType: EventType, pos: Int): View { + + itemView.apply { + event_type_title.text = eventType.title + event_type_color.setBackgroundWithStroke(eventType.color, activity.config.backgroundColor) + toggleItemSelection(this, markedItems.contains(pos), pos) + + event_type_title.setTextColor(textColor) + + setOnClickListener { viewClicked(multiSelector, eventType, 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, eventType: EventType, 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(eventType) + } + } + } + + interface DeleteEventTypeListener { + fun eventTypeDeleted() + } +} diff --git a/app/src/main/res/layout/activity_manage_event_types.xml b/app/src/main/res/layout/activity_manage_event_types.xml index 26611df0e..67f9209b3 100644 --- a/app/src/main/res/layout/activity_manage_event_types.xml +++ b/app/src/main/res/layout/activity_manage_event_types.xml @@ -9,9 +9,8 @@ + + + + + + + + + + diff --git a/app/src/main/res/menu/cab_event_type.xml b/app/src/main/res/menu/cab_event_type.xml new file mode 100644 index 000000000..af34568ba --- /dev/null +++ b/app/src/main/res/menu/cab_event_type.xml @@ -0,0 +1,9 @@ + + + +