Last active
June 14, 2023 07:10
-
-
Save tobitech/f5fddb189fc48e7f0831a98a2da26fd7 to your computer and use it in GitHub Desktop.
Creating Realm Model Objects in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define a Category object type that has a name and a color | |
class Category() : RealmObject { | |
@PrimaryKey | |
var _id: ObjectId = ObjectId.create() | |
var name: String | |
var color: String | |
constructor(name: String, color: String) : this() { | |
this.name = name | |
this.color = color | |
} | |
} | |
// Define a Transaction object type that has an amount, a date, a note and a category | |
class Transaction() : RealmObject { | |
@PrimaryKey | |
var _id: ObjectId = ObjectId.create() | |
var amount: Double | |
var date: Date | |
var note: String? | |
var category: Category? | |
constructor(amount: Double, date: Date, note: String?, category: Category?) : this() { | |
this.amount = amount | |
this.date = date | |
this.note = note | |
this.category = category | |
} | |
} | |
// using RealmInstant | |
// Define a Transaction object type that has an amount, a date, a note and a category | |
class Transaction() : RealmObject { | |
@PrimaryKey | |
var _id: ObjectId = ObjectId.create() | |
var amount: Double = 0.0 | |
private var _date: RealmInstant = RealmInstant.from(0, 0) // Private RealmInstant property | |
public var date: Instant // Public Instant property with getter and setter | |
get() { | |
return _date.toInstant() | |
} | |
set(value) { | |
_date = value.toRealmInstant() | |
} | |
var note: String? = null | |
var category: Category? = null // To-one relationship with Category | |
constructor(amount: Double, date: Instant, note: String?, category: Category?) : this() { | |
this.amount = amount | |
this.date = date | |
this.note = note | |
this.category = category | |
} | |
} | |
// Extension functions to convert between RealmInstant and Instant | |
fun RealmInstant.toInstant(): Instant { | |
val sec: Long = this.epochSeconds // The value always lies in the range `-999_999_999..999_999_999`. | |
// minus for timestamps before epoch, positive for after | |
val nano: Int = this.nanosecondsOfSecond | |
return if (sec >= 0) { | |
// For positive timestamps, conversion can happen directly | |
Instant.fromEpochSeconds(sec, nano.toLong()) | |
} else { | |
// For negative timestamps, RealmInstant starts from the higher value with negative | |
// nanoseconds, while Instant starts from the lower value with positive nanoseconds | |
val adjustedSec = sec + 1 | |
val adjustedNano = nano - 1_000_000_000 | |
Instant.fromEpochSeconds(adjustedSec, adjustedNano.toLong()) | |
} | |
} | |
fun Instant.toRealmInstant(): RealmInstant { | |
val sec = this.epochSeconds | |
val nano = this.nanosecondsOfSecond | |
return if (sec >= 0) { | |
// For positive timestamps, conversion can happen directly | |
RealmInstant.from(sec, nano) | |
} else { | |
// For negative timestamps, we need to adjust the values as explained above | |
val adjustedSec = sec - 1 | |
val adjustedNano = nano + 1_000_000_000 | |
RealmInstant.from(adjustedSec, adjustedNano) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment