Skip to content

Instantly share code, notes, and snippets.

@lgawin
Last active September 26, 2019 21:48
Show Gist options
  • Save lgawin/16c79980ea5d4d0118b3134e276f59cc to your computer and use it in GitHub Desktop.
Save lgawin/16c79980ea5d4d0118b3134e276f59cc to your computer and use it in GitHub Desktop.
ObservableList extensions for `me.tatarka.bindingcollectionadapter2.collections`
import androidx.databinding.ObservableList
import androidx.recyclerview.widget.DiffUtil
import me.tatarka.bindingcollectionadapter2.collections.AsyncDiffObservableList
import me.tatarka.bindingcollectionadapter2.collections.DiffObservableList
@Suppress("NOTHING_TO_INLINE", "FunctionName")
inline fun <T : Any> DiffObservableList(
itemCallback: DiffUtil.ItemCallback<T>,
async: Boolean = true
): DiffList<T> =
if (async) diffListOf(AsyncDiffObservableList<T>(itemCallback))
else diffListOf(DiffObservableList<T>(itemCallback))
@Suppress("FunctionName")
inline fun <T : Any> DiffObservableList(
crossinline areItemsTheSame: (T, T) -> Boolean,
crossinline areContentsTheSame: (T, T) -> Boolean = { old, new -> old == new },
async: Boolean = true
) = DiffObservableList(DiffItemCallback(areItemsTheSame, areContentsTheSame), async)
inline fun <T> diffListOf(list: DiffObservableList<T>): DiffList<T> =
object : DiffList<T>, ObservableList<T> by list {
override fun update(map: List<T>) = list.update(map)
}
inline fun <T> diffListOf(list: AsyncDiffObservableList<T>): DiffList<T> =
object : DiffList<T>, ObservableList<T> by list {
override fun update(map: List<T>) = list.update(map)
}
interface DiffList<T> : ObservableList<T> {
fun update(map: List<T>)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment