rewriting FilterEventTypeAdapter

This commit is contained in:
tibbi 2017-11-13 18:08:58 +01:00
parent 28aab35b4c
commit 61f637c03e

View file

@ -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<EventType>, mDisplayEventTypes: Set<String>) :
class FilterEventTypeAdapter(val activity: SimpleActivity, val eventTypes: List<EventType>, val displayEventTypes: Set<String>) :
RecyclerView.Adapter<FilterEventTypeAdapter.ViewHolder>() {
val views = ArrayList<View>()
companion object {
var textColor = 0
lateinit var displayEventTypes: Set<String>
lateinit var selectedItems: ArrayList<String>
}
private val itemViews = SparseArray<View>()
private val selectedPositions = HashSet<Int>()
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<String> {
val selectedItemsSet = HashSet<String>(selectedItems.size)
selectedItems.mapTo(selectedItemsSet, { it })
val selectedItemsSet = HashSet<String>(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)
}
}
}