forked from upstream-mirrors/quik
Fix crash when ViewHolder's adapterPosition is -1
This should address #1340
This commit is contained in:
@@ -28,6 +28,7 @@ import io.realm.RealmList
|
||||
import io.realm.RealmModel
|
||||
import io.realm.RealmRecyclerViewAdapter
|
||||
import io.realm.RealmResults
|
||||
import timber.log.Timber
|
||||
|
||||
abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkViewHolder>(null, true) {
|
||||
|
||||
@@ -79,6 +80,15 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun getItem(index: Int): T? {
|
||||
if (index < 0) {
|
||||
Timber.w("Only indexes >= 0 are allowed. Input was: $index")
|
||||
return null
|
||||
}
|
||||
|
||||
return super.getItem(index)
|
||||
}
|
||||
|
||||
override fun updateData(data: OrderedRealmCollection<T>?) {
|
||||
if (getData() === data) return
|
||||
|
||||
|
||||
@@ -36,14 +36,14 @@ class BlockedAdapter @Inject constructor() : QkRealmAdapter<Conversation>() {
|
||||
val view = LayoutInflater.from(parent.context).inflate(R.layout.blocked_list_item, parent, false)
|
||||
return QkViewHolder(view).apply {
|
||||
view.setOnClickListener {
|
||||
val conversation = getItem(adapterPosition)!!
|
||||
val conversation = getItem(adapterPosition) ?: return@setOnClickListener
|
||||
unblock.onNext(conversation.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val conversation = getItem(position)!!
|
||||
val conversation = getItem(position) ?: return
|
||||
val view = holder.containerView
|
||||
|
||||
view.avatars.contacts = conversation.recipients
|
||||
|
||||
@@ -151,7 +151,7 @@ class MessagesAdapter @Inject constructor(
|
||||
|
||||
return QkViewHolder(view).apply {
|
||||
view.setOnClickListener {
|
||||
val message = getItem(adapterPosition)!!
|
||||
val message = getItem(adapterPosition) ?: return@setOnClickListener
|
||||
when (toggleSelection(message.id, false)) {
|
||||
true -> view.isActivated = isSelected(message.id)
|
||||
false -> {
|
||||
@@ -162,7 +162,7 @@ class MessagesAdapter @Inject constructor(
|
||||
}
|
||||
}
|
||||
view.setOnLongClickListener {
|
||||
val message = getItem(adapterPosition)!!
|
||||
val message = getItem(adapterPosition) ?: return@setOnLongClickListener true
|
||||
toggleSelection(message.id)
|
||||
view.isActivated = isSelected(message.id)
|
||||
true
|
||||
@@ -171,7 +171,7 @@ class MessagesAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
||||
val message = getItem(position)!!
|
||||
val message = getItem(position) ?: return
|
||||
val previous = if (position == 0) null else getItem(position - 1)
|
||||
val next = if (position == itemCount - 1) null else getItem(position + 1)
|
||||
val view = viewHolder.containerView
|
||||
@@ -296,7 +296,7 @@ class MessagesAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
val message = getItem(position)!!
|
||||
val message = getItem(position) ?: return -1
|
||||
return when (message.isMe()) {
|
||||
true -> VIEW_TYPE_MESSAGE_OUT
|
||||
false -> VIEW_TYPE_MESSAGE_IN
|
||||
|
||||
+2
-2
@@ -42,14 +42,14 @@ class ConversationMediaAdapter @Inject constructor(
|
||||
val view = inflater.inflate(R.layout.conversation_media_list_item, parent, false)
|
||||
return QkViewHolder(view).apply {
|
||||
view.thumbnail.setOnClickListener {
|
||||
val part = getItem(adapterPosition)!!
|
||||
val part = getItem(adapterPosition) ?: return@setOnClickListener
|
||||
navigator.showMedia(part.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val part = getItem(position)!!
|
||||
val part = getItem(position) ?: return
|
||||
val view = holder.containerView
|
||||
|
||||
GlideApp.with(context)
|
||||
|
||||
+2
-2
@@ -44,7 +44,7 @@ class ConversationRecipientAdapter @Inject constructor(
|
||||
val view = layoutInflater.inflate(R.layout.conversation_recipient_list_item, parent, false)
|
||||
return QkViewHolder(view).apply {
|
||||
view.setOnClickListener {
|
||||
val recipient = getItem(adapterPosition)!!
|
||||
val recipient = getItem(adapterPosition) ?: return@setOnClickListener
|
||||
if (recipient.contact == null) {
|
||||
navigator.addContact(recipient.address)
|
||||
} else {
|
||||
@@ -55,7 +55,7 @@ class ConversationRecipientAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val recipient = getItem(position)!!
|
||||
val recipient = getItem(position) ?: return
|
||||
val view = holder.containerView
|
||||
|
||||
view.avatar.threadId = threadId
|
||||
|
||||
+4
-4
@@ -62,14 +62,14 @@ class ConversationsAdapter @Inject constructor(
|
||||
|
||||
return QkViewHolder(view).apply {
|
||||
view.setOnClickListener {
|
||||
val conversation = getItem(adapterPosition)!!
|
||||
val conversation = getItem(adapterPosition) ?: return@setOnClickListener
|
||||
when (toggleSelection(conversation.id, false)) {
|
||||
true -> view.isActivated = isSelected(conversation.id)
|
||||
false -> navigator.showConversation(conversation.id)
|
||||
}
|
||||
}
|
||||
view.setOnLongClickListener {
|
||||
val conversation = getItem(adapterPosition)!!
|
||||
val conversation = getItem(adapterPosition) ?: return@setOnLongClickListener true
|
||||
toggleSelection(conversation.id)
|
||||
view.isActivated = isSelected(conversation.id)
|
||||
true
|
||||
@@ -78,7 +78,7 @@ class ConversationsAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
||||
val conversation = getItem(position)!!
|
||||
val conversation = getItem(position) ?: return
|
||||
val view = viewHolder.containerView
|
||||
|
||||
view.isActivated = isSelected(conversation.id)
|
||||
@@ -98,6 +98,6 @@ class ConversationsAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return if (getItem(position)!!.read) 0 else 1
|
||||
return if (getItem(position)?.read == true) 0 else 1
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ class GalleryPagerAdapter @Inject constructor(private val context: Context) : Qk
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val part = getItem(position)!!
|
||||
val part = getItem(position) ?: return
|
||||
val view = holder.containerView
|
||||
when (getItemViewType(position)) {
|
||||
VIEW_TYPE_IMAGE -> {
|
||||
|
||||
+2
-2
@@ -56,14 +56,14 @@ class ScheduledMessageAdapter @Inject constructor(
|
||||
|
||||
return QkViewHolder(view).apply {
|
||||
view.setOnClickListener {
|
||||
val message = getItem(adapterPosition)!!
|
||||
val message = getItem(adapterPosition) ?: return@setOnClickListener
|
||||
clicks.onNext(message.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val message = getItem(position)!!
|
||||
val message = getItem(position) ?: return
|
||||
val view = holder.containerView
|
||||
|
||||
// GroupAvatarView only accepts recipients, so map the phone numbers to recipients
|
||||
|
||||
Reference in New Issue
Block a user