Skip to content

Add rxjava2 paging support to fix the sample crash #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions epoxy-pagingsample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ dependencies {
implementation rootProject.deps.kotlin

implementation "org.jetbrains.anko:anko-coroutines:0.10.5"
implementation "io.reactivex.rxjava2:rxjava:2.2.3"
implementation "io.reactivex.rxjava2:rxandroid:2.1.0"
implementation "androidx.room:room-common:$ANDROIDX_ROOM"
implementation "androidx.room:room-runtime:$ANDROIDX_ROOM"
implementation "androidx.paging:paging-common:$ANDROIDX_PAGING"
implementation "androidx.paging:paging-runtime:$ANDROIDX_PAGING"
implementation "androidx.paging:paging-rxjava2:$ANDROIDX_PAGING"
implementation "androidx.recyclerview:recyclerview:$ANDROIDX_RECYCLERVIEW"
kapt "androidx.room:room-compiler:$ANDROIDX_ROOM"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatTextView
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.paging.RxPagedListBuilder
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.room.Room
Expand All @@ -19,13 +17,18 @@ import com.airbnb.epoxy.EpoxyModel
import com.airbnb.epoxy.ModelView
import com.airbnb.epoxy.TextProp
import com.airbnb.epoxy.paging.PagedListEpoxyController
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.observers.DisposableObserver
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
import org.jetbrains.anko.coroutines.experimental.bg
import java.util.concurrent.TimeUnit

class PagingSampleActivity : AppCompatActivity() {
private val compositeDisposable = CompositeDisposable()
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -34,10 +37,27 @@ class PagingSampleActivity : AppCompatActivity() {
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = pagingController.adapter

val observer = object : DisposableObserver<PagedList<User>>() {
override fun onNext(pagedList: PagedList<User>) {
pagingController.submitList(pagedList)

}

override fun onError(e: Throwable) {

}

override fun onComplete() {
}
}

val viewModel = ViewModelProviders.of(this).get(ActivityViewModel::class.java)
viewModel.pagedList.observe(this, Observer {
pagingController.submitList(it)
})
compositeDisposable.add(viewModel.pagedList.subscribeWith(observer))
}

public override fun onDestroy() {
super.onDestroy()
compositeDisposable.dispose()
}
}

Expand Down Expand Up @@ -84,15 +104,18 @@ class PagingView(context: Context) : AppCompatTextView(context) {

}

class ActivityViewModel(app : Application) : AndroidViewModel(app) {
class ActivityViewModel(app: Application) : AndroidViewModel(app) {
val db by lazy {
Room.inMemoryDatabaseBuilder(app, PagingDatabase::class.java).build()
}
val pagedList : LiveData<PagedList<User>> by lazy {
LivePagedListBuilder<Int, User>(

val pagedList: Observable<PagedList<User>> by lazy {
RxPagedListBuilder<Int, User>(
db.userDao().dataSource, 100
).build()
).setNotifyScheduler(AndroidSchedulers.from(EpoxyAsyncUtil.getAsyncBackgroundHandler().looper))
.buildObservable()
}

init {
bg {
(1..3000).map {
Expand Down