Skip to content

Commit 108436e

Browse files
authored
RxJava 3 support (#1047)
1 parent d388efa commit 108436e

File tree

9 files changed

+131
-1
lines changed

9 files changed

+131
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
> __BREAKING CHANGES__
55
>
66
> - FIX: Corrected the `Installation` property `appVersion` to be the build version instead of the version name. This aligns the property with its equivalent in the Parse iOS SDK. See [#902](https://github.com/parse-community/Parse-SDK-Android/issues/902) for details. Thanks to [Manuel Trezza](https://github.com/mtrezza).
7+
- Added RxJava module to transform `Task`s into RxJava types.
78

89
### 1.24.2
910
- FIX: Fixed naming collission bug due to integration of bolts-tasks module. See [#1028](https://github.com/parse-community/Parse-SDK-Android/issues/1028) for details. Thanks to [Manuel Trezza](https://github.com/mtrezza)

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ dependencies {
4040
implementation "com.github.parse-community.Parse-SDK-Android:fcm:$parseVersion"
4141
// for Kotlin extensions support (optional)
4242
implementation "com.github.parse-community.Parse-SDK-Android:ktx:$parseVersion"
43+
// for Kotlin coroutines support (optional)
44+
implementation "com.github.parse-community.Parse-SDK-Android:coroutines:$parseVersion"
45+
// for RxJava support (optional)
46+
implementation "com.github.parse-community.Parse-SDK-Android:rxjava:$parseVersion"
4347
}
4448
```
4549

@@ -95,6 +99,7 @@ These are other official libraries we made that can help you create your Parse a
9599
- [Parse FCM](/fcm) - [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) support for sending push notifications.
96100
- [Parse KTX](/ktx) - Kotlin extensions for ease of developer use.
97101
- [Parse Coroutines](/coroutines) - Kotlin Coroutines support for various Parse async operations
102+
- [Parse RxJava](/rxjava) - Transform Parse `Task`s to RxJava `Completable`s and `Single`s
98103
- [ParseLiveQuery](https://github.com/parse-community/ParseLiveQuery-Android) - Realtime query subscription.
99104
- [ParseUI](https://github.com/parse-community/ParseUI-Android) - Prebuilt UI elements.
100105

rxjava/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

rxjava/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Parse SDK Android RxJava
2+
RxJava 3 support for Parse Android
3+
4+
## Dependency
5+
6+
After including JitPack:
7+
```gradle
8+
dependencies {
9+
implementation "com.github.parse-community.Parse-SDK-Android:rxjava:latest.version.here"
10+
}
11+
```
12+
13+
## Usage
14+
RxJava support is provided as an extension method on any `Task`. For example:
15+
```kotlin
16+
ParseTwitterUtils.logInInBackground(this)
17+
.toSingle()
18+
.subscribeOn(Schedulers.io())
19+
.observeOn(AndroidSchedulers.mainThread())
20+
.subscribe({
21+
Timber.d("Logged in with user ${it.objectId}"
22+
}, {
23+
Timber.e(it)
24+
})
25+
```
26+
Tasks with a Void results, ie. `Task<Void>` can be made into a `Completable`.
27+
For example:
28+
```kotlin
29+
val user = ParseUser.getCurrentUser()
30+
user.put("lastLoggedIn", System.currentTimeMillis())
31+
user.saveInBackground().toCompletable()
32+
.subscribeOn(Schedulers.io())
33+
.observeOn(AndroidSchedulers.mainThread())
34+
.subscribe({
35+
// yay
36+
Timber.d("user saved")
37+
}, {
38+
Timber.e(it)
39+
})
40+
```
41+
Note that these examples uses RxAndroid as well, which you need to add yourself as a dependency.
42+
43+
### From Java
44+
If you need to call this from Java:
45+
```java
46+
ParseUser user = ParseUser.getCurrentUser();
47+
Completable completable = ParseRxJavaUtils.toCompletable(user.saveInBackground());
48+
```
49+
You would use similar calls to create a `Single<T>`.
50+
51+
## License
52+
Copyright (c) 2015-present, Parse, LLC.
53+
All rights reserved.
54+
55+
This source code is licensed under the BSD-style license found in the
56+
LICENSE file in the root directory of this source tree. An additional grant
57+
of patent rights can be found in the PATENTS file in the same directory.

rxjava/build.gradle

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
apply plugin: "com.android.library"
2+
apply plugin: "kotlin-android"
3+
4+
android {
5+
compileSdkVersion rootProject.ext.compileSdkVersion
6+
7+
defaultConfig {
8+
minSdkVersion rootProject.ext.minSdkVersion
9+
targetSdkVersion rootProject.ext.targetSdkVersion
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
14+
packagingOptions {
15+
exclude "**/BuildConfig.class"
16+
}
17+
18+
lintOptions {
19+
abortOnError false
20+
}
21+
22+
buildTypes {
23+
release {
24+
minifyEnabled false
25+
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
26+
}
27+
}
28+
29+
}
30+
31+
dependencies {
32+
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
33+
api "io.reactivex.rxjava3:rxjava:3.0.4"
34+
implementation project(":parse")
35+
}
36+
37+
apply from: "https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.1.0/gradle-android-javadocs.gradle"

rxjava/proguard-rules.pro

Whitespace-only changes.

rxjava/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest package="com.parse.rxjava" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@file:JvmName("ParseRxJavaUtils")
2+
@file:Suppress("unused")
3+
4+
package com.parse.rxjava
5+
6+
import com.parse.boltsinternal.Task
7+
import io.reactivex.rxjava3.core.Completable
8+
import io.reactivex.rxjava3.core.Single
9+
10+
fun <T> Task<T>.toSingle(): Single<T> {
11+
return Single.defer {
12+
this.waitForCompletion()
13+
if (isFaulted) {
14+
throw error
15+
}
16+
Single.just(result)
17+
}
18+
}
19+
20+
fun Task<Void>.toCompletable(): Completable {
21+
return Completable.defer {
22+
this.waitForCompletion()
23+
if (isFaulted) {
24+
throw error
25+
}
26+
Completable.complete()
27+
}
28+
}

settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
include ':parse', ':fcm', ':gcm', ':ktx', ':coroutines', ':google', ':facebook', ':twitter', ':bolts-tasks'
1+
include ':parse', ':fcm', ':gcm', ':ktx', ':coroutines', 'rxjava', ':google', ':facebook', ':twitter', ':bolts-tasks'
22

0 commit comments

Comments
 (0)