store selected event types to display
This commit is contained in:
parent
f84ff6c45c
commit
bce6f85756
|
@ -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<EventType>) :
|
||||
class FilterEventTypeAdapter(val activity: SimpleActivity, val mItems: List<EventType>, mDisplayEventTypes: 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>
|
||||
}
|
||||
|
||||
init {
|
||||
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 {
|
||||
|
@ -37,11 +48,22 @@ class FilterEventTypeAdapter(val activity: SimpleActivity, val mItems: List<Even
|
|||
|
||||
class ViewHolder(val activity: SimpleActivity, view: View) : 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.title
|
||||
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
|
||||
|
|
|
@ -5,6 +5,7 @@ import android.view.LayoutInflater
|
|||
import com.simplemobiletools.calendar.R
|
||||
import com.simplemobiletools.calendar.activities.SimpleActivity
|
||||
import com.simplemobiletools.calendar.adapters.FilterEventTypeAdapter
|
||||
import com.simplemobiletools.calendar.extensions.config
|
||||
import com.simplemobiletools.calendar.helpers.DBHelper
|
||||
import com.simplemobiletools.calendar.models.EventType
|
||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||
|
@ -12,21 +13,28 @@ import kotlinx.android.synthetic.main.dialog_filter_event_types.view.*
|
|||
import java.util.*
|
||||
|
||||
class FilterEventTypesDialog(val activity: SimpleActivity, val callback: () -> Unit) : AlertDialog.Builder(activity) {
|
||||
val dialog: AlertDialog?
|
||||
var dialog: AlertDialog
|
||||
var eventTypes = ArrayList<EventType>()
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue