From 064211a71a3e9869e83dc405c062ab1eb8748ce6 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 13 Nov 2017 17:13:21 +0100 Subject: [PATCH] rewrite ManageEventTypes to the new adapter style --- .../activities/ManageEventTypesActivity.kt | 2 +- .../calendar/adapters/DayEventsAdapter.kt | 56 +++---- .../calendar/adapters/EventTypeAdapter.kt | 139 ++++-------------- .../calendar/adapters/MyAdapter.kt | 3 +- app/src/main/res/layout/dialog_event_type.xml | 3 +- 5 files changed, 65 insertions(+), 138 deletions(-) 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 d705d8dfe..1b1d4343b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt @@ -34,7 +34,7 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteEventTypesListener { dbHelper.getEventTypes { runOnUiThread { manage_event_types_list.adapter = EventTypeAdapter(this, it, this) { - showEventTypeDialog(it) + showEventTypeDialog(it as EventType) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt index 31aba85e4..7aaff80bc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/DayEventsAdapter.kt @@ -18,7 +18,7 @@ import kotlinx.android.synthetic.main.event_item_day_view.view.* class DayEventsAdapter(activity: SimpleActivity, val events: List, val listener: DeleteEventsListener?, itemClick: (Any) -> Unit) : MyAdapter(activity, itemClick) { - private var allDayString = activity.resources.getString(R.string.all_day) + private var allDayString = resources.getString(R.string.all_day) private var replaceDescriptionWithLocation = config.replaceDescription override fun getActionMenuId() = R.menu.cab_day @@ -34,33 +34,6 @@ class DayEventsAdapter(activity: SimpleActivity, val events: List, val li } } - private fun shareEvents() { - val eventIds = ArrayList(selectedPositions.size) - selectedPositions.forEach { - eventIds.add(events[it].id) - } - activity.shareEvents(eventIds.distinct()) - finishActMode() - } - - private fun askConfirmDelete() { - val eventIds = ArrayList(selectedPositions.size) - val timestamps = ArrayList(selectedPositions.size) - selectedPositions.forEach { - eventIds.add(events[it].id) - timestamps.add(events[it].startTS) - } - - DeleteEventDialog(activity, eventIds) { - if (it) { - listener?.deleteItems(eventIds) - } else { - listener?.addEventRepeatException(eventIds, timestamps) - } - finishActMode() - } - } - override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent?.context).inflate(R.layout.event_item_day_view, parent, false) return createViewHolder(view) @@ -109,4 +82,31 @@ class DayEventsAdapter(activity: SimpleActivity, val events: List, val li event_item_description.setTextColor(textColor) } } + + private fun shareEvents() { + val eventIds = ArrayList(selectedPositions.size) + selectedPositions.forEach { + eventIds.add(events[it].id) + } + activity.shareEvents(eventIds.distinct()) + finishActMode() + } + + private fun askConfirmDelete() { + val eventIds = ArrayList(selectedPositions.size) + val timestamps = ArrayList(selectedPositions.size) + selectedPositions.forEach { + eventIds.add(events[it].id) + timestamps.add(events[it].startTS) + } + + DeleteEventDialog(activity, eventIds) { + if (it) { + listener?.deleteItems(eventIds) + } else { + listener?.addEventRepeatException(eventIds, timestamps) + } + finishActMode() + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt index 20f723828..e4a058408 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventTypeAdapter.kt @@ -1,14 +1,10 @@ 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 android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.activities.SimpleActivity -import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.interfaces.DeleteEventTypesListener import com.simplemobiletools.calendar.models.EventType @@ -18,63 +14,48 @@ import com.simplemobiletools.commons.models.RadioItem import kotlinx.android.synthetic.main.item_event_type.view.* import java.util.* -class EventTypeAdapter(val activity: SimpleActivity, val mItems: List, val listener: DeleteEventTypesListener?, val itemClick: (EventType) -> Unit) : - RecyclerView.Adapter() { - val multiSelector = MultiSelector() - val views = ArrayList() +class EventTypeAdapter(activity: SimpleActivity, val eventTypes: List, val listener: DeleteEventTypesListener?, itemClick: (Any) -> Unit) : + MyAdapter(activity, itemClick) { - companion object { - var actMode: ActionMode? = null - val markedItems = HashSet() - var textColor = 0 + override fun getActionMenuId() = R.menu.cab_event_type - fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) { - itemView.event_item_frame.isSelected = select - if (pos == -1) { - return - } + override fun markItemSelection(select: Boolean, pos: Int) { + itemViews[pos].event_item_frame.isSelected = select + } - if (select) { - markedItems.add(pos) - } else { - markedItems.remove(pos) - } + override fun actionItemPressed(id: Int) { + when (id) { + R.id.cab_delete -> askConfirmDelete() } } - init { - textColor = activity.config.textColor + override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyAdapter.ViewHolder { + val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_event_type, parent, false) + return createViewHolder(view) } - private 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 onBindViewHolder(holder: ViewHolder, position: Int) { + val eventType = eventTypes[position] + val view = holder.bindView(eventType) { + setupView(it, eventType) } + itemViews.put(position, view) + toggleItemSelection(selectedPositions.contains(position), position) + } - 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 getItemCount() = eventTypes.size - 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 setupView(view: View, eventType: EventType) { + view.apply { + event_type_title.text = eventType.getDisplayTitle() + event_type_color.setBackgroundWithStroke(eventType.color, config.backgroundColor) + event_type_title.setTextColor(textColor) } } private fun askConfirmDelete() { - val selections = multiSelector.selectedPositions - val eventTypes = ArrayList(selections.size) - selections.forEach { eventTypes.add(mItems[it]) } + val eventTypes = ArrayList(selectedPositions.size) + selectedPositions.forEach { eventTypes.add(this.eventTypes[it]) } if (activity.dbHelper.doEventTypesContainEvent(eventTypes)) { val MOVE_EVENTS = 0 @@ -85,7 +66,7 @@ class EventTypeAdapter(val activity: SimpleActivity, val mItems: List add(RadioItem(DELETE_EVENTS, res.getString(R.string.remove_affected_events))) } RadioGroupDialog(activity, items, -1) { - actMode?.finish() + finishActMode() deleteEventTypes(it == DELETE_EVENTS, eventTypes) } } else { @@ -95,62 +76,6 @@ class EventTypeAdapter(val activity: SimpleActivity, val mItems: List private fun deleteEventTypes(deleteEvents: Boolean, eventTypes: ArrayList) { listener?.deleteEventTypes(eventTypes, deleteEvents) - actMode?.finish() - } - - override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_event_type, 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.getDisplayTitle() - 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 - } - - private 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) - } - } + finishActMode() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyAdapter.kt index 0dcc3bfd1..52c111ea0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyAdapter.kt @@ -16,10 +16,11 @@ import java.util.* abstract class MyAdapter(val activity: SimpleActivity, val itemClick: (Any) -> Unit) : RecyclerView.Adapter() { protected val config = activity.config + protected val resources = activity.resources protected var actMode: ActionMode? = null protected var primaryColor = config.primaryColor protected var textColor = config.textColor - protected var itemViews = SparseArray() + protected val itemViews = SparseArray() protected val selectedPositions = HashSet() protected val multiSelector = MultiSelector() diff --git a/app/src/main/res/layout/dialog_event_type.xml b/app/src/main/res/layout/dialog_event_type.xml index ac3c3deed..db7db9d05 100644 --- a/app/src/main/res/layout/dialog_event_type.xml +++ b/app/src/main/res/layout/dialog_event_type.xml @@ -25,7 +25,8 @@ android:inputType="textCapSentences" android:maxLength="80" android:singleLine="true" - android:textCursorDrawable="@null"/> + android:textCursorDrawable="@null" + android:textSize="@dimen/normal_text_size"/>