mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-19 21:28:26 +00:00
Add SelectableItem<>
This commit is contained in:
parent
8ef6ec2125
commit
4f2ef7f2af
@ -27,13 +27,8 @@ class BlockedContactsActivity: PassphraseRequiredActionBarActivity() {
|
||||
AlertDialog.Builder(this)
|
||||
.setTitle(title)
|
||||
.setMessage(message)
|
||||
.setPositiveButton(R.string.continue_2) { d, _ ->
|
||||
viewModel.unblock()
|
||||
d.dismiss()
|
||||
}
|
||||
.setNegativeButton(R.string.cancel) { d, _ ->
|
||||
d.dismiss()
|
||||
}
|
||||
.setPositiveButton(R.string.continue_2) { _, _ -> viewModel.unblock() }
|
||||
.setNegativeButton(R.string.cancel) { _, _ -> }
|
||||
.show()
|
||||
}
|
||||
|
||||
@ -46,7 +41,7 @@ class BlockedContactsActivity: PassphraseRequiredActionBarActivity() {
|
||||
|
||||
viewModel.subscribe(this)
|
||||
.observe(this) { state ->
|
||||
adapter.submitList(state.blockedContacts)
|
||||
adapter.submitList(state.items)
|
||||
binding.emptyStateMessageTextView.isVisible = state.emptyStateMessageTextViewVisible
|
||||
binding.nonEmptyStateGroup.isVisible = state.nonEmptyStateGroupVisible
|
||||
binding.unblockButton.isEnabled = state.unblockButtonEnabled
|
||||
|
@ -10,30 +10,26 @@ import network.loki.messenger.R
|
||||
import network.loki.messenger.databinding.BlockedContactLayoutBinding
|
||||
import org.session.libsession.utilities.recipients.Recipient
|
||||
import org.thoughtcrime.securesms.mms.GlideApp
|
||||
import org.thoughtcrime.securesms.util.adapter.SelectableItem
|
||||
|
||||
class BlockedContactsAdapter(val viewModel: BlockedContactsViewModel) : ListAdapter<Recipient,BlockedContactsAdapter.ViewHolder>(RecipientDiffer()) {
|
||||
typealias SelectableRecipient = SelectableItem<Recipient>
|
||||
|
||||
class RecipientDiffer: DiffUtil.ItemCallback<Recipient>() {
|
||||
override fun areItemsTheSame(oldItem: Recipient, newItem: Recipient) = oldItem === newItem
|
||||
override fun areContentsTheSame(oldItem: Recipient, newItem: Recipient) = oldItem == newItem
|
||||
class BlockedContactsAdapter(val viewModel: BlockedContactsViewModel) : ListAdapter<SelectableRecipient,BlockedContactsAdapter.ViewHolder>(RecipientDiffer()) {
|
||||
|
||||
class RecipientDiffer: DiffUtil.ItemCallback<SelectableRecipient>() {
|
||||
override fun areItemsTheSame(old: SelectableRecipient, new: SelectableRecipient) = old.item.address == new.item.address
|
||||
override fun areContentsTheSame(old: SelectableRecipient, new: SelectableRecipient) = old.isSelected == new.isSelected
|
||||
override fun getChangePayload(old: SelectableRecipient, new: SelectableRecipient) = new.isSelected
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.blocked_contact_layout, parent, false)
|
||||
return ViewHolder(itemView)
|
||||
}
|
||||
|
||||
private fun toggleSelection(recipient: Recipient, isSelected: Boolean, position: Int) {
|
||||
viewModel.select(recipient, isSelected)
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
|
||||
LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.blocked_contact_layout, parent, false)
|
||||
.let(::ViewHolder)
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val recipient = getItem(position)
|
||||
val isSelected = recipient in viewModel.state.selectedItems
|
||||
holder.bind(recipient, isSelected) {
|
||||
toggleSelection(recipient, !isSelected, position)
|
||||
}
|
||||
val selectable = getItem(position)
|
||||
holder.bind(selectable, viewModel::toggle)
|
||||
}
|
||||
|
||||
override fun onViewRecycled(holder: ViewHolder) {
|
||||
@ -46,15 +42,14 @@ class BlockedContactsAdapter(val viewModel: BlockedContactsViewModel) : ListAdap
|
||||
val glide = GlideApp.with(itemView)
|
||||
val binding = BlockedContactLayoutBinding.bind(itemView)
|
||||
|
||||
fun bind(recipient: Recipient, isSelected: Boolean, toggleSelection: () -> Unit) {
|
||||
binding.recipientName.text = recipient.name
|
||||
fun bind(selectable: SelectableRecipient, toggle: (SelectableRecipient) -> Unit) {
|
||||
binding.recipientName.text = selectable.item.name
|
||||
with (binding.profilePictureView.root) {
|
||||
glide = this@ViewHolder.glide
|
||||
update(recipient)
|
||||
update(selectable.item)
|
||||
}
|
||||
binding.root.setOnClickListener { toggleSelection() }
|
||||
binding.selectButton.isSelected = isSelected
|
||||
binding.root.setOnClickListener { toggle(selectable) }
|
||||
binding.selectButton.isSelected = selectable.isSelected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import network.loki.messenger.R
|
||||
import org.session.libsession.utilities.recipients.Recipient
|
||||
import org.thoughtcrime.securesms.database.DatabaseContentProviders
|
||||
import org.thoughtcrime.securesms.database.Storage
|
||||
import org.thoughtcrime.securesms.util.adapter.SelectableItem
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@ -101,10 +102,19 @@ class BlockedContactsViewModel @Inject constructor(private val storage: Storage)
|
||||
return context.getString(R.string.Unblock_dialog__message, stringBuilder.toString())
|
||||
}
|
||||
|
||||
fun toggle(selectable: SelectableItem<Recipient>) {
|
||||
_state.value = state.run {
|
||||
if (selectable.isSelected) copy(selectedItems = selectedItems - selectable.item)
|
||||
else copy(selectedItems = selectedItems + selectable.item)
|
||||
}
|
||||
}
|
||||
|
||||
data class BlockedContactsViewState(
|
||||
val blockedContacts: List<Recipient>,
|
||||
val selectedItems: Set<Recipient>
|
||||
) {
|
||||
val items = blockedContacts.map { SelectableItem(it, it in selectedItems) }
|
||||
|
||||
val isEmpty get() = blockedContacts.isEmpty()
|
||||
val unblockButtonEnabled get() = selectedItems.isNotEmpty()
|
||||
val emptyStateMessageTextViewVisible get() = blockedContacts.isEmpty()
|
||||
|
@ -0,0 +1,3 @@
|
||||
package org.thoughtcrime.securesms.util.adapter
|
||||
|
||||
data class SelectableItem<T>(val item: T, val isSelected: Boolean)
|
Loading…
x
Reference in New Issue
Block a user