From 14766800fff24cdd2797bf63580e7fb61cda5294 Mon Sep 17 00:00:00 2001 From: Radoslav Yankov Date: Tue, 21 Apr 2020 12:31:29 +0300 Subject: [PATCH] formatting and naming --- .../rados/recyclerviewlibrary/MainActivity.kt | 6 ++--- .../java/com/list/rados/fast_list/BaseList.kt | 25 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/list/rados/recyclerviewlibrary/MainActivity.kt b/app/src/main/java/com/list/rados/recyclerviewlibrary/MainActivity.kt index 3c2e587..472ece5 100644 --- a/app/src/main/java/com/list/rados/recyclerviewlibrary/MainActivity.kt +++ b/app/src/main/java/com/list/rados/recyclerviewlibrary/MainActivity.kt @@ -36,19 +36,19 @@ class MainActivity : AppCompatActivity() { } recycler_view.bind(list) - .map(layout = R.layout.item, predicate = {it:Item, _ -> it.type == 1 }) { item: Item, position: Int -> + .map(layout = R.layout.item, predicate = { it: Item, _ -> it.type == 1 }) { item: Item, position: Int -> item_text.text = item.value container.setOnClickListener { toast(item.value) } } - .map(layout = R.layout.item_second, predicate = {it:Item, _ -> it.type == 2 }) { item: Item, position: Int -> + .map(layout = R.layout.item_second, predicate = { it: Item, _ -> it.type == 2 }) { item: Item, position: Int -> item_second_text.text = item.value container_second.setOnClickListener { toast(item.value) } } - .map(layoutFactory = LocalFactory(this), predicate = {it:Item, _ -> it.type == 3 }) { item: Item, position: Int -> + .map(layoutFactory = LocalFactory(this), predicate = { it: Item, _ -> it.type == 3 }) { item: Item, position: Int -> item_custom_text.text = item.value container_custom.setOnClickListener { toast(item.value) diff --git a/fast-list/src/main/java/com/list/rados/fast_list/BaseList.kt b/fast-list/src/main/java/com/list/rados/fast_list/BaseList.kt index 8ff3349..a48d620 100644 --- a/fast-list/src/main/java/com/list/rados/fast_list/BaseList.kt +++ b/fast-list/src/main/java/com/list/rados/fast_list/BaseList.kt @@ -45,7 +45,7 @@ fun ViewPager2.bind(items: List): FastListAdapter { fun RecyclerView.bind(items: List, @LayoutRes singleLayout: Int = 0, singleBind: BindingClosure): FastListAdapter { layoutManager = LinearLayoutManager(context) return FastListAdapter(items.toMutableList(), this - ).map(singleLayout, {item: T, idx: Int -> true }, singleBind) + ).map(singleLayout, { item: T, position: Int -> true }, singleBind) } /** @@ -58,10 +58,9 @@ fun RecyclerView.bind(items: List, @LayoutRes singleLayout: Int = 0, sing */ fun ViewPager2.bind(items: List, @LayoutRes singleLayout: Int = 0, singleBind: BindingClosure): FastListAdapter { return FastListAdapter(items.toMutableList(), vpList = this - ).map(singleLayout, {item: T, idx: Int -> true }, singleBind) + ).map(singleLayout, { item: T, position: Int -> true }, singleBind) } - /** * Updates the list using DiffUtils. * @param newItems the new list which is to replace the old one. @@ -85,7 +84,7 @@ fun ViewPager2.update(newItems: List) { (adapter as? FastListAdapter)?.update(newItems) { o, n, _ -> o == n } } -open class FastListAdapter(private var items: MutableList, private var list: RecyclerView?=null, private var vpList : ViewPager2?=null) +open class FastListAdapter(private var items: MutableList, private var list: RecyclerView? = null, private var vpList: ViewPager2? = null) : RecyclerView.Adapter>() { init { @@ -96,11 +95,12 @@ open class FastListAdapter(private var items: MutableList, private var lis } - private inner class BindMap(val layout: Int, var type: Int = 0, val bind: BindingClosure, val predicate: (item: T, idx : Int) -> Boolean) { - constructor(lf: LayoutFactory, type: Int = 0, bind: BindingClosure, predicate: (item: T, idx : Int) -> Boolean) : this(0, type, bind, predicate){ + private inner class BindMap(val layout: Int, var type: Int = 0, val bind: BindingClosure, val predicate: (item: T, position: Int) -> Boolean) { + constructor(lf: LayoutFactory, type: Int = 0, bind: BindingClosure, predicate: (item: T, position: Int) -> Boolean) : this(0, type, bind, predicate) { layoutFactory = lf } - var layoutFactory : LayoutFactory? = null + + var layoutFactory: LayoutFactory? = null } private var bindMap = mutableListOf() @@ -110,7 +110,7 @@ open class FastListAdapter(private var items: MutableList, private var lis return bindMap.first { it.type == viewType }.let { it.layoutFactory?.let { return FastListViewHolder(it.createView(parent, viewType), viewType) - } ?: run{ + } ?: run { return FastListViewHolder(LayoutInflater.from(parent.context).inflate(it.layout, parent, false), viewType) } @@ -137,14 +137,13 @@ open class FastListAdapter(private var items: MutableList, private var lis * @param bind - The "binding" function between the item and the layout. This is the standard "bind" function in traditional ViewHolder classes. It uses Kotlin Extensions * so you can just use the XML names of the views inside your layout to address them. */ - fun map(@LayoutRes layout: Int, predicate: (item: T, idx : Int) -> Boolean, bind: BindingClosure): FastListAdapter { + fun map(@LayoutRes layout: Int, predicate: (item: T, position: Int) -> Boolean, bind: BindingClosure): FastListAdapter { bindMap.add(BindMap(layout, typeCounter++, bind, predicate)) list?.adapter = this vpList?.adapter = this return this } - /** * The function used for mapping types to layouts * @param layoutFactory - factory that creates the view for this adapter @@ -152,7 +151,7 @@ open class FastListAdapter(private var items: MutableList, private var lis * @param bind - The "binding" function between the item and the layout. This is the standard "bind" function in traditional ViewHolder classes. It uses Kotlin Extensions * so you can just use the XML names of the views inside your layout to address them. */ - fun map(layoutFactory: LayoutFactory, predicate: (item: T, idx : Int) -> Boolean, bind: BindingClosure): FastListAdapter { + fun map(layoutFactory: LayoutFactory, predicate: (item: T, position: Int) -> Boolean, bind: BindingClosure): FastListAdapter { bindMap.add(BindMap(layoutFactory, typeCounter++, bind, predicate)) list?.adapter = this vpList?.adapter = this @@ -163,7 +162,7 @@ open class FastListAdapter(private var items: MutableList, private var lis * Sets up a layout manager for the recycler view. */ fun layoutManager(manager: RecyclerView.LayoutManager): FastListAdapter { - vpList?.let{ throw UnsupportedOperationException("layoumanager not needed for ViewPager2")} + vpList?.let { throw UnsupportedOperationException("layoumanager not needed for ViewPager2") } list!!.layoutManager = manager return this } @@ -193,7 +192,7 @@ open class FastListAdapter(private var items: MutableList, private var lis } interface LayoutFactory { - fun createView(parent: ViewGroup, type: Int) : View + fun createView(parent: ViewGroup, type: Int): View } class FastListViewHolder(override val containerView: View, val holderType: Int) : RecyclerView.ViewHolder(containerView), LayoutContainer {