forked from upstream-mirrors/quik
Fixed DiffUtilCallback
This commit is contained in:
@@ -1,33 +0,0 @@
|
|||||||
package com.moez.QKSMS.common.util
|
|
||||||
|
|
||||||
import android.support.v7.util.DiffUtil
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Abstract implementation of DiffUtil.Callback, which can be used when we don't need
|
|
||||||
* a detailed change payload
|
|
||||||
*
|
|
||||||
* If we need a detailed change payload, this class can be extended and #areContentsTheSame
|
|
||||||
* and #getChangePayload should overridden
|
|
||||||
*/
|
|
||||||
class AbstractDiffUtilCallback<T>(
|
|
||||||
private val oldList: List<T>,
|
|
||||||
private val newList: List<T>) :
|
|
||||||
DiffUtil.Callback() {
|
|
||||||
|
|
||||||
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
|
|
||||||
return oldList[oldItemPosition] === newList[newItemPosition]
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getOldListSize(): Int {
|
|
||||||
return oldList.size
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getNewListSize(): Int {
|
|
||||||
return newList.size
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
|
|
||||||
return oldList[oldItemPosition] == newList[newItemPosition]
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,7 @@ import io.reactivex.disposables.Disposable
|
|||||||
* Base RecyclerView.Adapter that provides some convenience when creating a new Adapter, such as
|
* Base RecyclerView.Adapter that provides some convenience when creating a new Adapter, such as
|
||||||
* data list handing and item animations
|
* data list handing and item animations
|
||||||
*/
|
*/
|
||||||
abstract class FlowableAdapter<T, VH : RecyclerView.ViewHolder>(private val flowable: Flowable<List<T>>) : QkAdapter<T, VH>() {
|
abstract class FlowableAdapter<T>(private val flowable: Flowable<List<T>>) : QkAdapter<T>() {
|
||||||
|
|
||||||
private var disposable: Disposable? = null
|
private var disposable: Disposable? = null
|
||||||
|
|
||||||
|
|||||||
@@ -2,17 +2,16 @@ package com.moez.QKSMS.presentation.base
|
|||||||
|
|
||||||
import android.support.v7.util.DiffUtil
|
import android.support.v7.util.DiffUtil
|
||||||
import android.support.v7.widget.RecyclerView
|
import android.support.v7.widget.RecyclerView
|
||||||
import com.moez.QKSMS.common.util.AbstractDiffUtilCallback
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base RecyclerView.Adapter that provides some convenience when creating a new Adapter, such as
|
* Base RecyclerView.Adapter that provides some convenience when creating a new Adapter, such as
|
||||||
* data list handing and item animations
|
* data list handing and item animations
|
||||||
*/
|
*/
|
||||||
abstract class QkAdapter<T, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>() {
|
abstract class QkAdapter<T> : RecyclerView.Adapter<QkViewHolder>() {
|
||||||
|
|
||||||
var data: List<T> = ArrayList()
|
var data: List<T> = ArrayList()
|
||||||
set(value) {
|
set(value) {
|
||||||
val diff = DiffUtil.calculateDiff(provideDiffUtilCallback(field, value))
|
val diff = DiffUtil.calculateDiff(getDiffUtilCallback(field, value))
|
||||||
field = value
|
field = value
|
||||||
diff.dispatchUpdatesTo(this)
|
diff.dispatchUpdatesTo(this)
|
||||||
onDatasetChanged()
|
onDatasetChanged()
|
||||||
@@ -26,16 +25,32 @@ abstract class QkAdapter<T, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter
|
|||||||
return data.size
|
return data.size
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun onDatasetChanged() {
|
open fun onDatasetChanged() {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows the adapter implementation to provide a custom DiffUtil.Callback
|
* Allows the adapter implementation to provide a custom DiffUtil.Callback
|
||||||
* If not, then the abstract implementation will be used
|
* If not, then the abstract implementation will be used
|
||||||
*/
|
*/
|
||||||
open fun provideDiffUtilCallback(oldData: List<T>, newData: List<T>): DiffUtil.Callback {
|
private fun getDiffUtilCallback(oldData: List<T>, newData: List<T>): DiffUtil.Callback {
|
||||||
return AbstractDiffUtilCallback(oldData, newData)
|
return object : DiffUtil.Callback() {
|
||||||
|
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
|
||||||
|
areItemsTheSame(oldData[oldItemPosition], newData[newItemPosition])
|
||||||
|
|
||||||
|
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
|
||||||
|
areContentsTheSame(oldData[oldItemPosition], newData[newItemPosition])
|
||||||
|
|
||||||
|
override fun getOldListSize() = oldData.size
|
||||||
|
|
||||||
|
override fun getNewListSize() = newData.size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open protected fun areItemsTheSame(old: T, new: T): Boolean {
|
||||||
|
return old == new
|
||||||
|
}
|
||||||
|
|
||||||
|
open protected fun areContentsTheSame(old: T, new: T): Boolean {
|
||||||
|
return old == new
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ import kotlinx.android.synthetic.main.contact_chip.view.*
|
|||||||
class ChipsAdapter(
|
class ChipsAdapter(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val view: RecyclerView)
|
private val view: RecyclerView)
|
||||||
: QkAdapter<Contact, QkViewHolder>() {
|
: QkAdapter<Contact>() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val TYPE_EDIT_TEXT = 0
|
private val TYPE_EDIT_TEXT = 0
|
||||||
@@ -118,4 +118,8 @@ class ChipsAdapter(
|
|||||||
detailedChipView.hide()
|
detailedChipView.hide()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun areItemsTheSame(old: Contact, new: Contact): Boolean {
|
||||||
|
return old.recipientId == new.recipientId
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import io.reactivex.subjects.PublishSubject
|
|||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
import kotlinx.android.synthetic.main.contact_list_item.view.*
|
import kotlinx.android.synthetic.main.contact_list_item.view.*
|
||||||
|
|
||||||
class ContactAdapter(private val context: Context) : QkAdapter<Contact, QkViewHolder>() {
|
class ContactAdapter(private val context: Context) : QkAdapter<Contact>() {
|
||||||
|
|
||||||
val contactSelected: Subject<Contact> = PublishSubject.create()
|
val contactSelected: Subject<Contact> = PublishSubject.create()
|
||||||
|
|
||||||
@@ -34,4 +34,8 @@ class ContactAdapter(private val context: Context) : QkAdapter<Contact, QkViewHo
|
|||||||
view.address.text = contact.address
|
view.address.text = contact.address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun areItemsTheSame(old: Contact, new: Contact): Boolean {
|
||||||
|
return old.recipientId == new.recipientId
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@ import io.reactivex.Flowable
|
|||||||
import kotlinx.android.synthetic.main.conversation_list_item.view.*
|
import kotlinx.android.synthetic.main.conversation_list_item.view.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ConversationsAdapter(flowable: Flowable<List<ConversationMessagePair>>) : FlowableAdapter<ConversationMessagePair, QkViewHolder>(flowable) {
|
class ConversationsAdapter(flowable: Flowable<List<ConversationMessagePair>>) : FlowableAdapter<ConversationMessagePair>(flowable) {
|
||||||
|
|
||||||
@Inject lateinit var context: Context
|
@Inject lateinit var context: Context
|
||||||
@Inject lateinit var navigator: Navigator
|
@Inject lateinit var navigator: Navigator
|
||||||
@@ -61,4 +61,8 @@ class ConversationsAdapter(flowable: Flowable<List<ConversationMessagePair>>) :
|
|||||||
override fun getItemViewType(position: Int): Int {
|
override fun getItemViewType(position: Int): Int {
|
||||||
return if (getItem(position).message.read) 0 else 1
|
return if (getItem(position).message.read) 0 else 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun areItemsTheSame(old: ConversationMessagePair, new: ConversationMessagePair): Boolean {
|
||||||
|
return old.conversation.id == new.conversation.id
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user