Skip to content

Commit 39e0da3

Browse files
oliveiradevJawnnypoo
authored andcommitted
Feature/coroutines support (#958)
* 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
1 parent 3451435 commit 39e0da3

File tree

10 files changed

+196
-1
lines changed

10 files changed

+196
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ We want to make contributing to this project as easy and transparent as possible
7777

7878
- [Parse FCM](/fcm)
7979
- [Parse KTX](/ktx)
80+
- [Parse Coroutines](/coroutines)
8081
- [ParseUI](https://github.com/parse-community/ParseUI-Android)
8182
- [ParseLiveQuery](https://github.com/parse-community/ParseLiveQuery-Android)
8283
- [ParseFacebookUtils](https://github.com/parse-community/ParseFacebookUtils-Android)

coroutines/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

coroutines/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Parse SDK Android Coroutines
2+
Kotlin coroutines support for Parse Android
3+
4+
## Setup
5+
6+
### Installation
7+
After including JitPack:
8+
9+
```groovy
10+
dependencies {
11+
implementation "com.github.parse-community.Parse-SDK-Android:coroutines:latest.version.here"
12+
}
13+
```
14+
15+
## Use Parse Coroutines
16+
17+
### ParseQuery
18+
19+
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:
20+
21+
```kotlin
22+
launch { // Coroutine builder
23+
val cat = ParseQuery.getQuery(...).find()
24+
// get cats without callback
25+
}
26+
```
27+
We use a coroutine builder because `find()` is a suspend function.
28+
29+
### ParseCloud
30+
31+
We can call cloud function inline:
32+
33+
```kotlin
34+
launch { // Coroutine builder
35+
val catThumb = callCloudFunction("getThumbnail", mapOf("url" to "https://cat.jpg"))
36+
// get cats without callback
37+
}
38+
```
39+
40+
### ParseUser
41+
42+
SignUp:
43+
44+
```kotlin
45+
launch { // Coroutine builder
46+
user = ParseUser().apply {
47+
setUsername("my name")
48+
setPassword("my pass")
49+
setEmail("[email protected]")
50+
}.also {
51+
signUp()
52+
}
53+
}
54+
```
55+
Login:
56+
57+
```kotlin
58+
launch { // Coroutine builder
59+
val user = parseLogIn("username", "password")
60+
}
61+
```
62+
63+
## Contributing
64+
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.
65+
66+
## License
67+
Copyright (c) 2015-present, Parse, LLC.
68+
All rights reserved.
69+
70+
This source code is licensed under the BSD-style license found in the
71+
LICENSE file in the root directory of this source tree. An additional grant
72+
of patent rights can be found in the PATENTS file in the same directory.

coroutines/build.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
14+
}
15+
16+
lintOptions {
17+
abortOnError false
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
27+
}
28+
29+
dependencies {
30+
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31+
api 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.2'
32+
api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.2'
33+
implementation project(':parse')
34+
}
35+
36+
apply from: 'https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.1.0/gradle-android-javadocs.gradle'

coroutines/proguard-rules.pro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
2+
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
3+
-keepclassmembernames class kotlinx.** {
4+
volatile <fields>;
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest package="com.parse.coroutines" />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@file:JvmName("ParseCloudCoroutinesExtensions")
2+
3+
package com.parse.coroutines
4+
5+
import com.parse.ParseCloud
6+
import kotlin.coroutines.resume
7+
import kotlin.coroutines.resumeWithException
8+
import kotlin.coroutines.suspendCoroutine
9+
10+
suspend fun <T> callCloudFunction(functionName: String, params: Map<String, Any>): T {
11+
return suspendCoroutine { continuation ->
12+
ParseCloud.callFunctionInBackground<T>(functionName, params) { result, e ->
13+
if (e == null) continuation.resume(result)
14+
else continuation.resumeWithException(e)
15+
}
16+
}
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@file:JvmName("ParseQueryCoroutinesExtensions")
2+
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")
3+
4+
package com.parse.coroutines
5+
6+
import com.parse.ParseObject
7+
import com.parse.ParseQuery
8+
import kotlinx.coroutines.suspendCancellableCoroutine
9+
import kotlin.coroutines.resume
10+
import kotlin.coroutines.resumeWithException
11+
12+
suspend fun <T : ParseObject> ParseQuery<T>.find(): List<T> {
13+
return suspendCancellableCoroutine { continuation ->
14+
continuation.invokeOnCancellation {
15+
cancel()
16+
}
17+
18+
findInBackground { objects, e ->
19+
if (e == null) continuation.resume(objects)
20+
else continuation.resumeWithException(e)
21+
}
22+
}
23+
}
24+
25+
suspend fun <T : ParseObject> ParseQuery<T>.count(): Int {
26+
return suspendCancellableCoroutine { continuation ->
27+
continuation.invokeOnCancellation {
28+
cancel()
29+
}
30+
31+
countInBackground { count, e ->
32+
if (e == null) continuation.resume(count)
33+
else continuation.resumeWithException(e)
34+
}
35+
}
36+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@file:JvmName("ParseUserCoroutinesExtensions")
2+
3+
package com.parse.coroutines
4+
5+
import com.parse.ParseUser
6+
import kotlin.coroutines.resume
7+
import kotlin.coroutines.resumeWithException
8+
import kotlin.coroutines.suspendCoroutine
9+
10+
suspend fun ParseUser.singUp(): ParseUser {
11+
return suspendCoroutine { continuation ->
12+
signUpInBackground { e ->
13+
if (e == null) continuation.resume(this)
14+
else continuation.resumeWithException(e)
15+
}
16+
}
17+
}
18+
19+
suspend fun parseLogIn(username: String, password: String): ParseUser {
20+
return suspendCoroutine { continuation ->
21+
ParseUser.logInInBackground(username, password) { user, e ->
22+
if (e == null) continuation.resume(user)
23+
else continuation.resumeWithException(e)
24+
}
25+
}
26+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
include ':parse', ':fcm', ':gcm', ':ktx'
1+
include ':parse', ':fcm', ':gcm', ':ktx', ':coroutines'
22

0 commit comments

Comments
 (0)