rewrite ManageEventTypes to the new adapter style

This commit is contained in:
tibbi 2017-11-13 17:13:21 +01:00
parent 64c4eb1483
commit 064211a71a
5 changed files with 65 additions and 138 deletions

View file

@ -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)
}
}
}

View file

@ -18,7 +18,7 @@ import kotlinx.android.synthetic.main.event_item_day_view.view.*
class DayEventsAdapter(activity: SimpleActivity, val events: List<Event>, 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<Event>, val li
}
}
private fun shareEvents() {
val eventIds = ArrayList<Int>(selectedPositions.size)
selectedPositions.forEach {
eventIds.add(events[it].id)
}
activity.shareEvents(eventIds.distinct())
finishActMode()
}
private fun askConfirmDelete() {
val eventIds = ArrayList<Int>(selectedPositions.size)
val timestamps = ArrayList<Int>(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<Event>, val li
event_item_description.setTextColor(textColor)
}
}
private fun shareEvents() {
val eventIds = ArrayList<Int>(selectedPositions.size)
selectedPositions.forEach {
eventIds.add(events[it].id)
}
activity.shareEvents(eventIds.distinct())
finishActMode()
}
private fun askConfirmDelete() {
val eventIds = ArrayList<Int>(selectedPositions.size)
val timestamps = ArrayList<Int>(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()
}
}
}

View file

@ -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<EventType>, val listener: DeleteEventTypesListener?, val itemClick: (EventType) -> Unit) :
RecyclerView.Adapter<EventTypeAdapter.ViewHolder>() {
val multiSelector = MultiSelector()
val views = ArrayList<View>()
class EventTypeAdapter(activity: SimpleActivity, val eventTypes: List<EventType>, val listener: DeleteEventTypesListener?, itemClick: (Any) -> Unit) :
MyAdapter(activity, itemClick) {
companion object {
var actMode: ActionMode? = null
val markedItems = HashSet<Int>()
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<EventType>(selections.size)
selections.forEach { eventTypes.add(mItems[it]) }
val eventTypes = ArrayList<EventType>(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<EventType>
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<EventType>
private fun deleteEventTypes(deleteEvents: Boolean, eventTypes: ArrayList<EventType>) {
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()
}
}

View file

@ -16,10 +16,11 @@ import java.util.*
abstract class MyAdapter(val activity: SimpleActivity, val itemClick: (Any) -> Unit) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
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<View>()
protected val itemViews = SparseArray<View>()
protected val selectedPositions = HashSet<Int>()
protected val multiSelector = MultiSelector()

View file

@ -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"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/type_color_label"