├── .gitignore ├── .idea └── codeStyleSettings.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── github │ │ └── gfx │ │ └── android │ │ └── orma_kotlin_example │ │ ├── App.kt │ │ ├── BenchmarkFragment.kt │ │ ├── DataClass.kt │ │ ├── HandWrittenOpenHelper.kt │ │ ├── Item.kt │ │ ├── Location.kt │ │ ├── MainActivity.kt │ │ ├── OrmaHolder.kt │ │ ├── OrmaTodo.kt │ │ ├── RealmTodo.kt │ │ └── User.kt │ └── res │ ├── layout │ ├── activity_main.xml │ ├── fragment_benchmark.xml │ ├── item.xml │ └── item_result.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── circle.yml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .idea/* 5 | !.idea/codeStyleSettings.xml 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/codeStyleSettings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 370 | 372 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Orma-Kotlin-Example [![CircleCI](https://circleci.com/gh/gfx/OrmaWithKotlin.svg?style=svg)](https://circleci.com/gh/gfx/OrmaWithKotlin) 2 | 3 | Example Kotlin app for [Android Orma](https://github.com/gfx/Android-Orma/issues). 4 | 5 | Note that `OrmaDatabase` can't be handled by Dagger2+kapt because of kapt restrictions, as of Kotlin 1.0.2. 6 | 7 | Use singleton instances instead. 8 | 9 | ## See Also 10 | 11 | * https://github.com/JetBrains/kotlin 12 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.1.0' 3 | repositories { 4 | jcenter() 5 | } 6 | dependencies { 7 | classpath 'io.realm:realm-gradle-plugin:3.0.0' 8 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 9 | classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" 10 | } 11 | } 12 | 13 | apply plugin: 'com.android.application' 14 | apply plugin: 'kotlin-android' 15 | apply plugin: 'kotlin-android-extensions' 16 | apply plugin: 'kotlin-kapt' 17 | apply plugin: 'realm-android' 18 | 19 | android { 20 | compileSdkVersion 25 21 | buildToolsVersion '25.0.2' 22 | dataBinding.enabled = true 23 | defaultConfig { 24 | applicationId "com.github.gfx.android.orma_example_kotlin" 25 | minSdkVersion 16 26 | targetSdkVersion 25 27 | versionCode 1 28 | versionName '1.0.0' 29 | } 30 | buildTypes { 31 | release { 32 | minifyEnabled false 33 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 34 | } 35 | } 36 | lintOptions { 37 | abortOnError false 38 | disable 'SetTextI18n' 39 | textReport true 40 | textOutput 'stdout' 41 | } 42 | packagingOptions { 43 | exclude 'META-INF/LICENSE.txt' 44 | exclude 'META-INF/services/javax.annotation.processing.Processor' 45 | } 46 | productFlavors { 47 | } 48 | } 49 | 50 | dependencies { 51 | kapt 'com.github.gfx.android.orma:orma-processor:4.2.1' 52 | compile 'com.github.gfx.android.orma:orma:4.2.1' 53 | kapt 'com.android.databinding:compiler:2.3.0' 54 | compile 'com.android.support:appcompat-v7:25.2.0' 55 | compile 'com.android.support:cardview-v7:25.2.0' 56 | compile 'com.android.support:design:25.2.0' 57 | compile 'io.reactivex.rxjava2:rxjava:2.0.6' 58 | compile 'io.reactivex.rxjava2:rxandroid:2.0.1' 59 | compile 'com.jakewharton.threetenabp:threetenabp:1.0.4' 60 | compile 'com.facebook.stetho:stetho:1.4.2' 61 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 62 | } 63 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /opt/brew/opt/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/App.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma_kotlin_example 2 | 3 | import android.app.Application 4 | import io.realm.Realm 5 | 6 | class App: Application() { 7 | override fun onCreate() { 8 | super.onCreate() 9 | 10 | OrmaHolder.initialize(this) 11 | Realm.init(this) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/BenchmarkFragment.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma.example.fragment 2 | 3 | import android.annotation.SuppressLint 4 | import android.content.Context 5 | import android.database.sqlite.SQLiteDatabase 6 | import android.os.Bundle 7 | import android.support.v4.app.Fragment 8 | import android.util.Log 9 | import android.view.LayoutInflater 10 | import android.view.View 11 | import android.view.ViewGroup 12 | import android.widget.ArrayAdapter 13 | import android.widget.Toast 14 | import com.github.gfx.android.orma.AccessThreadConstraint 15 | import com.github.gfx.android.orma.example.handwritten.HandWrittenOpenHelper 16 | import com.github.gfx.android.orma_kotlin_example.OrmaDatabase 17 | import com.github.gfx.android.orma_kotlin_example.OrmaTodo 18 | import com.github.gfx.android.orma_kotlin_example.RealmTodo 19 | import com.github.gfx.android.orma_kotlin_example.databinding.FragmentBenchmarkBinding 20 | import com.github.gfx.android.orma_kotlin_example.databinding.ItemResultBinding 21 | import io.reactivex.Single 22 | import io.reactivex.android.schedulers.AndroidSchedulers 23 | import io.reactivex.schedulers.Schedulers 24 | import io.realm.Realm 25 | import io.realm.RealmConfiguration 26 | import io.realm.Sort 27 | import java.util.* 28 | import java.util.concurrent.TimeUnit 29 | import java.util.concurrent.atomic.AtomicInteger 30 | 31 | class BenchmarkFragment : Fragment() { 32 | 33 | internal val titlePrefix = "title " 34 | 35 | internal val contentPrefix = "content content content\n" + "content content content\n" + "content content content\n" + " " 36 | 37 | internal lateinit var orma: OrmaDatabase 38 | 39 | internal lateinit var hw: HandWrittenOpenHelper 40 | 41 | internal lateinit var binding: FragmentBenchmarkBinding 42 | 43 | internal lateinit var adapter: ResultAdapter 44 | 45 | override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { 46 | binding = FragmentBenchmarkBinding.inflate(inflater, container, false) 47 | 48 | adapter = ResultAdapter(context) 49 | binding.list.setAdapter(adapter) 50 | 51 | binding.run.setOnClickListener({ v -> run() }) 52 | 53 | return binding.getRoot() 54 | } 55 | 56 | override fun onResume() { 57 | super.onResume() 58 | 59 | val realmConf = RealmConfiguration.Builder().build() 60 | Realm.setDefaultConfiguration(realmConf) 61 | Realm.deleteRealm(realmConf) 62 | 63 | Schedulers.io().createWorker().schedule { 64 | context.deleteDatabase("orma-benchmark.db") 65 | orma = OrmaDatabase.builder(context) 66 | .name("orma-benchmark.db") 67 | .readOnMainThread(AccessThreadConstraint.NONE) 68 | .writeOnMainThread(AccessThreadConstraint.NONE) 69 | .trace(false) 70 | .build() 71 | orma.migrate() 72 | } 73 | 74 | context.deleteDatabase("hand-written.db") 75 | hw = HandWrittenOpenHelper(context, "hand-written.db") 76 | } 77 | 78 | override fun onPause() { 79 | super.onPause() 80 | } 81 | 82 | internal fun run() { 83 | Log.d(TAG, "Start performing a set of benchmarks") 84 | 85 | adapter.clear() 86 | 87 | val realm = Realm.getDefaultInstance() 88 | realm.executeTransaction { r -> r.delete(RealmTodo::class.java) } 89 | realm.close() 90 | 91 | hw.writableDatabase.execSQL("DELETE FROM todo") 92 | 93 | orma.deleteFromOrmaTodo().executeAsSingle().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).flatMap({ integer -> startInsertWithOrma() }).flatMap({ result -> 94 | adapter.add(result) 95 | startInsertWithRealm() // Realm objects can only be accessed on the thread they were created. 96 | }).flatMap({ result -> 97 | adapter.add(result) 98 | startInsertWithHandWritten() 99 | }).flatMap({ result -> 100 | adapter.add(result) 101 | startSelectAllWithOrma() 102 | }).flatMap({ result -> 103 | adapter.add(result) 104 | startSelectAllWithRealm() // Realm objects can only be accessed on the thread they were created. 105 | }).flatMap({ result -> 106 | adapter.add(result) 107 | startSelectAllWithHandWritten() 108 | }).subscribe( 109 | { result -> adapter.add(result) }, 110 | { error -> 111 | Log.wtf(TAG, error) 112 | Toast.makeText(context, error.message, Toast.LENGTH_LONG).show() 113 | 114 | }) 115 | } 116 | 117 | internal fun startInsertWithOrma(): Single { 118 | return Single.fromCallable { 119 | val result = runWithBenchmark( { 120 | orma.transactionSync({ 121 | val now = System.currentTimeMillis() 122 | 123 | val statement = orma.prepareInsertIntoOrmaTodo() 124 | 125 | for (i in 0..N_ITEMS - 1) { 126 | val todo = OrmaTodo(0, titlePrefix + 1, contentPrefix + 1, false, Date(now)) 127 | statement.execute(todo) 128 | } 129 | }) 130 | }) 131 | Result("Orma/insert", result) 132 | }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) 133 | } 134 | 135 | internal fun startInsertWithRealm(): Single { 136 | return Single.fromCallable { 137 | val result = runWithBenchmark( { 138 | val realm = Realm.getDefaultInstance() 139 | realm.executeTransaction { r -> 140 | val now = System.currentTimeMillis() 141 | 142 | for (i in 0..N_ITEMS - 1) { 143 | val todo = r.createObject(RealmTodo::class.java) 144 | 145 | todo.title = titlePrefix + i 146 | todo.content = contentPrefix + i 147 | todo.createdTime = Date(now) 148 | } 149 | } 150 | realm.close() 151 | }) 152 | Result("Realm/insert", result) 153 | }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) 154 | } 155 | 156 | internal fun startInsertWithHandWritten(): Single { 157 | return Single.fromCallable { 158 | val result = runWithBenchmark({ 159 | val db = hw.writableDatabase 160 | db.beginTransaction() 161 | 162 | val inserter = db.compileStatement( 163 | "INSERT INTO todo (title, content, done, createdTime) VALUES (?, ?, ?, ?)") 164 | 165 | val now = System.currentTimeMillis() 166 | 167 | for (i in 1..N_ITEMS) { 168 | inserter.bindAllArgsAsStrings(arrayOf(titlePrefix + i, // title 169 | contentPrefix + i, // content 170 | "0", // done 171 | now.toString())// createdTime 172 | ) 173 | inserter.executeInsert() 174 | } 175 | 176 | db.setTransactionSuccessful() 177 | db.endTransaction() 178 | }) 179 | Result("HandWritten/insert", result) 180 | 181 | }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) 182 | } 183 | 184 | internal fun startSelectAllWithOrma(): Single { 185 | return Single.fromCallable { 186 | val result = runWithBenchmark({ 187 | val count = AtomicInteger() 188 | 189 | val todos = orma.selectFromOrmaTodo().orderByCreatedTimeAsc() 190 | 191 | for (todo in todos) { 192 | @SuppressWarnings("unused") 193 | val title = todo.title 194 | @SuppressWarnings("unused") 195 | val content = todo.content 196 | @SuppressWarnings("unused") 197 | val createdTime = todo.createdTime 198 | 199 | count.incrementAndGet() 200 | } 201 | 202 | if (todos.count() !== count.get()) { 203 | throw AssertionError("unexpected get: " + count.get()) 204 | } 205 | Log.d(TAG, "Orma/forEachAll count: " + count) 206 | }) 207 | Result("Orma/forEachAll", result) 208 | }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) 209 | } 210 | 211 | internal fun startSelectAllWithRealm(): Single { 212 | return Single.fromCallable { 213 | val result = runWithBenchmark({ 214 | val count = AtomicInteger() 215 | val realm = Realm.getDefaultInstance() 216 | val results = realm.where(RealmTodo::class.java).findAllSorted("createdTime", Sort.ASCENDING) 217 | for (todo in results) { 218 | @SuppressWarnings("unused") 219 | val title = todo.title 220 | @SuppressWarnings("unused") 221 | val content = todo.content 222 | @SuppressWarnings("unused") 223 | val createdTime = todo.createdTime 224 | 225 | count.incrementAndGet() 226 | } 227 | if (results.size != count.get()) { 228 | throw AssertionError("unexpected get: " + count.get()) 229 | } 230 | realm.close() 231 | 232 | Log.d(TAG, "Realm/forEachAll count: " + count) 233 | }) 234 | Result("Realm/forEachAll", result) 235 | } 236 | } 237 | 238 | internal fun startSelectAllWithHandWritten(): Single { 239 | return Single.fromCallable { 240 | val result = runWithBenchmark({ 241 | val count = AtomicInteger() 242 | 243 | val db = hw.readableDatabase 244 | val cursor = db.query( 245 | "todo", 246 | arrayOf("id, title, content, done, createdTime"), 247 | null, null, null, null, "createdTime ASC" // whereClause, whereArgs, groupBy, having, orderBy 248 | ) 249 | 250 | if (cursor.moveToFirst()) { 251 | val titleIndex = cursor.getColumnIndexOrThrow("title") 252 | val contentIndex = cursor.getColumnIndexOrThrow("content") 253 | val createdTimeIndex = cursor.getColumnIndexOrThrow("createdTime") 254 | do { 255 | @SuppressWarnings("unused") 256 | val title = cursor.getString(titleIndex) 257 | @SuppressWarnings("unused") 258 | val content = cursor.getString(contentIndex) 259 | @SuppressWarnings("unused") 260 | val createdTime = Date(cursor.getLong(createdTimeIndex)) 261 | 262 | count.incrementAndGet() 263 | } while (cursor.moveToNext()) 264 | } 265 | cursor.close() 266 | 267 | val dbCount = longForQuery(db, "SELECT COUNT(*) FROM todo", null) 268 | if (dbCount != count.get().toLong()) { 269 | throw AssertionError("unexpected get: " + count.get() + " != " + dbCount) 270 | } 271 | 272 | Log.d(TAG, "HandWritten/forEachAll count: " + count) 273 | }) 274 | Result("HandWritten/forEachAll", result) 275 | }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) 276 | } 277 | 278 | internal class Result(val title: String, val elapsedMillis: Long) 279 | 280 | internal class ResultAdapter(context: Context) : ArrayAdapter(context, 0) { 281 | 282 | override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 283 | @SuppressLint("ViewHolder") val binding = ItemResultBinding.inflate(LayoutInflater.from(context), parent, false) 284 | 285 | val result = getItem(position)!! 286 | binding.title.setText(result.title) 287 | binding.elapsed.setText("${result.elapsedMillis}ms") 288 | 289 | return binding.getRoot() 290 | } 291 | } 292 | 293 | companion object { 294 | 295 | internal val TAG = BenchmarkFragment::class.java.simpleName 296 | 297 | internal val N_ITEMS = 10 298 | 299 | internal val N_OPS = 100 300 | 301 | fun newInstance(): Fragment { 302 | return BenchmarkFragment() 303 | } 304 | 305 | internal fun longForQuery(db: SQLiteDatabase, sql: String, args: Array?): Long { 306 | val cursor = db.rawQuery(sql, args) 307 | cursor.moveToFirst() 308 | val value = cursor.getLong(0) 309 | cursor.close() 310 | return value 311 | } 312 | 313 | internal fun runWithBenchmark(task: () -> Unit): Long { 314 | val t0 = System.nanoTime() 315 | 316 | for (i in 0..N_OPS - 1) { 317 | task() 318 | } 319 | 320 | return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t0) 321 | } 322 | } 323 | 324 | } 325 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/DataClass.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma_kotlin_example 2 | 3 | 4 | import android.support.annotation.Nullable 5 | import com.github.gfx.android.orma.annotation.Column 6 | import com.github.gfx.android.orma.annotation.PrimaryKey 7 | import com.github.gfx.android.orma.annotation.Setter 8 | import com.github.gfx.android.orma.annotation.Table 9 | 10 | @Table 11 | data class DataClass 12 | ( 13 | @Setter("id") @PrimaryKey var id: Long, 14 | @Setter("todo") @Column @Nullable var todo: OrmaTodo?, 15 | @Setter("content") @Column @Nullable var content: String? 16 | ) 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/HandWrittenOpenHelper.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma.example.handwritten 2 | 3 | import android.content.Context 4 | import android.database.sqlite.SQLiteDatabase 5 | import android.database.sqlite.SQLiteOpenHelper 6 | 7 | class HandWrittenOpenHelper(context: Context, name: String) : SQLiteOpenHelper(context, name, null, HandWrittenOpenHelper.VERSION) { 8 | 9 | override fun onCreate(db: SQLiteDatabase) { 10 | db.execSQL("CREATE TABLE todo (" 11 | + "id INTEGER PRIMARY KEY," 12 | + "title TEXT NOT NULL," 13 | + "content TEXT NULL," 14 | + "done BOOLEAN NOT NULL," 15 | + "createdTime INTEGER NOT NULL" 16 | + ")") 17 | db.execSQL("CREATE INDEX title_on_todo ON todo (title)") 18 | db.execSQL("CREATE INDEX createdTime_on_todo ON todo (createdTime)") 19 | } 20 | 21 | override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { 22 | db.execSQL("DROP TABLE todo") 23 | } 24 | 25 | override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { 26 | // nop 27 | } 28 | 29 | companion object { 30 | 31 | internal val VERSION = 4 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/Item.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 FUJI Goro (gfx). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.gfx.android.orma_kotlin_example 18 | 19 | import android.support.annotation.Nullable 20 | import com.github.gfx.android.orma.annotation.Column 21 | import com.github.gfx.android.orma.annotation.PrimaryKey 22 | import com.github.gfx.android.orma.annotation.Setter 23 | import com.github.gfx.android.orma.annotation.Table 24 | import java.sql.Timestamp 25 | 26 | @Table 27 | class Item { 28 | 29 | @PrimaryKey(autoincrement = true) val id: Long 30 | 31 | @Column val content: String 32 | 33 | @Nullable 34 | @Column val user: User? 35 | 36 | @Column val createdTime: Timestamp 37 | 38 | constructor( 39 | @Setter("id") id: Long, 40 | @Setter("content") content: String, 41 | @Setter("user") user: User?, 42 | @Setter("createdTime") createdTime: Timestamp) { 43 | this.id = id 44 | this.content = content 45 | this.user = user 46 | this.createdTime = createdTime 47 | } 48 | 49 | constructor(id: Long, content: String) { 50 | this.id = id 51 | this.content = content 52 | this.user = null 53 | this.createdTime = Timestamp(System.currentTimeMillis()) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/Location.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma_kotlin_example 2 | 3 | import com.github.gfx.android.orma.annotation.Column 4 | import com.github.gfx.android.orma.annotation.Setter 5 | import com.github.gfx.android.orma.annotation.Table 6 | 7 | @Table 8 | data class Location( 9 | @Setter("latitude") @Column val latitude: Double, 10 | @Setter("longitude") @Column val longitude: Double) { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma_kotlin_example 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | 6 | class MainActivity : AppCompatActivity() { 7 | 8 | override fun onCreate(savedInstanceState: Bundle?) { 9 | super.onCreate(savedInstanceState) 10 | setContentView(R.layout.activity_main) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/OrmaHolder.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma_kotlin_example 2 | 3 | import android.content.Context 4 | 5 | object OrmaHolder { 6 | lateinit var ORMA: OrmaDatabase; 7 | 8 | fun initialize(context: Context) { 9 | ORMA = OrmaDatabase.builder(context).build(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/OrmaTodo.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma_kotlin_example 2 | 3 | import com.github.gfx.android.orma.annotation.Column 4 | import com.github.gfx.android.orma.annotation.PrimaryKey 5 | import com.github.gfx.android.orma.annotation.Setter 6 | import com.github.gfx.android.orma.annotation.Table 7 | import java.util.* 8 | 9 | @Table 10 | data class OrmaTodo( 11 | @Setter("id") @PrimaryKey var id: Long, 12 | @Setter("title") @Column(indexed = true) var title: String, 13 | @Setter("content") @Column var content: String, 14 | @Setter("done") @Column var done: Boolean, 15 | @Setter("createdTime") @Column(indexed = true) var createdTime: Date 16 | ) { 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/RealmTodo.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma_kotlin_example 2 | 3 | import io.realm.RealmObject 4 | import io.realm.annotations.Index 5 | import java.util.* 6 | 7 | open class RealmTodo : RealmObject() { 8 | 9 | // The primary key is generated by Realm 10 | 11 | @Index 12 | var title: String? = null 13 | 14 | var content: String? = null 15 | 16 | var done: Boolean = false 17 | 18 | @Index 19 | var createdTime: Date = Date() 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/gfx/android/orma_kotlin_example/User.kt: -------------------------------------------------------------------------------- 1 | package com.github.gfx.android.orma_kotlin_example 2 | 3 | import com.github.gfx.android.orma.annotation.Column 4 | import com.github.gfx.android.orma.annotation.PrimaryKey 5 | import com.github.gfx.android.orma.annotation.Table 6 | 7 | @Table 8 | class User { 9 | @PrimaryKey(autoincrement = true) var id: Long = 0 10 | 11 | @Column var name: String? = null 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_benchmark.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 16 | 17 |