forked from upstream-mirrors/quik
Set click listeners when creating viewHolder instead of binding
This commit is contained in:
@@ -21,14 +21,11 @@ package com.moez.QKSMS.feature.compose
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.bumptech.glide.Glide
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.base.QkAdapter
|
||||
import com.moez.QKSMS.common.base.QkViewHolder
|
||||
import com.moez.QKSMS.model.Attachment
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.attachment_list_item.view.*
|
||||
@@ -40,29 +37,23 @@ class AttachmentAdapter @Inject constructor(
|
||||
|
||||
val attachmentDeleted: Subject<Attachment> = PublishSubject.create()
|
||||
|
||||
private val disposables = CompositeDisposable()
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
||||
val view = LayoutInflater.from(parent.context).inflate(R.layout.attachment_list_item, parent, false)
|
||||
|
||||
view.thumbnailBounds.clipToOutline = true
|
||||
|
||||
return QkViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
|
||||
super.onDetachedFromRecyclerView(recyclerView)
|
||||
disposables.clear()
|
||||
return QkViewHolder(view).apply {
|
||||
view.setOnClickListener {
|
||||
val attachment = getItem(adapterPosition)
|
||||
attachmentDeleted.onNext(attachment)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val attachment = getItem(position)
|
||||
val view = holder.itemView
|
||||
|
||||
view.clicks().subscribe {
|
||||
attachmentDeleted.onNext(attachment)
|
||||
}
|
||||
|
||||
Glide.with(context).load(attachment.getUri()).into(view.thumbnail)
|
||||
}
|
||||
|
||||
|
||||
@@ -84,7 +84,13 @@ class ChipsAdapter @Inject constructor(private val context: Context) : QkAdapter
|
||||
|
||||
else -> {
|
||||
val inflater = LayoutInflater.from(parent.context)
|
||||
QkViewHolder(inflater.inflate(R.layout.contact_chip, parent, false))
|
||||
val view = inflater.inflate(R.layout.contact_chip, parent, false)
|
||||
QkViewHolder(view).apply {
|
||||
view.setOnClickListener {
|
||||
val contact = getItem(adapterPosition)
|
||||
showDetailedChip(view.context, contact)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,8 +109,6 @@ class ChipsAdapter @Inject constructor(private val context: Context) : QkAdapter
|
||||
} else {
|
||||
contact.numbers.firstOrNull { it.address.isNotBlank() }?.address ?: ""
|
||||
}
|
||||
|
||||
view.setOnClickListener { showDetailedChip(view.context, contact) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ package com.moez.QKSMS.feature.compose
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.base.QkAdapter
|
||||
import com.moez.QKSMS.common.base.QkViewHolder
|
||||
@@ -39,15 +38,18 @@ class ContactAdapter @Inject constructor() : QkAdapter<Contact>() {
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
||||
val layoutInflater = LayoutInflater.from(parent.context)
|
||||
val view = layoutInflater.inflate(R.layout.contact_list_item, parent, false)
|
||||
return QkViewHolder(view)
|
||||
return QkViewHolder(view).apply {
|
||||
view.primary.setOnClickListener {
|
||||
val contact = getItem(adapterPosition)
|
||||
contactSelected.onNext(copyContact(contact, 0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val contact = getItem(position)
|
||||
val view = holder.itemView
|
||||
|
||||
view.primary.clicks().subscribe { contactSelected.onNext(copyContact(contact, 0)) }
|
||||
|
||||
view.avatar.setContact(contact)
|
||||
view.name.text = contact.name
|
||||
view.name.setVisible(view.name.text.isNotEmpty())
|
||||
@@ -58,7 +60,7 @@ class ContactAdapter @Inject constructor() : QkAdapter<Contact>() {
|
||||
contact.numbers.forEachIndexed { index, number ->
|
||||
if (index != 0) {
|
||||
val numberView = View.inflate(view.context, R.layout.contact_number_list_item, null)
|
||||
numberView.clicks().subscribe { contactSelected.onNext(copyContact(contact, index)) }
|
||||
numberView.setOnClickListener { contactSelected.onNext(copyContact(contact, index)) }
|
||||
numberView.address.text = number.address
|
||||
numberView.type.text = number.type
|
||||
view.addresses.addView(numberView)
|
||||
|
||||
@@ -143,7 +143,25 @@ class MessagesAdapter @Inject constructor(
|
||||
view.attachments.setRecycledViewPool(partsViewPool)
|
||||
view.body.forwardTouches(view)
|
||||
|
||||
return QkViewHolder(view)
|
||||
return QkViewHolder(view).apply {
|
||||
view.setOnClickListener {
|
||||
val message = getItem(adapterPosition)!!
|
||||
when (toggleSelection(message.id, false)) {
|
||||
true -> view.isSelected = isSelected(message.id)
|
||||
false -> {
|
||||
clicks.onNext(message)
|
||||
expanded[message.id] = view.status.visibility != View.VISIBLE
|
||||
notifyItemChanged(adapterPosition)
|
||||
}
|
||||
}
|
||||
}
|
||||
view.setOnLongClickListener {
|
||||
val message = getItem(adapterPosition)!!
|
||||
toggleSelection(message.id)
|
||||
view.isSelected = isSelected(message.id)
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
||||
@@ -152,22 +170,6 @@ class MessagesAdapter @Inject constructor(
|
||||
val next = if (position == itemCount - 1) null else getItem(position + 1)
|
||||
val view = viewHolder.itemView
|
||||
|
||||
view.setOnClickListener {
|
||||
when (toggleSelection(message.id, false)) {
|
||||
true -> view.isSelected = isSelected(message.id)
|
||||
false -> {
|
||||
clicks.onNext(message)
|
||||
expanded[message.id] = view.status.visibility != View.VISIBLE
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
view.setOnLongClickListener {
|
||||
toggleSelection(message.id)
|
||||
view.isSelected = isSelected(message.id)
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
// Update the selected state
|
||||
view.isSelected = isSelected(message.id) || highlight == message.id
|
||||
|
||||
@@ -59,15 +59,15 @@ class PartsAdapter(context: Context, navigator: Navigator, theme: Colors.Theme)
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
||||
val layout = partBinders.getOrNull(viewType)?.partLayout ?: 0
|
||||
return QkViewHolder(LayoutInflater.from(parent.context).inflate(layout, parent, false))
|
||||
val view = LayoutInflater.from(parent.context).inflate(layout, parent, false)
|
||||
messageView?.let(view::forwardTouches)
|
||||
return QkViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val part = data[position]
|
||||
val view = holder.itemView
|
||||
|
||||
messageView?.let(view::forwardTouches)
|
||||
|
||||
val canGroupWithPrevious = canGroup(message, previous) || position > 0
|
||||
val canGroupWithNext = canGroup(message, next) || position < itemCount - 1 || bodyVisible
|
||||
|
||||
|
||||
Reference in New Issue
Block a user