Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Gorbunov committed Jan 29, 2020
2 parents dbe9022 + 937a6df commit f58a315
Show file tree
Hide file tree
Showing 67 changed files with 3,770 additions and 8 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 MobileUp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
158 changes: 158 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Reactive Paging and Loading

[ ![Download](https://api.bintray.com/packages/1117847002272/RxPagingLoading/RxPagingLoading/images/download.svg?version=1.0.0) ](https://bintray.com/1117847002272/RxPagingLoading/RxPagingLoading/1.0.0/link)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

This library implements reactive paging and loading.

It helps to handle the states of loading a simple data (LCE - loading/content/error) or the complex states of lists with pagination (PLCE - paging/loading/content/error).

The solution is based on the usage of Unidirectional Data Flow pattern.

The library depends on RxJava, so you will find familiar interfaces in it's API.

## Dependency
Add the dependency to your build.gradle:
```Groovy
dependencies {
implementation 'ru.mobileup:rxpagingloading:1.0.0'
}
```

## Loading a simple data

`Loading` interface looks as follows:

```Kotlin
interface Loading<T> {

enum class Action { REFRESH, FORCE_REFRESH }

val state: Observable<State<T>>

val actions: Consumer<Action>

data class State<T>(
val content: T? = null,
val loading: Boolean = false,
val error: Throwable? = null
)
}
```

It includes:
- `State` class — represents LCE state.
- `Action` enum - the possible actions.
- `state: Observable` - observes changes of the LCE state.
- `actions: Consumer` - receives performed Action.

There are two implementations of this interface:

### LoadingOrdinary

This class is for simple case when at first load or after refresh content comes from `Single` data source, passed into the constructor:

```Kotlin
LoadingOrdinary(
source = Single.just("Content string")
)
```

### LoadingAssembled

This implementation is for case when there is a separate `Сompletable` to refresh the content and an `Observable` stream for receiving this content updates:

```Kotlin
LoadingAssembled(
refresh = repository.refreshDataCompletable(),
updates = repository.dataChangesObservable()
)
```

## Paging

The `Paging` interface looks a bit more complicated. In addition to the LCE, it has the paging states:

```Kotlin
interface Paging<T> {

enum class Action { REFRESH, FORCE_REFRESH, LOAD_NEXT_PAGE }

val state: Observable<State<T>>

val actions: Consumer<Action>

data class State<T>(
val content: List<T>? = null,
val loading: Boolean = false,
val error: Throwable? = null,
val pageLoading: Boolean = false,
val pageError: Throwable? = null,
val lastPage: Page<T>? = null
) {
val isEndReached: Boolean get() = lastPage?.isEndReached ?: false
}

interface Page<T> {
val items: List<T>
val lastItem: T? get() = items.lastOrNull()
val isEndReached: Boolean
}
}
```

Note, the `State` also stores the last loaded page. It is used to download the following page, as well as to determine the end of the list.

`Page` is an interface made for flexibility. Your data source can map a page data to it's own class. For example, you can store an identifier of the last entity, or a link to the next page, or any data depending on your back-end requirements. The last page will be passed to a lambda `pageSource` from the constructor of the `PagingImpl`:

```Kotlin
class PageInfo(
override val items: List<Item>,
override val isEndReached: Boolean
lastItemId: Int
) : Paging.Page<Item>

PagingImpl(
pageSource = { offset, lastPage ->
repository
.loadPage(lastItemId = lastPage?.lastItemId)
.map {
PageInfo(
items = it.list,
isEndReached = (offset + it.list.size) == it.totalCount
it.lastItemId
)
}
}
)
```

## Display the state
You can just use the resulting PLCE or LCE state to render your screen UI. Or you can use extensions from `LoadingExtensions.kt` and `PagingExtensions.kt` to observe individual state parts changes. It's helpful when you don't need all of the states or use with MVVM-like pattern.

In the [sample](https://github.com/MobileUpLLC/RxPagingLoading/tree/develop/sample) we use the [RxPM](https://github.com/dmdevgo/RxPM) library and extensions to split resulting state to the Presentation Model states.

## License
```
MIT License
Copyright (c) 2019 MobileUp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
15 changes: 10 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.30'

apply from: 'dependencies.gradle'

repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath gradlePlugins.android
classpath gradlePlugins.kotlin

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

classpath gradlePlugins.bintray
classpath gradlePlugins.maven
}
}

allprojects {
repositories {
google()
jcenter()

}
}

Expand Down
51 changes: 51 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
ext.versions = [

'minSdk' : 21,
'compileSdk' : 29,
'targetSdk' : 29,

'bintrayPlugin': '1.8.4',
'mavenPlugin': '1.5',

'androidGradlePlugin': '3.5.3',
'kotlin' : '1.3.61',

'junit' : '4.12',

'rxJava' : '2.2.17',
'rxAndroid' : '2.1.1',
'rxPm' : '2.0',
'rxRelay' : '2.1.1',
'rxBinding' : '3.1.0',

'appCompat' : '1.1.0',
'recyclerView' : '1.1.0',

'mockito' : '2.2.0'
]

ext.libraries = [
junit : "junit:junit:${versions.junit}",
kotlinStdLib : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}",

appCompat : "androidx.appcompat:appcompat:${versions.appCompat}",
recyclerView : "androidx.recyclerview:recyclerview:${versions.recyclerView}",

rxJava : "io.reactivex.rxjava2:rxjava:${versions.rxJava}",
rxAndroid : "io.reactivex.rxjava2:rxandroid:${versions.rxAndroid}",
rxPm : "me.dmdev.rxpm:rxpm:${versions.rxPm}",
rxRelay : "com.jakewharton.rxrelay2:rxrelay:${versions.rxRelay}",
rxBinding : "com.jakewharton.rxbinding3:rxbinding:${versions.rxBinding}",
rxBindingSwiperefreshlayout: "com.jakewharton.rxbinding3:rxbinding-swiperefreshlayout:${versions.rxBinding}",

junitKotlin : "org.jetbrains.kotlin:kotlin-test-junit:$versions.kotlin",
mockitoKotlin : "com.nhaarman.mockitokotlin2:mockito-kotlin:$versions.mockito"
]

ext.gradlePlugins = [
bintray : "com.jfrog.bintray.gradle:gradle-bintray-plugin:$versions.bintrayPlugin",
maven : "com.github.dcendents:android-maven-gradle-plugin:$versions.mavenPlugin",

android: "com.android.tools.build:gradle:$versions.androidGradlePlugin",
kotlin : "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
]
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Apr 18 16:29:15 MSK 2019
#Thu Aug 22 15:19:04 MSK 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
1 change: 1 addition & 0 deletions rxpagingloading/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
50 changes: 50 additions & 0 deletions rxpagingloading/bintray.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apply plugin: 'com.jfrog.bintray'

version = libraryVersion

if (project.hasProperty("android")) { // Android libraries
task sourcesJar(type: Jar) {
archiveClassifier = 'sources'
from android.sourceSets.main.java.srcDirs
}

} else { // Java libraries
task sourcesJar(type: Jar, dependsOn: classes) {
archiveClassifier = 'sources'
from sourceSets.main.allSource
}
}

artifacts {
archives sourcesJar
}

// Bintray
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")

configurations = ['archives']
pkg {
repo = bintrayRepo
name = bintrayName
desc = libraryDescription
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = allLicenses
userOrg = properties.getProperty("bintray.userOrg")
publish = true
publicDownloadNumbers = true
version {
desc = libraryDescription
gpg {
sign = true //Determines whether to GPG sign the files. The default is false
passphrase = properties.getProperty("bintray.gpg.password")
//Optional. The passphrase for GPG signing'
}
}
}
}
Loading

0 comments on commit f58a315

Please sign in to comment.