Skip to content

Commit

Permalink
Merge pull request #22 from Rere-kt/feature/function-of-move-list-items
Browse files Browse the repository at this point in the history
Removed mutableItems
  • Loading branch information
dionep authored Feb 10, 2021
2 parents 4dc6b6d + d77c30c commit 63f4d11
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
49 changes: 24 additions & 25 deletions rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ open class MultiBindingAdapter(
updateList(value)
field = value
}
get() = mutableItems

private var mutableItems: MutableList<Any> = mutableListOf()

@Suppress("UNCHECKED_CAST")
val bindersSet = binders.toList() as List<ViewBinder<Any, ViewBinding>>
Expand Down Expand Up @@ -58,26 +55,27 @@ open class MultiBindingAdapter(
* Updating list using DiffUtil, can be called only in [items] setter
*/
private fun updateList(newList: List<Any>) {
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
Expand All @@ -90,16 +88,17 @@ open class MultiBindingAdapter(
* @param toPosition - target position
*/
fun moveItem(fromPosition: Int, toPosition: Int): Boolean {
val swappedList = mutableListOf<Any>().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
}
}
31 changes: 17 additions & 14 deletions sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down

0 comments on commit 63f4d11

Please sign in to comment.