-
-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Coroutines suport Call parse query find as a suspend function Call login and singup as a suspend function Call cloud function as a suspend function * Add coroutines README link at root project
- Loading branch information
1 parent
3451435
commit 39e0da3
Showing
10 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Parse SDK Android Coroutines | ||
Kotlin coroutines support for Parse Android | ||
|
||
## Setup | ||
|
||
### Installation | ||
After including JitPack: | ||
|
||
```groovy | ||
dependencies { | ||
implementation "com.github.parse-community.Parse-SDK-Android:coroutines:latest.version.here" | ||
} | ||
``` | ||
|
||
## Use Parse Coroutines | ||
|
||
### ParseQuery | ||
|
||
Now we can call a parse query using a synchronous style, this is possible when we use coroutines. We need to use a regular coroutine builder: | ||
|
||
```kotlin | ||
launch { // Coroutine builder | ||
val cat = ParseQuery.getQuery(...).find() | ||
// get cats without callback | ||
} | ||
``` | ||
We use a coroutine builder because `find()` is a suspend function. | ||
|
||
### ParseCloud | ||
|
||
We can call cloud function inline: | ||
|
||
```kotlin | ||
launch { // Coroutine builder | ||
val catThumb = callCloudFunction("getThumbnail", mapOf("url" to "https://cat.jpg")) | ||
// get cats without callback | ||
} | ||
``` | ||
|
||
### ParseUser | ||
|
||
SignUp: | ||
|
||
```kotlin | ||
launch { // Coroutine builder | ||
user = ParseUser().apply { | ||
setUsername("my name") | ||
setPassword("my pass") | ||
setEmail("email@example.com") | ||
}.also { | ||
signUp() | ||
} | ||
} | ||
``` | ||
Login: | ||
|
||
```kotlin | ||
launch { // Coroutine builder | ||
val user = parseLogIn("username", "password") | ||
} | ||
``` | ||
|
||
## Contributing | ||
When contributing to the `coroutines` module, please first consider if the extension function you are wanting to add would potentially be better suited in the main `parse` module. If it is something specific to Kotlin users or only useful in a Kotlin project, feel free to make a PR adding it to this module. Otherwise, consider adding the addition to the `parse` module itself, so that it is still usable in Java. | ||
|
||
## 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
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 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.2' | ||
api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.2' | ||
implementation project(':parse') | ||
} | ||
|
||
apply from: 'https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.1.0/gradle-android-javadocs.gradle' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {} | ||
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {} | ||
-keepclassmembernames class kotlinx.** { | ||
volatile <fields>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest package="com.parse.coroutines" /> |
17 changes: 17 additions & 0 deletions
17
coroutines/src/main/java/com/parse/coroutines/ParseCloudCoroutinesExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@file:JvmName("ParseCloudCoroutinesExtensions") | ||
|
||
package com.parse.coroutines | ||
|
||
import com.parse.ParseCloud | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
suspend fun <T> callCloudFunction(functionName: String, params: Map<String, Any>): T { | ||
return suspendCoroutine { continuation -> | ||
ParseCloud.callFunctionInBackground<T>(functionName, params) { result, e -> | ||
if (e == null) continuation.resume(result) | ||
else continuation.resumeWithException(e) | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
coroutines/src/main/java/com/parse/coroutines/ParseQueryCoroutinesExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@file:JvmName("ParseQueryCoroutinesExtensions") | ||
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER") | ||
|
||
package com.parse.coroutines | ||
|
||
import com.parse.ParseObject | ||
import com.parse.ParseQuery | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
suspend fun <T : ParseObject> ParseQuery<T>.find(): List<T> { | ||
return suspendCancellableCoroutine { continuation -> | ||
continuation.invokeOnCancellation { | ||
cancel() | ||
} | ||
|
||
findInBackground { objects, e -> | ||
if (e == null) continuation.resume(objects) | ||
else continuation.resumeWithException(e) | ||
} | ||
} | ||
} | ||
|
||
suspend fun <T : ParseObject> ParseQuery<T>.count(): Int { | ||
return suspendCancellableCoroutine { continuation -> | ||
continuation.invokeOnCancellation { | ||
cancel() | ||
} | ||
|
||
countInBackground { count, e -> | ||
if (e == null) continuation.resume(count) | ||
else continuation.resumeWithException(e) | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
coroutines/src/main/java/com/parse/coroutines/ParseUserCoroutinesExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@file:JvmName("ParseUserCoroutinesExtensions") | ||
|
||
package com.parse.coroutines | ||
|
||
import com.parse.ParseUser | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
suspend fun ParseUser.singUp(): ParseUser { | ||
return suspendCoroutine { continuation -> | ||
signUpInBackground { e -> | ||
if (e == null) continuation.resume(this) | ||
else continuation.resumeWithException(e) | ||
} | ||
} | ||
} | ||
|
||
suspend fun parseLogIn(username: String, password: String): ParseUser { | ||
return suspendCoroutine { continuation -> | ||
ParseUser.logInInBackground(username, password) { user, e -> | ||
if (e == null) continuation.resume(user) | ||
else continuation.resumeWithException(e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
include ':parse', ':fcm', ':gcm', ':ktx' | ||
include ':parse', ':fcm', ':gcm', ':ktx', ':coroutines' | ||
|