make SelectContactsDialog properly handle returning a selected contact

This commit is contained in:
tibbi 2020-01-19 20:15:56 +01:00
parent 698ea25243
commit 6916876e4d

View file

@ -13,6 +13,7 @@ import kotlinx.android.synthetic.main.layout_select_contact.view.*
class SelectContactsDialog(val activity: SimpleActivity, initialContacts: ArrayList<Contact>, val allowSelectMultiple: Boolean, selectContacts: ArrayList<Contact>? = null, class SelectContactsDialog(val activity: SimpleActivity, initialContacts: ArrayList<Contact>, val allowSelectMultiple: Boolean, selectContacts: ArrayList<Contact>? = null,
val callback: (addedContacts: ArrayList<Contact>, removedContacts: ArrayList<Contact>) -> Unit) { val callback: (addedContacts: ArrayList<Contact>, removedContacts: ArrayList<Contact>) -> Unit) {
private var dialog: AlertDialog? = null
private var view = activity.layoutInflater.inflate(R.layout.layout_select_contact, null) private var view = activity.layoutInflater.inflate(R.layout.layout_select_contact, null)
private var initiallySelectedContacts = ArrayList<Contact>() private var initiallySelectedContacts = ArrayList<Contact>()
@ -28,15 +29,21 @@ class SelectContactsDialog(val activity: SimpleActivity, initialContacts: ArrayL
} }
activity.runOnUiThread { activity.runOnUiThread {
// if selecting multiple contacts is disabled, react on first contact click and dismiss the dialog
val contactClickCallback: ((Contact) -> Unit)? = if (allowSelectMultiple) null else { contact ->
callback(arrayListOf(contact), arrayListOf())
dialog!!.dismiss()
}
view.apply { view.apply {
select_contact_list.adapter = SelectContactsAdapter(activity, allContacts, initiallySelectedContacts, allowSelectMultiple, select_contact_list.adapter = SelectContactsAdapter(activity, allContacts, initiallySelectedContacts, allowSelectMultiple,
select_contact_list, select_contact_fastscroller) select_contact_list, select_contact_fastscroller, contactClickCallback)
select_contact_fastscroller.allowBubbleDisplay = activity.baseConfig.showInfoBubble select_contact_fastscroller.allowBubbleDisplay = activity.baseConfig.showInfoBubble
select_contact_fastscroller.setViews(select_contact_list) { select_contact_fastscroller.setViews(select_contact_list) {
select_contact_fastscroller.updateBubbleText(allContacts[it].getBubbleText()) select_contact_fastscroller.updateBubbleText(allContacts[it].getBubbleText())
} }
} }
}
val builder = AlertDialog.Builder(activity) val builder = AlertDialog.Builder(activity)
if (allowSelectMultiple) { if (allowSelectMultiple) {
@ -44,10 +51,11 @@ class SelectContactsDialog(val activity: SimpleActivity, initialContacts: ArrayL
} }
builder.setNegativeButton(R.string.cancel, null) builder.setNegativeButton(R.string.cancel, null)
builder.create().apply { dialog = builder.create().apply {
activity.setupDialogStuff(view, this) activity.setupDialogStuff(view, this)
} }
} }
}
private fun dialogConfirmed() { private fun dialogConfirmed() {
ensureBackgroundThread { ensureBackgroundThread {