Skip to content

Commit 1476680

Browse files
author
Radoslav Yankov
committed
formatting and naming
1 parent 39bb571 commit 1476680

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

app/src/main/java/com/list/rados/recyclerviewlibrary/MainActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ class MainActivity : AppCompatActivity() {
3636
}
3737

3838
recycler_view.bind(list)
39-
.map(layout = R.layout.item, predicate = {it:Item, _ -> it.type == 1 }) { item: Item, position: Int ->
39+
.map(layout = R.layout.item, predicate = { it: Item, _ -> it.type == 1 }) { item: Item, position: Int ->
4040
item_text.text = item.value
4141
container.setOnClickListener {
4242
toast(item.value)
4343
}
4444
}
45-
.map(layout = R.layout.item_second, predicate = {it:Item, _ -> it.type == 2 }) { item: Item, position: Int ->
45+
.map(layout = R.layout.item_second, predicate = { it: Item, _ -> it.type == 2 }) { item: Item, position: Int ->
4646
item_second_text.text = item.value
4747
container_second.setOnClickListener {
4848
toast(item.value)
4949
}
5050
}
51-
.map(layoutFactory = LocalFactory(this), predicate = {it:Item, _ -> it.type == 3 }) { item: Item, position: Int ->
51+
.map(layoutFactory = LocalFactory(this), predicate = { it: Item, _ -> it.type == 3 }) { item: Item, position: Int ->
5252
item_custom_text.text = item.value
5353
container_custom.setOnClickListener {
5454
toast(item.value)

fast-list/src/main/java/com/list/rados/fast_list/BaseList.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fun <T> ViewPager2.bind(items: List<T>): FastListAdapter<T> {
4545
fun <T> RecyclerView.bind(items: List<T>, @LayoutRes singleLayout: Int = 0, singleBind: BindingClosure<T>): FastListAdapter<T> {
4646
layoutManager = LinearLayoutManager(context)
4747
return FastListAdapter(items.toMutableList(), this
48-
).map(singleLayout, {item: T, idx: Int -> true }, singleBind)
48+
).map(singleLayout, { item: T, position: Int -> true }, singleBind)
4949
}
5050

5151
/**
@@ -58,10 +58,9 @@ fun <T> RecyclerView.bind(items: List<T>, @LayoutRes singleLayout: Int = 0, sing
5858
*/
5959
fun <T> ViewPager2.bind(items: List<T>, @LayoutRes singleLayout: Int = 0, singleBind: BindingClosure<T>): FastListAdapter<T> {
6060
return FastListAdapter(items.toMutableList(), vpList = this
61-
).map(singleLayout, {item: T, idx: Int -> true }, singleBind)
61+
).map(singleLayout, { item: T, position: Int -> true }, singleBind)
6262
}
6363

64-
6564
/**
6665
* Updates the list using DiffUtils.
6766
* @param newItems the new list which is to replace the old one.
@@ -85,7 +84,7 @@ fun <T> ViewPager2.update(newItems: List<T>) {
8584
(adapter as? FastListAdapter<T>)?.update(newItems) { o, n, _ -> o == n }
8685
}
8786

88-
open class FastListAdapter<T>(private var items: MutableList<T>, private var list: RecyclerView?=null, private var vpList : ViewPager2?=null)
87+
open class FastListAdapter<T>(private var items: MutableList<T>, private var list: RecyclerView? = null, private var vpList: ViewPager2? = null)
8988
: RecyclerView.Adapter<FastListViewHolder<T>>() {
9089

9190
init {
@@ -96,11 +95,12 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
9695

9796
}
9897

99-
private inner class BindMap(val layout: Int, var type: Int = 0, val bind: BindingClosure<T>, val predicate: (item: T, idx : Int) -> Boolean) {
100-
constructor(lf: LayoutFactory, type: Int = 0, bind: BindingClosure<T>, predicate: (item: T, idx : Int) -> Boolean) : this(0, type, bind, predicate){
98+
private inner class BindMap(val layout: Int, var type: Int = 0, val bind: BindingClosure<T>, val predicate: (item: T, position: Int) -> Boolean) {
99+
constructor(lf: LayoutFactory, type: Int = 0, bind: BindingClosure<T>, predicate: (item: T, position: Int) -> Boolean) : this(0, type, bind, predicate) {
101100
layoutFactory = lf
102101
}
103-
var layoutFactory : LayoutFactory? = null
102+
103+
var layoutFactory: LayoutFactory? = null
104104
}
105105

106106
private var bindMap = mutableListOf<BindMap>()
@@ -110,7 +110,7 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
110110
return bindMap.first { it.type == viewType }.let {
111111
it.layoutFactory?.let {
112112
return FastListViewHolder(it.createView(parent, viewType), viewType)
113-
} ?: run{
113+
} ?: run {
114114
return FastListViewHolder(LayoutInflater.from(parent.context).inflate(it.layout,
115115
parent, false), viewType)
116116
}
@@ -137,22 +137,21 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
137137
* @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
138138
* so you can just use the XML names of the views inside your layout to address them.
139139
*/
140-
fun map(@LayoutRes layout: Int, predicate: (item: T, idx : Int) -> Boolean, bind: BindingClosure<T>): FastListAdapter<T> {
140+
fun map(@LayoutRes layout: Int, predicate: (item: T, position: Int) -> Boolean, bind: BindingClosure<T>): FastListAdapter<T> {
141141
bindMap.add(BindMap(layout, typeCounter++, bind, predicate))
142142
list?.adapter = this
143143
vpList?.adapter = this
144144
return this
145145
}
146146

147-
148147
/**
149148
* The function used for mapping types to layouts
150149
* @param layoutFactory - factory that creates the view for this adapter
151150
* @param predicate - Function used to sort the items. For example, a Type field inside your items class with different values for different types.
152151
* @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
153152
* so you can just use the XML names of the views inside your layout to address them.
154153
*/
155-
fun map(layoutFactory: LayoutFactory, predicate: (item: T, idx : Int) -> Boolean, bind: BindingClosure<T>): FastListAdapter<T> {
154+
fun map(layoutFactory: LayoutFactory, predicate: (item: T, position: Int) -> Boolean, bind: BindingClosure<T>): FastListAdapter<T> {
156155
bindMap.add(BindMap(layoutFactory, typeCounter++, bind, predicate))
157156
list?.adapter = this
158157
vpList?.adapter = this
@@ -163,7 +162,7 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
163162
* Sets up a layout manager for the recycler view.
164163
*/
165164
fun layoutManager(manager: RecyclerView.LayoutManager): FastListAdapter<T> {
166-
vpList?.let{ throw UnsupportedOperationException("layoumanager not needed for ViewPager2")}
165+
vpList?.let { throw UnsupportedOperationException("layoumanager not needed for ViewPager2") }
167166
list!!.layoutManager = manager
168167
return this
169168
}
@@ -193,7 +192,7 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
193192
}
194193

195194
interface LayoutFactory {
196-
fun createView(parent: ViewGroup, type: Int) : View
195+
fun createView(parent: ViewGroup, type: Int): View
197196
}
198197

199198
class FastListViewHolder<T>(override val containerView: View, val holderType: Int) : RecyclerView.ViewHolder(containerView), LayoutContainer {

0 commit comments

Comments
 (0)