Skip to content

Commit

Permalink
Removed viewBinders() from dsl because of impossibility to save adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakhimulin Damir committed Jan 19, 2021
1 parent 3c01c4d commit 8429070
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ gradle.xml
.idea/markdown-navigator-enh.xml
.idea/markdown-navigator.xml
.idea/runConfigurations.xml
/.idea/kotlinScripting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

open class MultiBindingAdapter(
val bindersSet: List<ViewBinder<Any, ViewBinding>>
vararg binders: ViewBinder<*, *>
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

var items: List<Any> = listOf()
internal val bindersSet = binders.toList() as List<ViewBinder<Any, ViewBinding>>

override fun onCreateViewHolder(
parent: ViewGroup,
Expand Down
10 changes: 3 additions & 7 deletions rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewDSL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package com.rerekt.rekukler
import android.content.Context
import android.graphics.Bitmap
import androidx.core.graphics.drawable.toDrawable
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.*
import androidx.viewbinding.ViewBinding

fun RecyclerView.configure(
customAdapter: ((List<ViewBinder<Any, ViewBinding>>) -> MultiBindingAdapter)? = null,
adapter: MultiBindingAdapter,
block: RecyclerViewConfig.() -> Unit
) {
RecyclerViewConfig(context).also {
block(it)
layoutManager = it.layoutManager
adapter = customAdapter?.invoke(it.bindersSet) ?: MultiBindingAdapter(it.bindersSet)
this.adapter = adapter
it.itemDecorations.forEach { addItemDecoration(it) }
}
}
Expand All @@ -40,7 +41,6 @@ class RecyclerViewConfig(
private val context: Context
) {

internal var bindersSet = listOf<ViewBinder<Any, ViewBinding>>()
internal var itemDecorations: MutableList<RecyclerView.ItemDecoration> = mutableListOf()

internal var layoutManager: RecyclerView.LayoutManager =
Expand Down Expand Up @@ -82,8 +82,4 @@ class RecyclerViewConfig(
itemDecorations.add(decoration)
}

fun viewBinders(vararg viewBinder: ViewBinder<*, *>) {
bindersSet = viewBinder.toList() as List<ViewBinder<Any, ViewBinding>>
}

}
14 changes: 9 additions & 5 deletions sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Looper
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.rerekt.rekukler.MultiBindingAdapter
import com.rerekt.rekukler.configure
import com.rerekt.rekukler.updateList
import com.rerekt.sample.R
Expand All @@ -17,6 +18,13 @@ class ListFragment: Fragment(R.layout.fragment_main) {

lateinit var binding: FragmentMainBinding

private val adapter by lazy {
MultiBindingAdapter(
articlesBinder { println("Click from Article item") },
loadingBinder()
)
}

@ExperimentalStdlibApi
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down Expand Up @@ -47,15 +55,11 @@ class ListFragment: Fragment(R.layout.fragment_main) {
}

private fun initRecycler() {
binding.rvArticles.configure {
binding.rvArticles.configure(adapter) {
linearLayout {
reverseLayout = false
orientation = LinearLayoutManager.VERTICAL
}
viewBinders(
articlesBinder { println("Click from Article item") },
loadingBinder()
)
dividerItemDecoration(
size = 2.dip(resources).toInt()
)
Expand Down

0 comments on commit 8429070

Please sign in to comment.