From bce6f8575686c60677a8ea431f74cae45fd7e23e Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 12 Feb 2017 13:46:55 +0100 Subject: [PATCH] store selected event types to display --- .../adapters/FilterEventTypeAdapter.kt | 26 +++++++++++++++++-- .../dialogs/FilterEventTypesDialog.kt | 16 +++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) 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 f3acb62e4..99617adc5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/FilterEventTypeAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/FilterEventTypeAdapter.kt @@ -12,16 +12,27 @@ import com.simplemobiletools.commons.extensions.setBackgroundWithStroke import kotlinx.android.synthetic.main.filter_event_type_view.view.* import java.util.* -class FilterEventTypeAdapter(val activity: SimpleActivity, val mItems: List) : +class FilterEventTypeAdapter(val activity: SimpleActivity, val mItems: List, mDisplayEventTypes: Set) : RecyclerView.Adapter() { val views = ArrayList() companion object { var textColor = 0 + lateinit var displayEventTypes: Set + lateinit var selectedItems: ArrayList } init { textColor = activity.config.textColor + displayEventTypes = mDisplayEventTypes + selectedItems = ArrayList() + displayEventTypes.mapTo(selectedItems, { it }) + } + + fun getSelectedItemsSet(): HashSet { + val selectedItemsSet = HashSet(selectedItems.size) + selectedItems.mapTo(selectedItemsSet, { it }) + return selectedItemsSet } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { @@ -37,11 +48,22 @@ class FilterEventTypeAdapter(val activity: SimpleActivity, val mItems: List Unit) : AlertDialog.Builder(activity) { - val dialog: AlertDialog? + var dialog: AlertDialog var eventTypes = ArrayList() + val view = LayoutInflater.from(activity).inflate(R.layout.dialog_filter_event_types, null) init { - val view = LayoutInflater.from(activity).inflate(R.layout.dialog_filter_event_types, null) DBHelper.newInstance(activity).getEventTypes { + val displayEventTypes = activity.config.displayEventTypes 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) - .setPositiveButton(R.string.ok, null) + .setPositiveButton(R.string.ok, { dialogInterface, i -> confirmEventTypes() }) .setNegativeButton(R.string.cancel, null) .create().apply { 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() + } }