diff --git a/CHANGELOG.md b/CHANGELOG.md index 49694b8fe..79fc3a246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ > __BREAKING CHANGES__ > > - 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). +- Added RxJava module to transform `Task`s into RxJava types. ### 1.24.2 - 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) diff --git a/README.md b/README.md index 86af2ec19..03bceefe7 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ dependencies { implementation "com.github.parse-community.Parse-SDK-Android:fcm:$parseVersion" // for Kotlin extensions support (optional) implementation "com.github.parse-community.Parse-SDK-Android:ktx:$parseVersion" + // for Kotlin coroutines support (optional) + implementation "com.github.parse-community.Parse-SDK-Android:coroutines:$parseVersion" + // for RxJava support (optional) + implementation "com.github.parse-community.Parse-SDK-Android:rxjava:$parseVersion" } ``` @@ -95,6 +99,7 @@ These are other official libraries we made that can help you create your Parse a - [Parse FCM](/fcm) - [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) support for sending push notifications. - [Parse KTX](/ktx) - Kotlin extensions for ease of developer use. - [Parse Coroutines](/coroutines) - Kotlin Coroutines support for various Parse async operations +- [Parse RxJava](/rxjava) - Transform Parse `Task`s to RxJava `Completable`s and `Single`s - [ParseLiveQuery](https://github.com/parse-community/ParseLiveQuery-Android) - Realtime query subscription. - [ParseUI](https://github.com/parse-community/ParseUI-Android) - Prebuilt UI elements. diff --git a/rxjava/.gitignore b/rxjava/.gitignore new file mode 100644 index 000000000..796b96d1c --- /dev/null +++ b/rxjava/.gitignore @@ -0,0 +1 @@ +/build diff --git a/rxjava/README.md b/rxjava/README.md new file mode 100644 index 000000000..5bfb74ea1 --- /dev/null +++ b/rxjava/README.md @@ -0,0 +1,57 @@ +# Parse SDK Android RxJava +RxJava 3 support for Parse Android + +## Dependency + +After including JitPack: +```gradle +dependencies { + implementation "com.github.parse-community.Parse-SDK-Android:rxjava:latest.version.here" +} +``` + +## Usage +RxJava support is provided as an extension method on any `Task`. For example: +```kotlin +ParseTwitterUtils.logInInBackground(this) + .toSingle() + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + Timber.d("Logged in with user ${it.objectId}" + }, { + Timber.e(it) + }) +``` +Tasks with a Void results, ie. `Task` can be made into a `Completable`. +For example: +```kotlin +val user = ParseUser.getCurrentUser() +user.put("lastLoggedIn", System.currentTimeMillis()) +user.saveInBackground().toCompletable() + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + // yay + Timber.d("user saved") + }, { + Timber.e(it) + }) +``` +Note that these examples uses RxAndroid as well, which you need to add yourself as a dependency. + +### From Java +If you need to call this from Java: +```java +ParseUser user = ParseUser.getCurrentUser(); +Completable completable = ParseRxJavaUtils.toCompletable(user.saveInBackground()); +``` +You would use similar calls to create a `Single`. + +## License + Copyright (c) 2015-present, Parse, LLC. + All rights reserved. + + This source code is licensed under the BSD-style license found in the + LICENSE file in the root directory of this source tree. An additional grant + of patent rights can be found in the PATENTS file in the same directory. diff --git a/rxjava/build.gradle b/rxjava/build.gradle new file mode 100644 index 000000000..e1c160fc3 --- /dev/null +++ b/rxjava/build.gradle @@ -0,0 +1,37 @@ +apply plugin: "com.android.library" +apply plugin: "kotlin-android" + +android { + compileSdkVersion rootProject.ext.compileSdkVersion + + defaultConfig { + minSdkVersion rootProject.ext.minSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion + versionCode 1 + versionName "1.0" + } + + packagingOptions { + exclude "**/BuildConfig.class" + } + + lintOptions { + abortOnError false + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" + } + } + +} + +dependencies { + api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + api "io.reactivex.rxjava3:rxjava:3.0.4" + implementation project(":parse") +} + +apply from: "https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.1.0/gradle-android-javadocs.gradle" diff --git a/rxjava/proguard-rules.pro b/rxjava/proguard-rules.pro new file mode 100644 index 000000000..e69de29bb diff --git a/rxjava/src/main/AndroidManifest.xml b/rxjava/src/main/AndroidManifest.xml new file mode 100644 index 000000000..f25ee64d9 --- /dev/null +++ b/rxjava/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + diff --git a/rxjava/src/main/java/com/parse/rxjava/Extensions.kt b/rxjava/src/main/java/com/parse/rxjava/Extensions.kt new file mode 100644 index 000000000..65dad4bf3 --- /dev/null +++ b/rxjava/src/main/java/com/parse/rxjava/Extensions.kt @@ -0,0 +1,28 @@ +@file:JvmName("ParseRxJavaUtils") +@file:Suppress("unused") + +package com.parse.rxjava + +import com.parse.boltsinternal.Task +import io.reactivex.rxjava3.core.Completable +import io.reactivex.rxjava3.core.Single + +fun Task.toSingle(): Single { + return Single.defer { + this.waitForCompletion() + if (isFaulted) { + throw error + } + Single.just(result) + } +} + +fun Task.toCompletable(): Completable { + return Completable.defer { + this.waitForCompletion() + if (isFaulted) { + throw error + } + Completable.complete() + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 3adc5d6a5..93b9b130f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,2 @@ -include ':parse', ':fcm', ':gcm', ':ktx', ':coroutines', ':google', ':facebook', ':twitter', ':bolts-tasks' +include ':parse', ':fcm', ':gcm', ':ktx', ':coroutines', 'rxjava', ':google', ':facebook', ':twitter', ':bolts-tasks'