Skip to content

Instantly share code, notes, and snippets.

@arekolek
Last active August 27, 2020 04:09
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arekolek/c3ea17fab3921099d336202079a402a3 to your computer and use it in GitHub Desktop.
Save arekolek/c3ea17fab3921099d336202079a402a3 to your computer and use it in GitHub Desktop.
Using LiveDataReactiveStreams to handle lifecycle and threading while computing list diff for recycler view
package com.github.arekolek.diffutil
import android.arch.lifecycle.*
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.item.view.*
import java.util.concurrent.TimeUnit
class LiveDataReactiveStreamsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val model = ViewModelProviders.of(this).get(NamesViewModel::class.java)
// toggle the two comments and compare how rotations and pausing are handled
list.adapter = Adapter(model.names.toFlowable(this))
// list.adapter = Adapter(model.names)
}
}
class NamesViewModel : ViewModel() {
private val repository = Repository()
val names = repository.names.toLiveData()
// val names = repository.names
}
class Adapter(flowable: Flowable<List<String>>) : RecyclerView.Adapter<Holder>() {
private var data = emptyList<String>()
init {
flowable.observeOn(Schedulers.computation())
.scan(data to data) { prev, next -> prev.second to next }
.skip(1)
.map { (old, new) -> new to diffResult(old, new) }
.observeOn(AndroidSchedulers.mainThread())
.doOnNext { Log.d("Diff", "notified adapter: $this") }
.subscribe { (names, changes) ->
data = names
changes.dispatchUpdatesTo(this)
}
}
override fun getItemCount() = data.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
Holder(LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false))
override fun onBindViewHolder(holder: Holder, position: Int) = holder.bind(data[position])
private fun diffResult(old: List<String>, new: List<String>): DiffUtil.DiffResult {
return DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun getOldListSize() = old.size
override fun getNewListSize() = new.size
override fun areItemsTheSame(i: Int, j: Int) = old[i] == new[j]
override fun areContentsTheSame(i: Int, j: Int) = true
})
}
}
class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(name: String) {
itemView.name.text = name
}
}
class Repository {
val names: Flowable<List<String>> = Flowable
.interval(3, TimeUnit.SECONDS)
.subscribeOn(Schedulers.computation())
.map { NAMES.shuffled().take(15).sorted() }
companion object {
private val NAMES = listOf("Clementina Mccook", "Azzie Parry", "Herma Oday", "Ira Laughridge",
"Caron Thakkar", "Leta Tineo", "Saul Penaflor", "Toi Macomber", "Markus Goebel",
"Vanda Kessler", "Jackelyn Killinger", "Bernardina Aguon", "Ann Wilks", "Mindi Fleet",
"Mi Greenstein", "Bertram Ewart", "Deneen House", "Ossie Dinsmore", "Vincenzo Baumgart")
}
}
fun <T> LiveData<T>.toFlowable(owner: LifecycleOwner): Flowable<T> =
Flowable.fromPublisher(LiveDataReactiveStreams.toPublisher(owner, this))
fun <T> Flowable<T>.toLiveData(): LiveData<T> = LiveDataReactiveStreams.fromPublisher(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment