From d77c30c6fda517287a2f66e2da9ea965119a0baa Mon Sep 17 00:00:00 2001 From: Dima Ivanov Date: Wed, 10 Feb 2021 19:11:41 +0300 Subject: [PATCH] Removed mutableItems --- .../rerekt/rekukler/MultibindingsAdapter.kt | 49 +++++++++---------- .../java/com/rerekt/sample/ui/ListFragment.kt | 31 ++++++------ 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt b/rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt index b490025..52e1bf9 100644 --- a/rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt +++ b/rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt @@ -25,9 +25,6 @@ open class MultiBindingAdapter( updateList(value) field = value } - get() = mutableItems - - private var mutableItems: MutableList = mutableListOf() @Suppress("UNCHECKED_CAST") val bindersSet = binders.toList() as List> @@ -58,26 +55,27 @@ open class MultiBindingAdapter( * Updating list using DiffUtil, can be called only in [items] setter */ private fun updateList(newList: List) { - mutableItems = newList.toMutableList() - DiffUtil.calculateDiff(object : DiffUtil.Callback() { - override fun getOldListSize() = items.size - override fun getNewListSize() = newList.size - override fun areItemsTheSame(old: Int, new: Int) = - kotlin.runCatching { - bindersSet.find { it.isForItem(items[old]) }?.areItemsSame?.invoke( - items[old], - newList[new] - ) ?: false - }.getOrElse { false } + DiffUtil.calculateDiff( + object : DiffUtil.Callback() { + override fun getOldListSize() = items.size + override fun getNewListSize() = newList.size + override fun areItemsTheSame(old: Int, new: Int) = + kotlin.runCatching { + bindersSet.find { it.isForItem(items[old]) }?.areItemsSame?.invoke( + items[old], + newList[new] + ) ?: false + }.getOrElse { false } - override fun areContentsTheSame(old: Int, new: Int) = - kotlin.runCatching { - bindersSet.find { it.isForItem(items[old]) }?.areContentsSame?.invoke( - items[old], - newList[new] - ) ?: false - }.getOrElse { false } - }).dispatchUpdatesTo(this) + override fun areContentsTheSame(old: Int, new: Int) = + kotlin.runCatching { + bindersSet.find { it.isForItem(items[old]) }?.areContentsSame?.invoke( + items[old], + newList[new] + ) ?: false + }.getOrElse { false } + } + ).dispatchUpdatesTo(this) } // view type must be position of binder in bindersSet @@ -90,16 +88,17 @@ open class MultiBindingAdapter( * @param toPosition - target position */ fun moveItem(fromPosition: Int, toPosition: Int): Boolean { + val swappedList = mutableListOf().apply { items.forEach(::add) } if (fromPosition < toPosition) { for (i in fromPosition until toPosition) { - Collections.swap(mutableItems, i, i + 1) + Collections.swap(swappedList, i, i + 1) } } else { for (i in fromPosition downTo toPosition + 1) { - Collections.swap(mutableItems, i, i - 1) + Collections.swap(swappedList, i, i - 1) } } - notifyItemMoved(fromPosition, toPosition) + items = swappedList return true } } \ No newline at end of file diff --git a/sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt b/sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt index 0e8de9a..d82f3e4 100644 --- a/sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt +++ b/sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt @@ -1,10 +1,12 @@ package com.rerekt.sample.ui import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.view.View import android.widget.LinearLayout import androidx.fragment.app.Fragment -import com.rerekt.rekukler.* +import com.rerekt.rekukler.MultiBindingAdapter import com.rerekt.rekukler.dsl.configure import com.rerekt.rekukler.dsl.itemDecoration import com.rerekt.rekukler.dsl.itemTouchHelper @@ -13,7 +15,6 @@ import com.rerekt.rekukler.utils.MarginDividerItemDecoration import com.rerekt.sample.R import com.rerekt.sample.databinding.FragmentMainBinding import com.rerekt.sample.ui.global.dip -import com.rerekt.sample.ui.global.int import com.rerekt.sample.ui.global.list.* class ListFragment: Fragment(R.layout.fragment_main) { @@ -37,18 +38,20 @@ class ListFragment: Fragment(R.layout.fragment_main) { @ExperimentalStdlibApi private fun fillAdapterItems() { - articlesAdapter.items = buildList { - addAll( - (0..20).map { - Article( - id = it, - title = "Title#$it", - description = "Description#$it" - ) - } - ) - add(Loading) - } + Handler(Looper.getMainLooper()).postDelayed( + { articlesAdapter.items = buildList { + addAll( + (0..20).map { + Article( + id = it, + title = "Title#$it", + description = "Description#$it" + ) + } + ) + add(Loading) + } }, 1000 + ) } private fun initRecycler() {