Skip to content

Commit b677dd7

Browse files
authored
Kotlin java union (#13)
* rename example to example_javaOnly * add example_kotlinOnly * update to plugin style for annotation processor * remove nullable and notnull annotations * fix activity and make testrecord open * quick fix * ignore valueof warning and add example type info in log tag * update to official version of android gradle plugin * add example_kotlinJavaMix * finished kotlinJavaMix * remove "redundant public modifier" * Cleanup test imports * Cleanup example records * fix some warnings see: https://stackoverflow.com/questions/47833237/android-studio-shows-kotlin-dependency-warning-after-second-build * Update Readme.md : change code sections * Readme.md: fix 'App module build file dependencies' * Readme.md : fix typo @SkipGenerationOfRealmFieldNames to @SkipGenerationOfRealmFieldName * Readme.md: wrong annotation * Readme.md: start spliting usages JavaOnly first * Readme.md: spliting usages KotlinOnly * Readme.md: fix unnecessary non-null cast * readme.md: adjust indent of kotlin Example Queries
1 parent c2133c5 commit b677dd7

File tree

59 files changed

+822
-30
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+822
-30
lines changed

README.md

Lines changed: 107 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@ realm.where(Person.class).equalTo(PersonFieldNames.FIRST_NAME, "Sally").findFirs
2525
RealmTypeSafeQuery.with(realm).where(Person.class).equalTo(PersonFields.FIRST_NAME, "Sally").findFirst();
2626
```
2727

28-
## How to include
28+
## How to include _JavaOnly project_
2929

3030
#### In your top level build file, add the jitpack repository along with realm
3131
```groovy
3232
buildscript {
3333
dependencies {
34-
classpath "io.realm:realm-gradle-plugin:4.3.1" // supported version of realm
34+
classpath "io.realm:realm-gradle-plugin:4.3.4" // supported version of realm
3535
}
3636
}
3737
3838
allprojects {
3939
repositories {
4040
jcenter()
41+
google()
4142
maven { url "https://jitpack.io" } // RTSQ is hosted on jitpack
4243
}
4344
}
@@ -46,17 +47,23 @@ allprojects {
4647
#### App module build file dependencies:
4748
```groovy
4849
apply plugin: 'realm-android' // realm setup at top of file
49-
50-
// requires java 8
51-
compileOptions {
52-
sourceCompatibility JavaVersion.VERSION_1_8
53-
targetCompatibility JavaVersion.VERSION_1_8
50+
android {
51+
...[elided]...
52+
// requires java 8
53+
compileOptions {
54+
sourceCompatibility JavaVersion.VERSION_1_8
55+
targetCompatibility JavaVersion.VERSION_1_8
56+
}
57+
...[elided]...
5458
}
5559
5660
dependencies {
57-
compileOnly 'com.github.quarkworks.RealmTypeSafeQuery-Android:annotations:{{version_number}}' // annotations
58-
annotationProcessor 'com.github.quarkworks.RealmTypeSafeQuery-Android:annotationprocessor:{{version_number}}' // annotation processor
59-
implementation 'com.github.quarkworks.RealmTypeSafeQuery-Android:query:{{version_number}}' // query dsl
61+
...[elided]...
62+
compileOnly "com.github.quarkworks.RealmTypeSafeQuery-Android:annotations:$RTSQ_version" // annotations
63+
annotationProcessor "com.github.quarkworks.RealmTypeSafeQuery-Android:annotationprocessor:$RTSQ_version" // annotation processor
64+
implementation "com.github.quarkworks.RealmTypeSafeQuery-Android:query:$RTSQ_version" // query dsl
65+
...[elided]...
66+
}
6067
```
6168

6269
#### Example Model
@@ -72,7 +79,7 @@ class Person extends RealmObject {
7279

7380
// If what pops out of the code generator doesn't compile add these annotations.
7481
// Realm constantly updates their api and RTSQ might be a little behind.
75-
@SkipGenerationOfRealmFieldNames
82+
@SkipGenerationOfRealmFieldName
7683
@SkipGenerationOfRealmField
7784
RealmList<String> website;
7885
}
@@ -103,20 +110,96 @@ RealmResults<Person> peopleWithHeavyPets = RealmTypeSafeQuery.with(realm).where(
103110
.greaterThan(PersonFields.PETS.link(PetFields.WEIGHT), 9000).findAll();
104111
```
105112

106-
#### Bonus
107-
108-
```java
113+
## How to include _KotlinOnly project_
109114

110-
final Realm realm = ...
115+
#### In your top level build file, add the jitpack repository along with realm
116+
```groovy
117+
buildscript {
118+
dependencies {
119+
classpath "io.realm:realm-gradle-plugin:4.3.4" // supported version of realm
120+
}
121+
}
122+
123+
allprojects {
124+
repositories {
125+
jcenter()
126+
google()
127+
maven { url "https://jitpack.io" } // RTSQ is hosted on jitpack
128+
}
129+
}
130+
```
131+
132+
#### App module build file dependencies:
133+
```groovy
134+
apply plugin: 'kotlin-android'
135+
apply plugin: 'kotlin-kapt'
136+
apply plugin: 'realm-android' // realm setup at top of file but below 'kotlin-kapt'
137+
138+
android {
139+
...[elided]...
140+
// requires java 8 (android build issue)
141+
compileOptions {
142+
sourceCompatibility JavaVersion.VERSION_1_8
143+
targetCompatibility JavaVersion.VERSION_1_8
144+
}
145+
...[elided]...
146+
}
111147
112-
// For chainable sorting
113-
RealmTypeSafeQuery.with(realm).where(ExampleModel.class).sort(field1).sort(field3).sort(field2).findAll();
148+
dependencies {
149+
...[elided]...
150+
compileOnly "com.github.quarkworks.RealmTypeSafeQuery-Android:annotations:$RTSQ_version" // annotations
151+
// use kapt not annotationProcessor
152+
kapt "com.github.quarkworks.RealmTypeSafeQuery-Android:annotationprocessor:$RTSQ_version" // annotation processor
153+
implementation "com.github.quarkworks.RealmTypeSafeQuery-Android:query:$RTSQ_version" // query dsl
154+
...[elided]...
155+
}
156+
```
157+
158+
#### Example Model
159+
```kotlin
160+
@GenerateRealmFields // Generates a file called PersonFields.java. This is a RTSQ annotation.
161+
@GenerateRealmFieldNames // Generates a file called PersonFieldNames.java This is a RTSQ annotation.
162+
// Kotlin classes are final by default (notice open)
163+
open class Person : RealmObject() {
164+
var firstName: String? = null
165+
var lastName: String? = null
166+
var birthday: Date? = null
114167

115-
// For creating query groups with lambdas
116-
RealmTypeSafeQuery.with(realm).where(ExampleModel.class).group((query) -> {}).findAll();
117-
RealmTypeSafeQuery.with(realm).where(ExampleModel.class).or((query) -> {}).findAll();
118-
119-
// For those pesky CSV fields that have a delimiter
120-
final String delimiter = ",";
121-
RealmTypeSafeQuery.with(realm).where(ExampleModel.class).contains(field, value, delimiter).findAll();
168+
var pets: RealmList<Pet>? = null
169+
170+
// If what pops out of the code generator doesn't compile add these annotations.
171+
// Realm constantly updates their api and RTSQ might be a little behind.
172+
@SkipGenerationOfRealmFieldName
173+
@SkipGenerationOfRealmField
174+
var website: RealmList<String>? = null
175+
}
176+
177+
@GenerateRealmFields // Generates a file called PetFields.java.
178+
@GenerateRealmFieldNames // Generates a file called PetFieldNames.java.
179+
open class Pet : RealmObject() {
180+
var name: String? = null
181+
var weight: Int? = null
182+
}
183+
```
184+
185+
#### Example Queries
186+
187+
```kotlin
188+
Realm.init(this.applicationContext)
189+
190+
Realm.getDefaultInstance().use { realm ->
191+
realm.executeTransaction { realm ->
192+
193+
val sallyNotSmiths = RealmTypeSafeQuery.with(realm).where(Person::class.java)
194+
.equalTo(PersonFields.FIRST_NAME, "Sally")
195+
.notEqualTo(PersonFields.LAST_NAME, "Smith", Case.INSENSITIVE)
196+
.lessThan(PersonFields.BIRTHDAY, Date())
197+
.findAllSorted(PersonFields.BIRTHDAY, Sort.ASCENDING)
198+
199+
//Link queries also work too
200+
201+
val peopleWithHeavyPets = RealmTypeSafeQuery.with(realm).where(Person::class.java)
202+
.greaterThan(PersonFields.PETS.link(PetFields.WEIGHT), 9000).findAll()
203+
}
204+
}
122205
```

annotationprocessor/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
apply plugin: 'java'
22
apply plugin: 'kotlin'
3+
apply plugin: 'kotlin-kapt'
34
apply plugin: 'maven' // jitpack.io
45

56
buildscript {

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.2.0'
5-
ext.realm_version = '4.3.1'
4+
ext.kotlin_version = '1.2.20'
5+
ext.realm_version = '4.3.4'
66
ext.sdk_version = 25
77
ext.build_tools_version = '27.0.3'
88
ext.support_library_version = '25.1.1'
@@ -13,7 +13,7 @@ buildscript {
1313
}
1414

1515
dependencies {
16-
classpath 'com.android.tools.build:gradle:3.1.0-rc02'
16+
classpath 'com.android.tools.build:gradle:3.1.0'
1717
//noinspection DifferentKotlinGradleVersion
1818
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1919
classpath "io.realm:realm-gradle-plugin:$realm_version"
File renamed without changes.

example/build.gradle renamed to example_javaOnly/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ android {
2323

2424
dependencies {
2525
implementation fileTree(dir: 'libs', include: ['*.jar'])
26-
testCompile 'junit:junit:4.12'
2726
implementation "com.android.support:appcompat-v7:$support_library_version"
2827
implementation project(':query')
2928
implementation project(':annotations')
File renamed without changes.

example/src/main/java/com/quarkworks/android/realmtypesafequery/example/MainActivity.java renamed to example_javaOnly/src/main/java/com/quarkworks/android/realmtypesafequery/example/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
public class MainActivity extends AppCompatActivity {
1818

19-
private static final String TAG = MainActivity.class.getSimpleName();
19+
private static final String TAG = MainActivity.class.getSimpleName() + " javaOnly";
2020

2121

2222
@Override

0 commit comments

Comments
 (0)