Skip to content

Commit

Permalink
Kotlin java union (#13)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JamesDurham authored Apr 10, 2018
1 parent c2133c5 commit b677dd7
Show file tree
Hide file tree
Showing 59 changed files with 822 additions and 30 deletions.
131 changes: 107 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ realm.where(Person.class).equalTo(PersonFieldNames.FIRST_NAME, "Sally").findFirs
RealmTypeSafeQuery.with(realm).where(Person.class).equalTo(PersonFields.FIRST_NAME, "Sally").findFirst();
```

## How to include
## How to include _JavaOnly project_

#### In your top level build file, add the jitpack repository along with realm
```groovy
buildscript {
dependencies {
classpath "io.realm:realm-gradle-plugin:4.3.1" // supported version of realm
classpath "io.realm:realm-gradle-plugin:4.3.4" // supported version of realm
}
}
allprojects {
repositories {
jcenter()
google()
maven { url "https://jitpack.io" } // RTSQ is hosted on jitpack
}
}
Expand All @@ -46,17 +47,23 @@ allprojects {
#### App module build file dependencies:
```groovy
apply plugin: 'realm-android' // realm setup at top of file
// requires java 8
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
android {
...[elided]...
// requires java 8
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
...[elided]...
}
dependencies {
compileOnly 'com.github.quarkworks.RealmTypeSafeQuery-Android:annotations:{{version_number}}' // annotations
annotationProcessor 'com.github.quarkworks.RealmTypeSafeQuery-Android:annotationprocessor:{{version_number}}' // annotation processor
implementation 'com.github.quarkworks.RealmTypeSafeQuery-Android:query:{{version_number}}' // query dsl
...[elided]...
compileOnly "com.github.quarkworks.RealmTypeSafeQuery-Android:annotations:$RTSQ_version" // annotations
annotationProcessor "com.github.quarkworks.RealmTypeSafeQuery-Android:annotationprocessor:$RTSQ_version" // annotation processor
implementation "com.github.quarkworks.RealmTypeSafeQuery-Android:query:$RTSQ_version" // query dsl
...[elided]...
}
```

#### Example Model
Expand All @@ -72,7 +79,7 @@ class Person extends RealmObject {

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

#### Bonus

```java
## How to include _KotlinOnly project_

final Realm realm = ...
#### In your top level build file, add the jitpack repository along with realm
```groovy
buildscript {
dependencies {
classpath "io.realm:realm-gradle-plugin:4.3.4" // supported version of realm
}
}
allprojects {
repositories {
jcenter()
google()
maven { url "https://jitpack.io" } // RTSQ is hosted on jitpack
}
}
```

#### App module build file dependencies:
```groovy
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android' // realm setup at top of file but below 'kotlin-kapt'
android {
...[elided]...
// requires java 8 (android build issue)
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
...[elided]...
}
// For chainable sorting
RealmTypeSafeQuery.with(realm).where(ExampleModel.class).sort(field1).sort(field3).sort(field2).findAll();
dependencies {
...[elided]...
compileOnly "com.github.quarkworks.RealmTypeSafeQuery-Android:annotations:$RTSQ_version" // annotations
// use kapt not annotationProcessor
kapt "com.github.quarkworks.RealmTypeSafeQuery-Android:annotationprocessor:$RTSQ_version" // annotation processor
implementation "com.github.quarkworks.RealmTypeSafeQuery-Android:query:$RTSQ_version" // query dsl
...[elided]...
}
```

#### Example Model
```kotlin
@GenerateRealmFields // Generates a file called PersonFields.java. This is a RTSQ annotation.
@GenerateRealmFieldNames // Generates a file called PersonFieldNames.java This is a RTSQ annotation.
// Kotlin classes are final by default (notice open)
open class Person : RealmObject() {
var firstName: String? = null
var lastName: String? = null
var birthday: Date? = null

// For creating query groups with lambdas
RealmTypeSafeQuery.with(realm).where(ExampleModel.class).group((query) -> {}).findAll();
RealmTypeSafeQuery.with(realm).where(ExampleModel.class).or((query) -> {}).findAll();

// For those pesky CSV fields that have a delimiter
final String delimiter = ",";
RealmTypeSafeQuery.with(realm).where(ExampleModel.class).contains(field, value, delimiter).findAll();
var pets: RealmList<Pet>? = null

// If what pops out of the code generator doesn't compile add these annotations.
// Realm constantly updates their api and RTSQ might be a little behind.
@SkipGenerationOfRealmFieldName
@SkipGenerationOfRealmField
var website: RealmList<String>? = null
}

@GenerateRealmFields // Generates a file called PetFields.java.
@GenerateRealmFieldNames // Generates a file called PetFieldNames.java.
open class Pet : RealmObject() {
var name: String? = null
var weight: Int? = null
}
```

#### Example Queries

```kotlin
Realm.init(this.applicationContext)

Realm.getDefaultInstance().use { realm ->
realm.executeTransaction { realm ->

val sallyNotSmiths = RealmTypeSafeQuery.with(realm).where(Person::class.java)
.equalTo(PersonFields.FIRST_NAME, "Sally")
.notEqualTo(PersonFields.LAST_NAME, "Smith", Case.INSENSITIVE)
.lessThan(PersonFields.BIRTHDAY, Date())
.findAllSorted(PersonFields.BIRTHDAY, Sort.ASCENDING)

//Link queries also work too

val peopleWithHeavyPets = RealmTypeSafeQuery.with(realm).where(Person::class.java)
.greaterThan(PersonFields.PETS.link(PetFields.WEIGHT), 9000).findAll()
}
}
```
1 change: 1 addition & 0 deletions annotationprocessor/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
apply plugin: 'maven' // jitpack.io

buildscript {
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.2.0'
ext.realm_version = '4.3.1'
ext.kotlin_version = '1.2.20'
ext.realm_version = '4.3.4'
ext.sdk_version = 25
ext.build_tools_version = '27.0.3'
ext.support_library_version = '25.1.1'
Expand All @@ -13,7 +13,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.1.0-rc02'
classpath 'com.android.tools.build:gradle:3.1.0'
//noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.realm:realm-gradle-plugin:$realm_version"
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion example/build.gradle → example_javaOnly/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
implementation "com.android.support:appcompat-v7:$support_library_version"
implementation project(':query')
implementation project(':annotations')
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class MainActivity extends AppCompatActivity {

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


@Override
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions example_kotlinJavaMix/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
32 changes: 32 additions & 0 deletions example_kotlinJavaMix/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

android {
compileSdkVersion sdk_version
buildToolsVersion build_tools_version

defaultConfig {
applicationId "com.quarkworks.android.realmtypesafequery.example"
minSdkVersion 19
targetSdkVersion sdk_version
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.android.support:appcompat-v7:$support_library_version"
implementation project(':query')
implementation project(':annotations')
kapt project(':annotationprocessor')
}
17 changes: 17 additions & 0 deletions example_kotlinJavaMix/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/berbschloe/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
22 changes: 22 additions & 0 deletions example_kotlinJavaMix/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quarkworks.android.realmtypesafequery.example">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivityJavaCallMix"></activity>
<activity android:name=".MainActivityKotlincallMix"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.quarkworks.android.realmtypesafequery.example

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button

class MainActivity : Activity() {
var javaCallMix:Button? = null;
var kotlinCallMix:Button? = null;

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
javaCallMix = findViewById(R.id.javaCallMix) as Button?
kotlinCallMix = findViewById(R.id.kotlinCallMix) as Button?
javaCallMix!!.setOnClickListener {
val i = Intent(this, MainActivityJavaCallMix::class.java)
startActivity(i)

}
kotlinCallMix!!.setOnClickListener {
val i = Intent(this, MainActivityKotlincallMix::class.java)
startActivity(i)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.quarkworks.android.realmtypesafequery.example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;


import com.quarkworks.android.realmtypesafequery.RealmTypeSafeQuery;
import com.quarkworks.android.realmtypesafequery.generated.TestRecordKtFieldNames;
import com.quarkworks.android.realmtypesafequery.generated.TestRecordKtFields;

import java.util.Date;

import io.realm.Realm;
import io.realm.RealmConfiguration;

public class MainActivityJavaCallMix extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_mix);

Object f1 = TestRecordKtFieldNames.DATE_FIELD;
Object f2 = TestRecordKtFieldNames.STRING_FIELD;

Realm.init(this.getApplicationContext());

RealmConfiguration config = new RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().build();
Realm.setDefaultConfiguration(config);

try (Realm realm = Realm.getDefaultInstance()) {

realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.deleteAll();
for (int i = 0; i < 10; i++) {
TestRecordKt record = realm.createObject(TestRecordKt.class, String.valueOf(i));
record.setBooleanField ( i % 2 == 0 );
record.setByteArrayField( new byte[]{(byte) i} );
record.setByteField ( (byte) i );
record.setDateField ( new Date(i * 1000) );
record.setDoubleField ( i * 1000d );
record.setFloatField ( i * 2000f );
record.setIntegerField ( i );
record.setLongField ( i * 10L );
record.setShortField ( (short) i );
record.setStringField ( i % 3 == 0 ? null : String.valueOf(i) );
record.setIgnoredField ( new Object() );
record.setIndexedField ( "indexed value: " + i );
record.setRequiredField ( String.valueOf(i) );
}
}
});

Log.d(TAG, "TestRecordKt Count:" + RealmTypeSafeQuery.with(realm).where(TestRecordKt.class).count());

Log.d(TAG, "TestRecordKt Equal To 1: " + RealmTypeSafeQuery.with(realm).where(TestRecordKt.class).equalTo(TestRecordKtFields.STRING_FIELD, "1").findAll().toString());
Log.d(TAG, "TestRecordKt Equal To null: " + RealmTypeSafeQuery.with(realm).where(TestRecordKt.class).equalTo(TestRecordKtFields.STRING_FIELD, null).findAll().toString());
Log.d(TAG, "TestRecordKt IsNull: " + RealmTypeSafeQuery.with(realm).where(TestRecordKt.class).isNull(TestRecordKtFields.STRING_FIELD).findAll().toString());
Log.d(TAG, "TestRecordKt IsNotNull: " + RealmTypeSafeQuery.with(realm).where(TestRecordKt.class).isNotNull(TestRecordKtFields.STRING_FIELD).findAll().toString());
}
}
}
Loading

0 comments on commit b677dd7

Please sign in to comment.