Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mazenrashed/RxPagination

Implement pagination in just few lines with RxPagination
https://github.com/mazenrashed/RxPagination

pagination recyclerview rx rxandroid rxjava rxjava2 rxkotlin rxpagination rxrelay

Last synced: about 1 month ago
JSON representation

Implement pagination in just few lines with RxPagination

Awesome Lists containing this project

README

        

# RxPagination

[![](https://jitpack.io/v/mazenrashed/RxPagination.svg)](https://jitpack.io/#mazenrashed/RxPagination)

Implement the pagination in a few lines,

![](https://media.giphy.com/media/RN2oZPyBkUKQbi1WD1/giphy.gif)
### Add the JitPack repository to your build file
```groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
### Add dependency
```groovy
dependencies {
implementation 'com.github.mazenrashed:RxPagination:${LAST_VERSION}'
}
```
# Usage
We will use the car example,
## In Your repository
Jest implement ListRepository\
```kotlin
class CarRepository : ListRepository{
...
}
```

Then, you need to override getDataList(page: Int, pageSize : Int) inside the repository
```kotlin
class CarRepository : ListRepository {

override fun getDataList(page: Int, pageSize : Int): Single> {
return endPoints.getCars(pageSize, page) //For Example (You need to return your data source here)
}

}
```

## In The View Model
Inehet from RxPagination\
```kotlin
class CarsViewModel(carsRepository: CarRepository) :
RxPagination(carsRepository, PAGE_SIZE, FIRST_PAGE) {

init {
loadDataList()
}

}
```
Then, you need to override onFetchDataListSubscribed,
onFetchDataListError and onFetchDataListSuccess
```kotlin
class CarsViewModel(carsRepository: CarRepository) :
RxPagination(carsRepository, PAGE_SIZE, FIRST_PAGE) {

init {
loadDataList()
}

override fun onFetchDataListSubscribed() {
//Start fetching data
}

override fun onFetchDataListError(throwable: Throwable) {

}

override fun onFetchDataListSuccess(lastLoadedList: ArrayList) {
//You don't need to handle the data, but if you need it, it's available
//This lastLoadedList is the new loaded part.
}
}
```
## In the UI (Activity or Fragment)
To observe the data list, use viewModel.dataList,
dataList is a BehaviorRelay\\>
```kotlin
viewModel
.dataList
.observeOn(AndroidSchedulers.mainThread())
.subscribe { dataList ->
// notify your list adapter here
}
```
## Load and reload data
```kotlin
viewModel.loadDataList() //To load the more data

viewModel.reloadDataList() //To reload the data
```

## Access page informations
To access cerrent page
```kotlin
viewModel.page.value //Int
```
To check if you reach the last page
```kotlin
viewModel.isLastPage.value //Boolean
```

## Contributing

We welcome contributions !
* ⇄ Pull requests and ★ Stars are always welcome.