Skip to content

Commit 4030884

Browse files
authored
Add KTX module (#860)
* Add KTX module * Add contribution note * Add a few other extensions related to primative support
1 parent ca91d87 commit 4030884

File tree

11 files changed

+207
-9
lines changed

11 files changed

+207
-9
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ dependencies {
2424
implementation "com.github.parse-community.Parse-SDK-Android:parse:latest.version.here"
2525
// for FCM Push support (optional)
2626
implementation "com.github.parse-community.Parse-SDK-Android:fcm:latest.version.here"
27+
// for Kotlin extensions support (optional)
28+
implementation "com.github.parse-community.Parse-SDK-Android:ktx:latest.version.here"
2729
}
2830
```
2931

build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
buildscript {
2+
ext.kotlin_version = '1.2.51'
23
repositories {
34
jcenter()
45
google()
@@ -7,6 +8,7 @@ buildscript {
78
classpath 'com.android.tools.build:gradle:3.1.3'
89
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.2'
910
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
11+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1012
}
1113
}
1214

fcm/README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ FCM support for Parse Android apps
44
## Setup
55

66
### Installation
7-
8-
Add dependency to the application level `build.gradle` file.
7+
After including JitPack:
98

109
```groovy
1110
dependencies {
12-
implementation 'com.parse:parse-android:latest.version.here'
13-
implementation 'com.parse:parse-fcm-android:latest.version.here'
11+
implementation "com.github.parse-community.Parse-SDK-Android:parse:latest.version.here"
12+
implementation "com.github.parse-community.Parse-SDK-Android:fcm:latest.version.here"
1413
}
1514
```
1615
Then, follow Google's docs for [setting up an Firebase app](https://firebase.google.com/docs/android/setup). Although the steps are different for setting up FCM with Parse, it is also a good idea to read over the [Firebase FCM Setup](https://firebase.google.com/docs/cloud-messaging/android/client).

gcm/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ Please note that GCM is deprecated in favor of FCM. This module exists as a back
77
## Setup
88

99
### Installation
10-
11-
Add dependency to the application level `build.gradle` file.
10+
After including JitPack:
1211

1312
```groovy
1413
dependencies {
15-
implementation 'com.parse:parse-android:latest.version.here'
16-
implementation 'com.parse:parse-gcm-android:latest.version.here'
14+
implementation "com.github.parse-community.Parse-SDK-Android:parse:latest.version.here"
15+
implementation "com.github.parse-community.Parse-SDK-Android:gcm:latest.version.here"
1716
}
1817
```
18+
1919
You will then need to register some things in your manifest, firstly, the GCM sender ID:
2020
```xml
2121
<meta-data

ktx/.gitignore

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

ktx/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Parse SDK Android KTX
2+
Kotlin extension 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:ktx:latest.version.here"
12+
}
13+
```
14+
Then, you will be able to use the extension functions. For example:
15+
```kotlin
16+
/**
17+
* A cat, ParseObject subclass. Register with ParseObject.registerSubclass(Cat::class.java)
18+
*/
19+
@ParseClassName(Cat.NAME)
20+
class Cat : ParseObject() {
21+
22+
companion object {
23+
const val NAME = "Cat"
24+
25+
const val KEY_NAME = "name"
26+
const val KEY_AGE = "age"
27+
}
28+
29+
var name: String?
30+
get() = getString(KEY_NAME)
31+
set(value) = putOrIgnore(KEY_NAME, value)
32+
33+
var age: Int?
34+
get() = getInt(KEY_AGE)
35+
set(value) = putOrRemove(KEY_AGE, value)
36+
}
37+
38+
```
39+
40+
## Contributing
41+
When contributing to the `ktx` 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.
42+
43+
## License
44+
Copyright (c) 2015-present, Parse, LLC.
45+
All rights reserved.
46+
47+
This source code is licensed under the BSD-style license found in the
48+
LICENSE file in the root directory of this source tree. An additional grant
49+
of patent rights can be found in the PATENTS file in the same directory.

ktx/build.gradle

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply plugin: 'com.android.library'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
android {
6+
compileSdkVersion rootProject.ext.compileSdkVersion
7+
8+
defaultConfig {
9+
minSdkVersion rootProject.ext.minSdkVersion
10+
targetSdkVersion rootProject.ext.targetSdkVersion
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
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+
implementation project(':parse')
34+
}
35+
36+
apply from: 'https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.1.0/gradle-android-javadocs.gradle'

ktx/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

ktx/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest package="com.parse.ktx" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
@file:Suppress("unused")
2+
3+
package com.parse.ktx
4+
5+
import com.parse.ParseObject
6+
7+
/**
8+
* Get the object as the passed value. This has a chance of throwing an exception if the object
9+
* cannot be cast properly
10+
* @param key the key
11+
* @return the value
12+
*/
13+
@Suppress("UNCHECKED_CAST")
14+
fun <T> ParseObject.getAs(key: String): T = get(key) as T
15+
16+
/**
17+
* Get the object optionally as null. This has a chance of throwing an exception if the object
18+
* cannot be cast properly
19+
* @param key the key
20+
* @return the value, or null if nothing is there
21+
*/
22+
@Suppress("UNCHECKED_CAST")
23+
fun <T> ParseObject.getAsOrNull(key: String): T? {
24+
if (containsKey(key)) {
25+
return get(key) as T?
26+
}
27+
return null
28+
}
29+
30+
/**
31+
* [ParseObject.put] the value, doing nothing if the value is null
32+
*/
33+
fun ParseObject.putOrIgnore(key: String, value: Any?) {
34+
if (value != null) {
35+
put(key, value)
36+
}
37+
}
38+
39+
/**
40+
* [ParseObject.put] the value, or [ParseObject.remove] it if the value is null
41+
*/
42+
fun ParseObject.putOrRemove(key: String, value: Any?) {
43+
if (value == null) {
44+
remove(key)
45+
} else {
46+
put(key, value)
47+
}
48+
}
49+
50+
/**
51+
* Get the boolean optionally as null
52+
* @param key the key
53+
* @return the value, or null if nothing is there
54+
*/
55+
fun ParseObject.getBooleanOrNull(key: String): Boolean? {
56+
if (containsKey(key)) {
57+
return getBoolean(key)
58+
}
59+
return null
60+
}
61+
62+
/**
63+
* Get the int optionally as null
64+
* @param key the key
65+
* @return the value, or null if nothing is there
66+
*/
67+
fun ParseObject.getIntOrNull(key: String): Int? {
68+
return getNumber(key)?.toInt()
69+
}
70+
71+
/**
72+
* Get the long optionally as null
73+
* @param key the key
74+
* @return the value, or null if nothing is there
75+
*/
76+
fun ParseObject.getLongOrNull(key: String): Long? {
77+
return getNumber(key)?.toLong()
78+
}
79+
80+
/**
81+
* Get the double optionally as null
82+
* @param key the key
83+
* @return the value, or null if nothing is there
84+
*/
85+
fun ParseObject.getDoubleOrNull(key: String): Double? {
86+
return getNumber(key)?.toDouble()
87+
}

settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
include ':parse', ':fcm', ':gcm'
1+
include ':parse', ':fcm', ':gcm', ':ktx'
22

0 commit comments

Comments
 (0)