diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/FilterEventTypeAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/FilterEventTypeAdapter.kt index 3202aea37..e09891362 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/FilterEventTypeAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/FilterEventTypeAdapter.kt @@ -1,7 +1,7 @@ package com.simplemobiletools.calendar.adapters import android.support.v7.widget.RecyclerView -import android.view.LayoutInflater +import android.util.SparseArray import android.view.View import android.view.ViewGroup import com.simplemobiletools.calendar.R @@ -9,64 +9,76 @@ import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.commons.extensions.setBackgroundWithStroke +import com.simplemobiletools.commons.interfaces.MyAdapterListener import kotlinx.android.synthetic.main.filter_event_type_view.view.* import java.util.* -class FilterEventTypeAdapter(val activity: SimpleActivity, val mItems: List, mDisplayEventTypes: Set) : +class FilterEventTypeAdapter(val activity: SimpleActivity, val eventTypes: List, val displayEventTypes: Set) : RecyclerView.Adapter() { - val views = ArrayList() - - companion object { - var textColor = 0 - lateinit var displayEventTypes: Set - lateinit var selectedItems: ArrayList - } + private val itemViews = SparseArray() + private val selectedPositions = HashSet() init { - textColor = activity.config.textColor - displayEventTypes = mDisplayEventTypes - selectedItems = ArrayList() - displayEventTypes.mapTo(selectedItems, { it }) + eventTypes.forEachIndexed { index, eventType -> + if (displayEventTypes.contains(eventType.id.toString())) { + selectedPositions.add(index) + } + } + } + + private fun toggleItemSelection(select: Boolean, pos: Int) { + if (select) { + if (itemViews[pos] != null) { + selectedPositions.add(pos) + } + } else { + selectedPositions.remove(pos) + } + + itemViews[pos]?.filter_event_type_checkbox?.isChecked = select + } + + private val adapterListener = object : MyAdapterListener { + override fun toggleItemSelectionAdapter(select: Boolean, position: Int) { + toggleItemSelection(select, position) + } + + override fun getSelectedPositions() = selectedPositions } fun getSelectedItemsSet(): HashSet { - val selectedItemsSet = HashSet(selectedItems.size) - selectedItems.mapTo(selectedItemsSet, { it }) + val selectedItemsSet = HashSet(selectedPositions.size) + selectedPositions.forEach { selectedItemsSet.add(eventTypes[it].id.toString()) } return selectedItemsSet } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent?.context).inflate(R.layout.filter_event_type_view, parent, false) - return ViewHolder(activity, view) + val view = activity.layoutInflater.inflate(R.layout.filter_event_type_view, parent, false) + return ViewHolder(view, adapterListener, activity) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - views.add(holder.bindView(mItems[position])) + val eventType = eventTypes[position] + itemViews.put(position, holder.bindView(eventType)) + toggleItemSelection(selectedPositions.contains(position), position) } - override fun getItemCount() = mItems.size + override fun getItemCount() = eventTypes.size - class ViewHolder(val activity: SimpleActivity, view: View) : RecyclerView.ViewHolder(view) { + class ViewHolder(view: View, val adapterListener: MyAdapterListener, val activity: SimpleActivity) : RecyclerView.ViewHolder(view) { fun bindView(eventType: EventType): View { - val id = eventType.id.toString() itemView.apply { filter_event_type_checkbox.setColors(activity.config.textColor, activity.config.primaryColor, activity.config.backgroundColor) filter_event_type_checkbox.text = eventType.getDisplayTitle() filter_event_type_color.setBackgroundWithStroke(eventType.color, activity.config.backgroundColor) - - filter_event_type_holder.setOnClickListener { - filter_event_type_checkbox.toggle() - if (filter_event_type_checkbox.isChecked) { - selectedItems.add(id) - } else { - selectedItems.remove(id) - } - } - - filter_event_type_checkbox.isChecked = displayEventTypes.contains(id) + filter_event_type_holder.setOnClickListener { viewClicked(!filter_event_type_checkbox.isChecked) } } return itemView } + + private fun viewClicked(select: Boolean) { + adapterListener.toggleItemSelectionAdapter(select, adapterPosition) + } } }