-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for coroutines data source.
- Loading branch information
Sourabh Singh Rawat
committed
Oct 30, 2019
1 parent
930a35c
commit 95cdf18
Showing
29 changed files
with
128 additions
and
58 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
library/src/main/java/com/nooblabs/quickrvadapter/RvAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.