store selected event types to display

This commit is contained in:
tibbi 2017-02-12 13:46:55 +01:00
parent f84ff6c45c
commit bce6f85756
2 changed files with 36 additions and 6 deletions

View File

@ -12,16 +12,27 @@ import com.simplemobiletools.commons.extensions.setBackgroundWithStroke
import kotlinx.android.synthetic.main.filter_event_type_view.view.* import kotlinx.android.synthetic.main.filter_event_type_view.view.*
import java.util.* import java.util.*
class FilterEventTypeAdapter(val activity: SimpleActivity, val mItems: List<EventType>) : class FilterEventTypeAdapter(val activity: SimpleActivity, val mItems: List<EventType>, mDisplayEventTypes: Set<String>) :
RecyclerView.Adapter<FilterEventTypeAdapter.ViewHolder>() { RecyclerView.Adapter<FilterEventTypeAdapter.ViewHolder>() {
val views = ArrayList<View>() val views = ArrayList<View>()
companion object { companion object {
var textColor = 0 var textColor = 0
lateinit var displayEventTypes: Set<String>
lateinit var selectedItems: ArrayList<String>
} }
init { init {
textColor = activity.config.textColor textColor = activity.config.textColor
displayEventTypes = mDisplayEventTypes
selectedItems = ArrayList()
displayEventTypes.mapTo(selectedItems, { it })
}
fun getSelectedItemsSet(): HashSet<String> {
val selectedItemsSet = HashSet<String>(selectedItems.size)
selectedItems.mapTo(selectedItemsSet, { it })
return selectedItemsSet
} }
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
@ -37,11 +48,22 @@ class FilterEventTypeAdapter(val activity: SimpleActivity, val mItems: List<Even
class ViewHolder(val activity: SimpleActivity, view: View) : RecyclerView.ViewHolder(view) { class ViewHolder(val activity: SimpleActivity, view: View) : RecyclerView.ViewHolder(view) {
fun bindView(eventType: EventType): View { fun bindView(eventType: EventType): View {
val id = eventType.id.toString()
itemView.apply { itemView.apply {
filter_event_type_checkbox.setColors(activity.config.textColor, activity.config.primaryColor, activity.config.backgroundColor) filter_event_type_checkbox.setColors(activity.config.textColor, activity.config.primaryColor, activity.config.backgroundColor)
filter_event_type_checkbox.text = eventType.title filter_event_type_checkbox.text = eventType.title
filter_event_type_color.setBackgroundWithStroke(eventType.color, activity.config.backgroundColor) filter_event_type_color.setBackgroundWithStroke(eventType.color, activity.config.backgroundColor)
filter_event_type_holder.setOnClickListener { filter_event_type_checkbox.toggle() }
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)
} }
return itemView return itemView

View File

@ -5,6 +5,7 @@ import android.view.LayoutInflater
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.adapters.FilterEventTypeAdapter import com.simplemobiletools.calendar.adapters.FilterEventTypeAdapter
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.DBHelper import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.setupDialogStuff
@ -12,21 +13,28 @@ import kotlinx.android.synthetic.main.dialog_filter_event_types.view.*
import java.util.* import java.util.*
class FilterEventTypesDialog(val activity: SimpleActivity, val callback: () -> Unit) : AlertDialog.Builder(activity) { class FilterEventTypesDialog(val activity: SimpleActivity, val callback: () -> Unit) : AlertDialog.Builder(activity) {
val dialog: AlertDialog? var dialog: AlertDialog
var eventTypes = ArrayList<EventType>() var eventTypes = ArrayList<EventType>()
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_filter_event_types, null)
init { init {
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_filter_event_types, null)
DBHelper.newInstance(activity).getEventTypes { DBHelper.newInstance(activity).getEventTypes {
val displayEventTypes = activity.config.displayEventTypes
eventTypes = it eventTypes = it
view.filter_event_types_list.adapter = FilterEventTypeAdapter(activity, it) view.filter_event_types_list.adapter = FilterEventTypeAdapter(activity, it, displayEventTypes)
} }
dialog = AlertDialog.Builder(activity) dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, { dialogInterface, i -> confirmEventTypes() })
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.create().apply { .create().apply {
activity.setupDialogStuff(view, this, R.string.filter_events_by_type) activity.setupDialogStuff(view, this, R.string.filter_events_by_type)
} }
} }
private fun confirmEventTypes() {
val selectedItems = (view.filter_event_types_list.adapter as FilterEventTypeAdapter).getSelectedItemsSet()
activity.config.displayEventTypes = selectedItems
dialog.dismiss()
}
} }