Skip to content

Commit

Permalink
Added support for coroutines data source.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sourabh Singh Rawat committed Oct 30, 2019
1 parent 930a35c commit 95cdf18
Show file tree
Hide file tree
Showing 29 changed files with 128 additions and 58 deletions.
57 changes: 0 additions & 57 deletions app/src/main/java/com/nooblabs/quickrvadapter/Main.kt

This file was deleted.

5 changes: 4 additions & 1 deletion app/build.gradle → library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
versionName "1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -36,4 +36,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

//Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
}
File renamed without changes.
File renamed without changes.
89 changes: 89 additions & 0 deletions library/src/main/java/com/nooblabs/quickrvadapter/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.nooblabs.quickrvadapter

import androidx.annotation.LayoutRes
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect

/**
* Creates a [RvAdapter].
*
* Example Usage:
* ```
* private val rvAdapter by lazy {
* adapter(R.layout.item_layout, listOf("One", "Two")) {
* bindings {
* bind<TextView>(R.id.count_text) { datum, countTextView ->
* countTextView.text = datum
* }
* }
* }
* }
* ```
*
* @param layout id of the item layout
* @param initialData list of initial data
* @param init initialization block
* @return [RvAdapter]
*/
fun <T> adapter(
@LayoutRes layout: Int, initialData: List<T> = emptyList(),
init: AdapterDsl<T>.() -> Unit
): RvAdapter<T> {

val adapterDsl = AdapterDsl<T>()

val rvAdapter = RvAdapter(layout, adapterDsl)

adapterDsl.init()

return rvAdapter
}

/**
* Creates a [RvAdapter] from [flow] of data.
*
* Example Usage:
* ```
* private val rvAdapter by lazy {
* adapter(R.layout.item_layout, flowOf("One", "Two")) {
* bindings {
* bind<TextView>(R.id.count_text) { datum, countTextView ->
* countTextView.text = datum
* }
* }
* }
* }
* ```
*
* @param layout id of the item layout
* @param flow flow stream of data
* @param coroutineScope scope where the [flow] will be collected. Defaults to [GlobalScope]
* @param init initialization block
* @return [RvAdapter]
*/
fun <T> adapter(
@LayoutRes layout: Int,
flow: Flow<T>,
coroutineScope: CoroutineScope = GlobalScope,
init: AdapterDsl<T>.() -> Unit
): RvAdapter<T> {

val adapterDsl = AdapterDsl<T>()

val rvAdapter = RvAdapter(layout, adapterDsl)

adapterDsl.init()

coroutineScope.launch {
flow.collect { datum ->
delay(3000)
launch(Dispatchers.Main) {
rvAdapter.addItem(datum)
}
}
}

return rvAdapter;

}
35 changes: 35 additions & 0 deletions library/src/main/java/com/nooblabs/quickrvadapter/RvAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.nooblabs.quickrvadapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView

class RvAdapter<T>(@LayoutRes private val layout: Int, private val adapterDsl: AdapterDsl<T>) :
RecyclerView.Adapter<RvViewHolder>() {

private var data: ArrayList<T> = ArrayList()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RvViewHolder {
return RvViewHolder(LayoutInflater.from(parent.context).inflate(layout, parent, false))
}

override fun getItemCount() = data.size

override fun onBindViewHolder(holder: RvViewHolder, position: Int) {
val datum = data[position]
adapterDsl.viewBindings.forEach { viewBinding ->
viewBinding.mapper(datum, holder.itemView.findViewById(viewBinding.viewId))
}
}

fun addItem(item: T) {
data.add(item)
notifyDataSetChanged()
}

fun clear() {
data.clear()
notifyDataSetChanged()
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 95cdf18

Please sign in to comment.